NSURLSession Example

Hi all,

I am trying to follow an example on how to Parse information from a web server. The code seems to work, but it doesn't display the information. I suspect it is the below line, beings that their is a warning on it and it has been deprecated. How would I change the below to make it work? Thanks.


NSData* data = [NSURLConnection sendSynchronousRequest:requestForWeatherData returningResponse:&response error:&error];

Replies

First, if you are going to use an NSURLConnection rather than an NSURLSession then use an asynchronous (not anything that is synchronous!):

    NSURL *url= [[NSURL alloc] initWithString:website];
    NSURLRequest *req=[[NSURLRequest alloc] initWithURL:url];
    NSURLConnection *con=[[NSURLConnection alloc] initWithRequest:req delegate:self];


This is deprecated in iOS9 but it still works.


If you want to convert to the new NSURLSession then you need to do something like:


    NSURL *storeURL = [[NSURL alloc] initWithString: webSiteString];
    NSURLSessionConfiguration *ephemeralConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    theConnection=[[NSURLSession sessionWithConfiguration:ephemeralConfigObject delegate:self delegateQueue:nil] dataTaskWithURL:storeURL];
    [theConnection resume];