简体   繁体   English

将数据从watchKit发送到iPhone并获得回调,而无需启动iOS App

[英]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. 我想在不启动iPhone应用程序的情况下将表从WatchKit发送到watchOS 2中的iPhone,并从iPhone得到答复(回叫)。

In watchOS 1, this is possible by calling openParentApplication method when button tapped in Watch: 在watchOS 1中,可以通过在Watch中点击按钮时调用openParentApplication方法来实现:

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

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

and in AppDelegate class: 并在AppDelegate类中:

- (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. 它进入处理程序并打印“ Button1”,而不启动iOS应用。我不知道这对于watchOS 2是否可行。

Yes it is possible in watchOS 2. They have deprecated the openParent call and replaced it with a new connectivity framework. 是的,在watchOS 2中是可能的。他们已弃用openParent调用,并将其替换为新的连接框架。 Documentation in the watchOS 2 transition guide states: watchOS 2过渡指南中的文档指出:

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. 该框架提供了用于在后台或两个应用程序都处于活动状态时传输数据的选项,并替换了WKInterfaceController类的现有openParentApplication:reply:方法。

Connectivity Framework Documentation 连接框架文档

The function you should look into is 您应该研究的功能是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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