0

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

Matteo Villa
  • 47
  • 2
  • 12
  • You have to use NSURLSession to make network request , check out this tutorial https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started. You can also use third party framework Alamofire ,which makes life easier check this out https://github.com/Alamofire/Alamofire – vishnuvarthan Sep 13 '16 at 07:36
  • NSURLSession is easy to use for simple cases, no need for any third-party framework. Example: http://stackoverflow.com/a/31808605/2227743 – Eric Aya Sep 13 '16 at 07:58

2 Answers2

2

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'
tech4242
  • 2,348
  • 2
  • 23
  • 33
  • thanks! how can i see if i have actually installed cocoapods? – Matteo Villa Sep 13 '16 at 08:41
  • @MatteoVilla `pod --version` in your Terminal; Check out their official [guides](https://guides.cocoapods.org/) for more info – tech4242 Sep 13 '16 at 08:48
  • i follow the steps to install the Alamofire via Cocoapods. but when i try to build the project with xcode i have several issues. i've added the screenshot in the question – Matteo Villa Sep 13 '16 at 10:12
  • 1
    Installing CocoaPods is fairly as *rather complicated, especially for beginners* as *native Swift networking* ;-) – vadian Sep 13 '16 at 10:25
  • ok i find out why... i must have swift 3.0 and xcode 8 installed in order to use Alamofire 4.0 – Matteo Villa Sep 13 '16 at 10:29
  • I just installed Alamofire 3.0 instead of 4.0 and it build without errors. I also tried to import Alamofire and it works fine. – Matteo Villa Sep 13 '16 at 10:48
  • @MatteoVilla I am glad I could help and even more glad you figured it out! – tech4242 Sep 13 '16 at 17:59
  • @vadian I guess it depends - if someone is completely new to programming and the concept of frameworks then yes. Instead I just assumed that the OP was new to the iOS world. It's a matter of perspective either way; I loved frameworks from the beginning but I was also a CS student. Nevertheless I agree with you at least to some extent. – tech4242 Sep 13 '16 at 18:03
0

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.

Ganesh Amrule
  • 407
  • 2
  • 12