1

Is it possible to have a WatchKit app be a controller for it's parent iOS app?

I want to have buttons on the WatchKit app that cause an action on the iPhone app, but I can't find a way to get that going. Tried sending a notification - no joy.

Tried using WKInterfaceController.openParentApplication but that doesn't work either.

Can someone point me in the right direction?

Thanks.

Ken

macgeezer
  • 484
  • 1
  • 6
  • 15
  • Yes you can. See this http://stackoverflow.com/questions/27190949/apple-watch-watchkit-extension-and-main-application/27410392#27410392 for how to send data to your iOS app. – Stephen Johnson Mar 12 '15 at 20:56
  • From my reading that only opens the app on the phone in background mode. Not what I need – macgeezer Mar 12 '15 at 21:25
  • That is true, but if the user has already opened the app then you can control the live app. I have an app that does just that. When the user has the app open on their iPhone there are actions on the watch app that update the UI on the iOS app. – Stephen Johnson Mar 12 '15 at 21:29
  • how do I see that in the simulator? do you send the action to be performed in the dictionary object? -- Thanks for your help – macgeezer Mar 12 '15 at 21:45
  • After you launch your WatchKit app from Xcode you can just tap on your iPhone app in the iPhone simulator to open it. Yes, I send the data about the action to perform in the dictionary object. – Stephen Johnson Mar 12 '15 at 21:48
  • Stephen, really appreciate your help. But when I try that my app crashes. Could you possibly share your code or a sample similar to the code in your handleWatchKitExtensionsRequest delegate method? Sorry to be dense. – macgeezer Mar 12 '15 at 22:11
  • I don't have my code in front of me right now. I will post it when I have the code. – Stephen Johnson Mar 12 '15 at 22:27
  • Thanks so much for your help. Got what I needed to have working done. – macgeezer Mar 13 '15 at 12:49

2 Answers2

1

See example from the blog of @NatashaTheRobot:

http://natashatherobot.com/watchkit-open-ios-app-from-watch/

KepPM
  • 1,129
  • 7
  • 17
0

Here is what my handleWatchKitExtensionRequest looks like. Check the spelling of this method in your code. You didn't spell it right in the comment and that would cause a unrecognized selector crash.

-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
    NSString* command = [userInfo objectForKey:@"command"];
    NSString* uuid = [userInfo objectForKey:@"uuid"];

    if (command == nil)
    {
        reply(nil);
    }
    else
    {
        if ([command isEqualToString:@"startTrip"])
        {
            ...
            //The uuid of the trip is returned to the watch app
            reply(@{@"uuid": uuid});
        }
        else if ([command isEqualToString:@"stopTrip"])
        {
            ...                
            reply(nil);
        }
        else if ([command isEqualToString:@"pauseTrip"])
        {
            ...
            reply(nil);
        }
        else if ([command isEqualToString:@"resumeTrip"])
        {
            ...                
            reply(nil);
        }
    }
}
Stephen Johnson
  • 5,293
  • 1
  • 23
  • 37