当应用程序在后台触发任务时更新位置。但无法以后台模式执行任务。我的scheduleBackgroundRefreshWithPreferredDate示例代码如下
[WKExtension.sharedExtension scheduleBackgroundRefreshWithPreferredDate:[NSDate dateWithTimeIntervalSinceNow:60] userInfo:nil scheduledCompletion:^(NSError * _Nullable error) {
if(error == nil) {
NSLog(@"background refresh task re-scheduling successfuly ");
} else{
NSLog(@"Error occurred while re-scheduling background refresh: %@",error.localizedDescription);
}
}];
在安排任务重新安排之后handleBackgroundTasks:
- (void)handleBackgroundTasks:(NSSet<WKRefreshBackgroundTask *> *)backgroundTasks
{
for (WKRefreshBackgroundTask * task in backgroundTasks) {
if ([task isKindOfClass:[WKApplicationRefreshBackgroundTask class]]) {
WKApplicationRefreshBackgroundTask *backgroundTask = (WKApplicationRefreshBackgroundTask*)task;
// location update methods schedule as background task
[self startLocationUpdate];
[backgroundTask setTaskCompleted];
} else if ([task isKindOfClass:[WKSnapshotRefreshBackgroundTask class]]) {
WKSnapshotRefreshBackgroundTask *snapshotTask = (WKSnapshotRefreshBackgroundTask*)task;
[snapshotTask setTaskCompletedWithDefaultStateRestored:YES estimatedSnapshotExpiration:[NSDate distantFuture] userInfo:nil];
} else if ([task isKindOfClass:[WKWatchConnectivityRefreshBackgroundTask class]]) {
WKWatchConnectivityRefreshBackgroundTask *backgroundTask = (WKWatchConnectivityRefreshBackgroundTask*)task;
[backgroundTask setTaskCompleted];
} else if ([task isKindOfClass:[WKURLSessionRefreshBackgroundTask class]]) {
WKURLSessionRefreshBackgroundTask *backgroundTask = (WKURLSessionRefreshBackgroundTask*)task;
[backgroundTask setTaskCompleted];
} else {
[task setTaskCompleted];
}
}
}
后台任务方法如下
-(void)startLocationUpdate {
locationMgr = [[CLLocationManager alloc] init];
[locationMgr setDelegate:self];
locationMgr.desiredAccuracy = kCLLocationAccuracyBest;
locationMgr.distanceFilter = kCLDistanceFilterNone;
// locationMgr.allowsBackgroundLocationUpdates = YES;
[locationMgr requestAlwaysAuthorization];
[locationMgr startUpdatingLocation];
[WKExtension.sharedExtension scheduleBackgroundRefreshWithPreferredDate:[NSDate dateWithTimeIntervalSinceNow:60] userInfo:nil scheduledCompletion:^(NSError * _Nullable error) {
if(error == nil) {
NSLog(@"background refresh task re-scheduling successfuly ");
} else{
NSLog(@"Error occurred while re-scheduling background refresh: %@",error.localizedDescription);
}
}];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations {
NSTimeInterval locationAge = -[[locations lastObject].timestamp timeIntervalSinceNow];
NSLog(@"Location Age : %f",locationAge);
if (locationAge > 5.0) return;
NSLog(@"latitude: %f longitude: %f",[locations lastObject].coordinate.latitude,[locations lastObject].coordinate.longitude);
//NSString *strLocation = [NSString stringWithFormat:@"%f,%f" ,[locations lastObject].coordinate.latitude , [locations lastObject].coordinate.longitude];
NSString *strLocation = @"bgLocation";
NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[strLocation] forKeys:@[@"watchlocation"]];
[[WCSession defaultSession] transferUserInfo:applicationData];
}