如何在IOS应用程序和WatchOS应用程序(NativeScript)之间建立连接

时间:2015-07-22 16:41:15

标签: ios telerik watchkit nativescript

我一直在阅读关于watchkit samle应用程序link的信息 - 我已经让它与我自己的应用程序一起工作了。花了一些时间对控制器进行所有开/关检查。

嗯 - 我的问题是,我需要手表应用程序要求HTTP并发出请求 - 我认为“最佳实践”方式是在IOS应用程序中实现所有logik,然后将其提供给WatchOS应用程序。 (如果我错了,请纠正我。)

但老实说,我对我的/ platforms / ios /“watchappname”扩展名/ app / bootstrap.js与我的IOS应用程序进行通信的方式有点困惑。

你的做法是什么?

注意:如果您遇到类似问题,请知道我也在GITHUB repo上发布了此问题

3 个答案:

答案 0 :(得分:0)

我认为你正在寻找的方法就是这个:

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

在iPhone应用程序的AppDelegate.m文件中,您应该添加此方法。在方法中你应该使用

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

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];
} );

总而言之,iPhone应用程序上的方法应该是这样的:

//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];
} );
}

在手表方面,您应该使用以下代码:

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);

}];

希望,这对你有所帮助。

修改

此代码仅适用于watchOS 1.如果您已经为watchOS 2开发,请查看Watch Connectivity Framework

答案 1 :(得分:0)

使用watchOS 2.0,您需要使用Watch Connectivity framework代替openParentApplication 根据文档:

  

该框架提供了在后台传输数据的选项   或两个应用程序都处于活动状态并替换现有应用程序   openParentApplication:reply:WKInterfaceController的方法   类。

答案 2 :(得分:0)

使用Watch Connectivity Framework,使用WCSession。您可以查看此Tutorialthis。当我试图以编程方式将我的手表和iPhone连接起来时,它帮助了我很多次