0

I want to use an api for a swift app and I need to make a URL request to retrieve information in a json format.

I tried to follow an online example:

var urlString = "http://api.shephertz.com" // Your Normal URL String
var url = NSURL.URLWithString(urlString)// Creating URL
var request = NSURLRequest(URL: url) // Creating Http Request
var response:AutoreleasingUnsafePointer<NSURLResponse?> = nil;
var error: AutoreleasingUnsafePointer<NSErrorPointer?> = nil;
// Sending Synchronous request using NSURLConnection
var responseData = NSURLConnection.sendSynchronousRequest(request, returningResponse: response, error:nil) as NSData
if error != nil
{
   // You can handle error response here  
}
else
{
   //Converting data to String
   var responseStr:NSString = NSString(data:responseData, encoding:NSUTF8StringEncoding) 
}

But I haven't ever seen the AutoreleasingUnsafePointer used and I can't find anything about it in the Apple's documentation. The above example did not work and I was wondering if there was any other way to do it.

1 Answer 1

1

This isn't a quick fix, but I recommend AFNetworking, which makes this way easier. The top answer here explains how to integrate AFNetworking with Swift in about 2 minutes.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the link. I made the call but I am having a little trouble with the networking, getting a "Network connection lost" error. Any specific reason for this?
And does AFNetworking need to be performed on a separate thread?
No, you can make calls to AFNetworking from the main thread. They handle threading then call your completion block on the main thread when done!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.