简体   繁体   中英

watchOS 2: haptic feedback in background

I think I know the answer to this question already, but I wanted to ask just to be thorough.

Consider the Apple Watch built-in Maps app. When you're using turn-by-turn directions, when it's time to turn left or right, the watch plays a custom haptic pattern - even though the screen is off and the app is backgrounded. Another example is while you're doing a workout - if you've set a goal, you'll get a light tap on your wrist when you get 50% there and 100% there, even if you're not looking at the watch at the time (screen off, app backgrounded).

In watchOS 2, is there any way for us 3rd party developers to have an app play a certain haptic pattern when the screen is off and the app is backgrounded? I know the playHaptic: method works while the app is active to let you play several different kinds of haptic patterns, and I know that while the app is inactive, you can have a notification come in - but the notification would only ever play the 'notification' haptic feeling, no choice in that.

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

Here is how i'm playing haptic in background, first of all you need to enable background mod in Capabilities for WatchExtensionand to enable:Workout Processing and Audio,Airplay. Also you need to enable for WatchExtension HealthKit.

#import < HealthKit/HealthKit.h > add 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);
}

And now you can play haptic in background.

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


 }

- (void)timerTrick:(NSTimer *)time {

        [[WKInterfaceDevice currentDevice] playHaptic:WKHapticTypeStart];

}

Don't forghet to stop workout Session after leaving controller:

     [cwHealthStore endWorkoutSession:cwSession];

Just to post an update to my own question a couple years later - in watchOS 3 workout apps were granted background execution, but no haptics (I think).

In watchOS 4, workout apps, audio recording apps and navigation apps have background execution; navigation apps can send haptics in the background. Furthermore, the "frontmost app" (app last used which still appears if wrist is raised within 2 minutes, or 8 if extended frontmost time is enabled) has some privileges for sending haptics at the conclusion of a WatchConnectivity or NSURLSession data transfer, or when a notification comes in. See the docs for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM