0
votes

I read this about the URL Loading System:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i

Can't figure out what class use and how to use it.

errorSreenshot

2
You have to use NSURLSession to make network request , check out this tutorial raywenderlich.com/110458/nsurlsession-tutorial-getting-started. You can also use third party framework Alamofire ,which makes life easier check this out github.com/Alamofire/Alamofirevishnuvarthan
NSURLSession is easy to use for simple cases, no need for any third-party framework. Example: stackoverflow.com/a/31808605/2227743Eric Aya

2 Answers

2
votes

I suggest using a third party framework like Alamofire The native Swift networking is rather complicated, especially for beginners, because Swift is type-safe language and a JSON object can have nesting etc. that can only be seen after actually fetching of the object.

The good thing is that Alamofire can be easily installed with CocoaPods and it is actively being developed. Here is an example of a request made with Alamofire's 4.0 version for Swift 3 to fetch a JSON object from a url called yourUrl:

Alamofire.request("https://yourUrl").responseJSON { response in
  print(response.request)  // original URL request
  print(response.response) // HTTP URL response
  print(response.data)     // server data
  print(response.result)   // result of response serialization

  if let json = response.result.value {
    print("JSON: \(json)")
    // do something with json here
  }
}

You can then use another third party framework like SwiftyJSON to parse the JSON you retrieve - especially if it has a complicated structure. But sadly there is no update in sight for Swift 3 so I guess we have to wait and see how this pans out.

EDIT: There is an unofficial Swift 3 branch for SwiftyJSON, which can be installed with cocoapods (it works for me on iOS10/Swift 3):

pod 'SwiftyJSON', git: 'https://github.com/BaiduHiDeviOS/SwiftyJSON.git', branch: 'swift3'
0
votes

Use following function to load URL.

fun loadURL()->void{
 let defaultSession = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
    var dataTask: NSURLSessionDataTask?
    let url = NSURL(string: "URL TO LOAD")//Add url string here
    dataTask = defaultSession.dataTaskWithURL(url!) {
        data, response, error in
        dispatch_async(dispatch_get_main_queue()) {
            UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        }
        if let error = error {
            print(error.localizedDescription)
        } else if let httpResponse = response as? NSHTTPURLResponse {
            if httpResponse.statusCode == 200 {
                print(data);
                //Process data here..
            }
        }
    }
    dataTask?.resume()
}

You can use build in NSJSonSerialization class to convert NSdata into NSDictionary and then parse Dictionary to extract values.Your parsing logic will be added //Process data here.. in place of this comment in above code base.