5
当应用程序在后台触发任务时更新位置。但无法执行背景模式的任务。与scheduleBackgroundRefreshWithPreferredDate我的示例代码如下在苹果iWatch后台任务没有被调用或触发与计划时间
[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];
}
我想发送当前位置(更新监视位置)到iOS应用,即使在后台模式 – Ashish
手表应用使用WatchConnectivity将手表的位置信息发送到手机是没有意义的。如果您想使用WatchConnectivity,应用程序需要至少在手机的后台运行,所以只需在手机上获取位置数据,而不是将手表放在手表上并将其发送给电话。 –
如果我在iPhone上看到的位置比计算iPhone和iWatch之间的距离并触发通知基于它们之间的距离 – Ashish