Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Handoff with Watchkit to bring the iPhone app to the foreground

I have a simple project where the user taps a button on the Apple Watch and some audio plays on the iPhone, this is easy enough to do with the openParentApplication method and having the handleWatchKitExtensionRequest code in AppDelegate. However, while this works in the simulator, it will NOT work on the actual devices if the iPhone app is not already open. I'm trying to find if it's possible to use other methods, that will work even if the iPhone app isn't already open.

I've read on a stackoverflow answer here that it is possible to use Handoff to (partially) bring the phone app to the foreground, using WKInterfaceController updateUserActivity:userInfo:webpageURL: and UIApplicationDelegate application:continueUserActivity:restorationHandler. However, as a new developer I'm struggling to work out how to do this properly without any examples. Can anyone give some example code of how this would work, where both these are used together to run some code on the iphone app?

like image 278
ZhouW Avatar asked Apr 09 '15 00:04

ZhouW


1 Answers

  1. Register your activity type names in your iphone application's plist. Add a row named NSUserActivityTypes and make it an array. Eg:

enter image description here

  1. Include the continueUserActivity: method in your AppDelegate. Eg:

    -(BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray *))restorationHandler
            {
                // Extract the payload
                NSString *type = [userActivity activityType];
                NSDictionary *userInfo = [userActivity userInfo];
    
                // Assume the app delegate has a text field to display the activity information
                NSLog(@"User activity is of type %@, and user info %@", type, userInfo);
    
                restorationHandler(@[self.window.rootViewController]);
    
                return YES;
            }
    
  2. In your watchkit controller's awakeWithContext, add the updateUserActivity method.

    [self updateUserActivity:@"com.co.YourApp.watchkitextension.activity" userInfo:@{@"yo": @"dawg"} webpageURL:nil];
    

You should now see the app icon after opening the selected viewcontroller in your watch app.

like image 200
Sohail Avatar answered Sep 26 '22 05:09

Sohail