So I'm trying to migrate some code out of the deprecated method:
And into a BGAppRefreshTask. Basically what's done here is the app downloads some content and displays notification in the the Notification Center.
When I run the app on the device fire off the task via the debugger like so:
The task fires. But any NSURLSessionTask instances I create fail and I get an error:
finished with error [-997] Error Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service"
So I just made a small sample project here and still getting the error. Here is how it's configured:
Code Block - (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler API_DEPRECATED("Use a BGAppRefreshTask in the BackgroundTasks framework instead", ios(7.0, 13.0), tvos(11.0, 13.0));
And into a BGAppRefreshTask. Basically what's done here is the app downloads some content and displays notification in the the Notification Center.
When I run the app on the device fire off the task via the debugger like so:
Code Block e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.refreshidhere"];
The task fires. But any NSURLSessionTask instances I create fail and I get an error:
finished with error [-997] Error Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service"
So I just made a small sample project here and still getting the error. Here is how it's configured:
Code Block -(void)handleAppRefreshWithTask:(BGAppRefreshTask*)task { // Fetch the latest feed entries from server. [self scheduleAppRefresh]; if (self.runningAppRefreshTask != nil) { NSLog(@"already running app refresh task..."); [task setTaskCompletedWithSuccess:YES]; return; } self.runningAppRefreshTask = task; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.apple.com"]]; NSURLSessionDataTask *dataTask = [self.backgroundURLSessionForTest dataTaskWithRequest:request]; [task setExpirationHandler:^{ NSLog(@"Expiration handler called..."); [dataTask cancel]; }]; [dataTask resume]; } #pragma mark - NSURLSessionDataDelegate -(void)URLSession:(NSURLSession*)session dataTask:(nonnull NSURLSessionDataTask*)dataTask didReceiveData:(nonnull NSData*)data { [self.backgroundDownloadData appendData:data]; } -(void)URLSession:(NSURLSession*)session task:(NSURLSessionDataTask*)task didCompleteWithError:(nullable NSError*)error { NSData *theData = [self.backgroundDownloadData copy]; dispatch_async(dispatch_get_main_queue(), ^{ if (error == nil) { NSLog(@"got %@",theData); [self.runningAppRefreshTask setTaskCompletedWithSuccess:YES]; } else { NSLog(@"error: %@",error); [self.runningAppRefreshTask setTaskCompletedWithSuccess:NO]; } self.runningAppRefreshTask = nil; [self.backgroundDownloadData setData:[[NSData alloc]init]]; }); }