15

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

考虑一下 Apple Watch 内置的地图应用程序。当您使用转向指示时,当需要左转或右转时,手表会播放自定义触觉模式 - 即使屏幕关闭且应用程序处于后台。另一个例子是当你在锻炼时——如果你设定了一个目标,当你达到 50% 和 100% 时,即使你没有看手表,你也会在手腕上轻拍一下当时(屏幕关闭,应用程序后台)。

在 watchOS 2 中,我们第 3 方开发人员有什么办法可以让应用在屏幕关闭且应用处于后台时播放特定的触觉模式?我知道该playHaptic:方法在应用程序处于活动状态时有效,可让您播放几种不同类型的触觉模式,而且我知道当应用程序处于非活动状态时,您可以收到通知 - 但通知只会播放“通知”触觉的感觉,别无选择。

4

3 回答 3

2

You can only run custom code when you app is active. So I´m afraid you can't do this.

于 2015-08-03T10:20:52.633 回答
1

这是我在后台播放触觉的方式,首先你需要在 WatchExtension 的功能中启用后台模块并启用:锻炼处理和音频,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];
于 2017-04-19T07:46:19.860 回答
1

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

在 watchOS 4 中,锻炼应用、录音应用和导航应用都有后台执行;导航应用程序可以在后台发送触觉。此外,“最前面的应用程序”(如果手腕在 2 分钟内抬起,则最后使用的应用程序仍然出现,如果启用了延长最前面的时间,则为 8)在 WatchConnectivity 或 NSURLSession 数据传输结束时或当收到通知。有关详细信息,请参阅文档。

于 2017-06-08T01:20:28.830 回答