简体   繁体   中英

How to make a connection between IOS app and WatchOS app (NativeScript)

I have been reading information on the watchkit samle app link - and I've got it to work with my own app. Took a bit of time with all the on/off checking of the controllers.

Well - My issue is, that I need the watch app to require HTTP and make requests - I think the "best practice" way is to implement all the logik in the IOS app and then serve it to the WatchOS app. (Correct me if am wrong).

But honestly I'm a bit confused on how my /platforms/ios/"watchappname" extension/app/bootstrap.js is suppose to communicate with my IOS app.

What would your approach be on doing this?

Note: If you are having a similar issue, know that i also posted this on their GITHUB repo

I think the method you are looking for is this one:

-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply

In the AppDelegate.m file on your iPhone app you should add this method. Within the method you should use

__block UIBackgroundTaskIdentifier watchKitHandler;
watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
                                                               expirationHandler:^{
                                                                   watchKitHandler = UIBackgroundTaskInvalid;
                                                               }];

and

dispatch_after( dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 10), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
    [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
} );

So all in all, the method on your iPhone app should somehow look like this:

//handle watch app request

-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{



//Make it a background task

__block UIBackgroundTaskIdentifier watchKitHandler;
watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask"
                                                               expirationHandler:^{
                                                                   watchKitHandler = UIBackgroundTaskInvalid;
                                                               }];

NSString* command = [userInfo objectForKey:@"command"];

if ([command isEqualToString:@"request"]) {

    //do some action here

// use the reply dictionary if necessary

NSDictionary *responseObject = @{@"info": @"some Info"};

reply(responseObject);    

} else if ([command isEqualToString:@"request2"]) {

    // do some other action here

}

//finish background task

dispatch_after( dispatch_time( DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 10), dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
    [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler];
} );
}

On the watch side, you should use the following code:

NSDictionary *request = @{ @"command": @"request", @"info": @"some additional things here for example"};
[WKInterfaceController openParentApplication:request reply:^(NSDictionary *replyInfo, NSError *error ) {

//do something with the reply dictionary here

    NSLog(@"%@", replyInfo);

}];

Hope, that helps you.

EDIT:

This code only works with watchOS 1. If you are already developing for watchOS 2, have a look at the Watch Connectivity Framework .

With watchOS 2.0 you need to look at using Watch Connectivity framework instead of openParentApplication Per the docs:

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.

Use Watch Connectivity Framework, use WCSession. You could take a look at this Tutorial and this . It helped me a lot when I tried to conect my watch and iPhone programmatically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM