簡體   English   中英

如何在IOS應用程序和WatchOS應用程序之間建立連接(NativeScript)

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

我一直在閱讀有關watchkit samle應用程序鏈接的信息 -並且我已經將其與我自己的應用程序配合使用。 花費一些時間進行控制器的所有打開/關閉檢查。

好吧-我的問題是,我需要watch應用程序要求HTTP並發出請求-我認為“最佳實踐”方法是在IOS應用程序中實現所有logik,然后將其提供給WatchOS應用程序。 (如果我做錯了,請糾正我)。

但說實話,我對/ platforms / ios /“ watchappname”擴展名/app/bootstrap.js如何與IOS應用進行通信感到困惑。

您將采取什么措施?

注意:如果您遇到類似的問題,請知道我也將此問題發布在他們的GITHUB存儲庫中

我認為您正在尋找的方法是這種方法:

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

}];

希望對您有幫助。

編輯:

該代碼僅適用於watchOS1。如果您已經在為watchOS 2進行開發,請查看Watch Connectivity Framework

使用watchOS 2.0,您需要研究使用Watch Connectivity框架而不是openParentApplication

該框架提供了在后台或兩個應用程序都處於活動狀態時傳輸數據的選項,並替換了WKInterfaceController類的現有openParentApplication:reply:方法。

使用Watch Connectivity Framework,使用WCSession。 您可以看一下本教程this 當我嘗試以編程方式連接手表和iPhone時,這對我很有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM