【问题标题】:Send data from watchKit to iPhone and got callback without launching iOS App将数据从 watchKit 发送到 iPhone 并在不启动 iOS App 的情况下获得回调
【发布时间】:2015-07-24 13:20:40
【问题描述】:

我想在 watchOS 2 中将字典从 WatchKit 发送到 iPhone,并在不启动 iPhone 应用的情况下从 iPhone 获得回复(回调)。

在 watchOS 1 中,这可以通过在 Watch 中点击按钮时调用 openParentApplication 方法来实现:

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

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

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

它进入处理程序并在不启动 iOS 应用程序的情况下打印“Button1”。我不知道这对于 watchOS 2 是否可行。

【问题讨论】:

  • “openParentApplication”在后台打开父应用,实际上并没有启动它。
  • 是的!我知道,“openParentApplication”正在后台打开父应用程序并在手表中发送回调而不启动 iPhone 应用程序,实际上这与我在手表 OS2 中想要的一样

标签: objective-c iphone swift watchkit apple-watch


【解决方案1】:

是的,在 watchOS 2 中是可能的。他们已弃用 openParent 调用,并用新的连接框架取而代之。 watchOS 2 过渡指南中的Documentation 状态:

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

Connectivity Framework Documentation

你应该研究的函数是

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

【讨论】:

  • 这对我现在不起作用。 iPhone 从不接收用户信息(虽然它接收其他事件,如 didReceiveMessage 等。所以这不是委托错误)
猜你喜欢
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多