watchOS 2:背景中的触觉反馈

我想我已经知道这个问题的答案了,但是我想问的只是彻底。

考虑Apple Watch内置的地图应用程序。 当您使用转弯转向的方向时,即使屏幕closures,应用程序显示为背景时,手表也可以左转或右转,手表会自定义触控模式。 另一个例子是当你在做锻炼的时候 – 如果你设定了一个目标,那么当你在那里得到50%和100%的时候,即使你没有看着手表,你的手腕也会轻轻一击在当时(屏幕closures,应用程序背景)。

在watchOS 2中,有什么办法可以让我们的第三方开发者在屏幕closures和应用程序背景时使应用程序发挥一定的触觉模式? 我知道playHaptic:方法在应用程序处于活动状态时工作,可让您播放几种不同types的触觉模式,而且我知道,虽然应用程序处于非活动状态,但您可以playHaptic:通知 – 但通知只会播放“通知“触觉,没有select。

您只能在应用处于活动状态时运行自定义代码。 所以恐怕你不能这样做。

这里是我如何在后台玩触觉,首先你需要在WatchExtensionfunction中启用背景模块,并启用:锻炼处理和audio,Airplay。 您还需要启用WatchExtension HealthKit。

#import <HealthKit / HealthKit.h>添加HKWorkoutSessionDelegate

 -(void)awakeWithContext:(id)context{ [super awakeWithContext:context]; HKHealthStore *cwHealthStore = [[HKHealthStore alloc] init]; cwConfiguration = [[HKWorkoutConfiguration alloc] init]; cwConfiguration.activityType = HKWorkoutActivityTypeOther; NSError *error; HKWorkoutSession *cwSession = [[HKWorkoutSession alloc] initWithConfiguration:cwConfiguration error:&error]; [cwSession setDelegate:self]; if (!error) { [cwHealthStore startWorkoutSession:cwSession]; } [self test]; } #pragma mark WorkoutSession Delegates - (void)workoutSession:(HKWorkoutSession *)workoutSession didChangeToState:(HKWorkoutSessionState)toState fromState:(HKWorkoutSessionState)fromState date:(NSDate *)date{ NSLog(@"------>%ld", (long)toState); } - (void)workoutSession:(HKWorkoutSession *)workoutSession didFailWithError:(NSError *)error{ NSLog(@"%@", error); } 

现在你可以在后台玩触觉。

  -(void)test{ NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerTrick:) userInfo:nil repeats:true]; } - (void)timerTrick:(NSTimer *)time { [[WKInterfaceDevice currentDevice] playHaptic:WKHapticTypeStart]; } 

不要忍心停止锻炼会后离开控制器会话:

  [cwHealthStore endWorkoutSession:cwSession]; 

只是几年后发布一个更新到我自己的问题 – 在watchOS 3锻炼应用程序被授予后台执行,但没有触觉(我认为)。

在watchOS 4中,锻炼应用程序,录音应用程序和导航应用程序具有后台执行; 导航应用程序可以在后台发送触觉。 此外,“最前面的应用程序”(最后使用的应用程序,如果在2分钟内提起手腕仍然出现,或者如果启用了扩展最前面的时间,则为8)具有在WatchConnectivity或NSURLSession数据传输结束时发送触觉的某些权限,一个通知来了。详情请参阅文档。