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?
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With