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