Hieu Duc Pham Hieu Duc Pham - 3 months ago 23x
Swift Question

How can I open the parent app on iPhone from my WatchKit app?

I'm trying to open the parent application of my Apple Watch app.

In Xcode Beta 2 we could use this code:


However, in Xcode beta 3 I couldn't found that code any longer. Now I don't know how to open the parent application from the watch app. Please help.


The Objective-C method is:

+ (BOOL)openParentApplication:(NSDictionary *)userInfo
                    reply:(void (^)(NSDictionary *replyInfo,
                                    NSError *error))reply

The Swift method is:

class func openParentApplication(_ userInfo: [NSObject : AnyObject]!,
                    reply reply: (([NSObject : AnyObject]!,
                                    NSError!) -> Void)!) -> Bool

So you need to pass the iPhone application a reply() block in order to have activate it from your WatchKit extension. Here's one way it could be implemented, for instance:

NSString *requestString = [NSString stringWithFormat:@"executeMethodA"]; // This string is arbitrary, just must match here and at the iPhone side of the implementation.
NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[requestString] forKeys:@[@"theRequestString"]];

[WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {
    NSLog(@"\nReply info: %@\nError: %@",replyInfo, error);

Your iPhone application's AppDelegate needs to implement the following method:

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

if ([request isEqualToString:@"executeMethodA"]) {
    // Do whatever you want to do when sent the message. For instance... 
    [self executeMethodABC];

// This is just an example of what you could return. The one requirement is 
// you do have to execute the reply block, even if it is just to 'reply(nil)'.
// All of the objects in the dictionary [must be serializable to a property list file][3].
// If necessary, you can covert other objects to NSData blobs first. 
NSArray * objects = [[NSArray alloc] initWithObjects:myObjectA, myObjectB, myObjectC, nil];
NSArray * keys = [[NSArray alloc] initWithObjects:@"objectAName", @"objectBName", @"objectCName", nil];
NSDictionary * replyContent = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];


The WKInterfaceController method openParentApplication:reply: launches the containing app in the background when the iPhone (or iOS Simulator) is unlocked or locked. Note that statements from Apple indicate that the WatchKit extension was always intended to launch your iPhone application in the background, and it was only an implementation detail of the simulator that it appeared to launch your iPhone application in the foreground in previous betas.

If you want to test your WatchKit app and your iPhone app running at the same time, simply launch the WatchKit app from Xcode under the Schemes menu, and then manually launch your iPhone app in the simulator by clicking on its springboard icon.