1

It seems that every time I want to edit and update my app, Apple has changed something that blocks me from 'let me just edit that quickly and upload my new version'. They now have disabled the possibility to use NSURLConnection for the Watch. I have used this before for the Watch but it seems Apple now wants me to use NSURLSession and I just can't seem to figure out how it works. Here is my code, can someone please take a look and let me know how to adjust it so it will work with NSURLSession.

- (void)someFunction:(NSString *)startTime {
    // Download the json file
    NSUserDefaults *prefs = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.domain.something"];
    NSURL *jsonFileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://aurlwithparameter%@&another%@", [prefs stringForKey:@"user_id"], startTime]];
    _starttedTime = startTime;
    // Create the request
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:jsonFileUrl];

    // Create the NSURLConnection
    [NSURLConnection connectionWithRequest:urlRequest delegate:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [self.delegate connectionTimedOutMakingTempBlock:true];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // Initialize the data object
    _registeredUser = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // Append the newly downloaded data
    [_registeredUser appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    // Create an array to store the locations

    // Parse the JSON that came in
    NSUserDefaults *prefs = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.domain.something"];

    [prefs setObject:_starttedTime forKey:@"temp_start_time"];

    // Ready to notify delegate that data is ready and pass back items
    if (self.delegate)
    {
        [self.delegate tempDidStart];
    }
}

}

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97

2 Answers2

1

You can use the following snippet. The completionHandler block replaces the delegate methods that you would use with NSURLConnection (e.g. -connectionDidFinishLoading: etc).

// Create the request
NSURLRequest *urlRequest = ...;

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

     // Handle success / error response here.

}];
Vinny Coyne
  • 2,365
  • 15
  • 24
1
 NSURLSession *session = [NSURLSession sharedSession];
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                                completionHandler:
                                      ^(NSData *data, NSURLResponse *response, NSError *connectionError) {
                                          // ...
                                          NSLog(@"coming inside session side");
                                          }];

This is best link you can refer this

NSURLSESSION LINK

Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47