我想我已经知道这个问题的答案了,但我想问的只是为了彻底。
以Apple Watch内置地图应用程序为例。当你使用逐个转弯的方向时,当需要左转或右转时,手表会播放自定义的触觉模式-即使屏幕关闭,应用程序处于背景状态。另一个例子是当你在锻炼的时候--如果你设定了一个目标,当你达到50%和100%的时候,你的手腕会受到轻拍,即使你当时没有看手表(关闭屏幕,应用程序背景)。
在watchOS 2中,我们第三方开发人员有没有办法让应用程序在关闭屏幕和应用程序背景的情况下播放特定的触觉模式?我知道当应用程序处于活动状态时,playHaptic:方法可以让你玩几种不同的触觉模式,我也知道当应用程序处于非活动状态时,你可以得到一个通知--但通知只会播放“通知”触觉,没有选择。
发布于 2015-08-03 18:20:53
您只能在应用程序处于活动状态时运行自定义代码。所以恐怕你不能这么做。
发布于 2017-04-19 15:46:20
这是我是如何在后台玩触觉的,首先你需要在WatchExtensionand中启用后台模式的功能来启用:锻炼处理和音频,播放。此外,您还需要启用WatchExtension HealthKit。
#导入< 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];发布于 2017-06-08 09:20:29
只是为了更新几年后我自己的问题-在watchOS 3中,锻炼应用程序被授予后台执行权限,但没有触觉(我想)。
在watchOS 4中,锻炼应用程序、录音应用程序和导航应用程序都有后台执行;导航应用程序可以在后台发送触觉。此外,“最前面的应用程序”(最后使用的应用程序,如果手腕在2分钟内抬起,或如果启用了延长的最前面时间,则仍会出现8分钟)具有一些特权,用于在WatchConnectivity或NSURLSession数据传输结束时或在收到通知时发送触觉。有关详细信息,请参阅文档。
https://stackoverflow.com/questions/31707974
复制相似问题