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.

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
DosCadenas
  • 95
  • 1
  • 10

1 Answers1

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.

Community
  • 1
  • 1
Ryan
  • 3,853
  • 4
  • 28
  • 32
  • 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? – DosCadenas Jun 23 '14 at 02:25
  • And does AFNetworking need to be performed on a separate thread? – DosCadenas Jun 23 '14 at 02:55
  • 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! – Ryan Jun 23 '14 at 04:43