Hi, I'm trying to test the new BackgroundTasks framework.
* Xcode 11.0 beta 3
* On the simulator, the submit task raises an error
* On my iPhone 7 beta 2, task doesn't be triggered
Here is the steps :
- I checked the "Background Processing" check box in capabilities
- I added com.example.refresh in the Info.plist
<key>BGTaskSchedulerPermittedIdentifiers</key>
<array>
<string>com.example.refresh</string>
</array>
- I put the following code in the application didFinishLaunchingWithOptions :
// Background tasks testing
if (@available(iOS 13.0, *)) {
[[BGTaskScheduler sharedScheduler] registerForTaskWithIdentifier:@"com.example.refresh" usingQueue:nil launchHandler:^(__kindof BGTask * _Nonnull task) {
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.fireDate = [NSDate date];
notification.alertBody = @"YEAH";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
[task setTaskCompletedWithSuccess:YES];
}];
[[BGTaskScheduler sharedScheduler] cancelAllTaskRequests];
BGProcessingTaskRequest *bgTaskRequest = [[BGProcessingTaskRequest alloc] initWithIdentifier:@"com.example.refresh"];
NSDate *d = [NSDate date];
int nbMin = 5;
[bgTaskRequest setEarliestBeginDate:[d dateByAddingTimeInterval:(nbMin*60)]];
[bgTaskRequest setRequiresNetworkConnectivity:NO];
[bgTaskRequest setRequiresExternalPower:NO];
NSError *e;
[[BGTaskScheduler sharedScheduler] submitTaskRequest:bgTaskRequest error:&e];
NSLog(@"ERROR TASK : %@", e);
NSLog(@"Task planified at %@", [d dateByAddingTimeInterval:(nbMin*60)]);
} else {
// Fallback on earlier versions
}
The notification is never triggered.