gpt4 book ai didi

ios - 在 apple iWatch 中,后台任务未被调用或按计划时间触发

转载 作者:可可西里 更新时间:2023-11-01 03:48:27 25 4
gpt4 key购买 nike

当应用程序在后台触发任务时更新位置。但无法以后台模式执行任务。我的 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];

}

最佳答案

后台执行在 watchOS3 上非常困难。有很多限制,即使您成功安排了后台刷新任务,也不能保证 watchOS 会启动它。

根据我深入研究 WWDC session 和文档后的经验:

  1. 如果应用程序不在 Dock 中或当前表盘上没有事件的复杂功能,watchOS3 将不会为您的应用程序提供任何后台执行时间
  2. 对于 Dock 中的应用,后台刷新任务的数量限制为每小时 1 次左右,对于具有事件并发症的应用,每小时限制为 2 次
  3. 后台执行的时间也是有限制的,如果app超过这个时间,就会被watchOS daemon终止
  4. 调用 setTaskCompleted 后,应用会进入挂起状态,因此不应执行代码中的异步 locationManager:didUpdateLocations: 方法

关于ios - 在 apple iWatch 中,后台任务未被调用或按计划时间触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44649768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com