Bhavuk Sehgal
Bhavuk Sehgal

Reputation: 121

Send data from watchKit to iPhone and got callback without launching iOS App

I want to send a dictionary from WatchKit to iPhone in watchOS 2 and get a reply(callback) from iPhone without launching iPhone app.

In watchOS 1, this is possible by calling openParentApplication method when button tapped in Watch:

@IBAction func btnTapped() {
        let dictionary = ["Button":0]

        WKInterfaceController.openParentApplication(dictionary, reply: { (replyInfo, error) -> Void in
            print(replyInfo["ReturnButton"])
        })
    }

and in AppDelegate class:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
    int key = (int)[[userInfo objectForKey:@"Button"] integerValue];

    NSMutableDictionary* dict = [[NSMutableDictionary alloc]init];

    switch (key){
    case 0:
        [dict setObject:@"Button1" forKey:@"ReturnButton"];
        break;
    default:
        break;
    }
    reply(dict);
}

Its goes in handler and print "Button1" without launching iOS app.I don't know this is possible or not for watchOS 2.

Upvotes: 1

Views: 481

Answers (1)

lostAtSeaJoshua
lostAtSeaJoshua

Reputation: 1755

Yes it is possible in watchOS 2. They have deprecated the openParent call and replaced it with a new connectivity framework. Documentation in the watchOS 2 transition guide states:

The framework provides options for transferring data in the background or while both apps are active and replaces the existing openParentApplication:reply: method of the WKInterfaceController class.

Connectivity Framework Documentation

The function you should look into is

func transferUserInfo(_ userInfo: [String : AnyObject]) -> WCSessionUserInfoTransfer 

Upvotes: 1

Related Questions