Duck
Duck

Reputation: 35933

NSURLSession not working on tvOS?

I have this method to get the contents of an URL on TV:

-(void)placeGetRequestForDeveloperID:(NSString *)developerID andRunOnCompletion:(void (^)(NSData *data, NSURLResponse *response, NSError *error))ourBlock {

  NSString *urlString = [NSString stringWithFormat:@"https://itunes.apple.com/lookup?id=%@&entity=software", developerID];

  NSURL *url = [NSURL URLWithString:urlString];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];

  NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

  [session dataTaskWithRequest:request completionHandler:ourBlock];

}

The completion block is never called.

NSURLSession is allowed on tvOS. Is there something I have to set on the project, like entitlements or something on the Info.plist to make this work? Is this code OK?

Thanks!

Upvotes: 2

Views: 294

Answers (2)

Yohan
Yohan

Reputation: 1108

You have to resume the task to start the API call.

You can try the API call as like below too.

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
      NSURLSession *session = [NSURLSession sessionWithConfiguration:config];



  NSURLSessionDataTask *apiCall = [session dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

             NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

}];

[apiCall resume];

Upvotes: 0

Rafał Sroka
Rafał Sroka

Reputation: 40030

By default, when you create the task, it's suspended. You have to start the task by calling resume.

[[session dataTaskWithRequest:request completionHandler:ourBlock] resume]

Upvotes: 3

Related Questions