watchkit,iOS在手表和iphone之间发送数据

我想在手表中创建一个按钮,同时点击手表启动一个进程到我的ios应用程序。 如何在2台设备之间发送数据

-(void)viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(sayHello:) name: @"sayHelloNotification" object: nil]; } 

  [[NSNotificationCenter defaultCenter] postNotificationName: @"sayHelloNotification" object: nil]; 

在我的按钮手表,但它不起作用

AFAIK,你不能直接共享数据,比如在它们之间发送一些数据。
您可以做的是将数据写入同一文件。

看到这篇博文:
http://www.atomicbird.com/blog/sharing-with-app-extensions

如果要将数据发送到父应用程序,请使用。

 [WKInterfaceController openParentApplication:userInfo reply:^(NSDictionary *replyInfo, NSError *error){}]; 

在您的监视应用程序中调用此方法将触发AppDelegate中的回调

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

您必须在发送的userInfo字典中定义数据。

WatchKit中的手表代码直接在iPhone上执行。 请参阅Apple文档。

在运行时,您可以通过读取和写入共享容器目录中的文件来在进程之间共享数据。 要访问容器,请使用NSFileManager的containerURLForSecurityApplicationGroupIdentifier:方法检索目录的基本URL。 使用提供的URL枚举目录内容或为目录中的文件创建新URL。

不幸的是,NSNotificationCenter无法在应用程序之间工作。 使用MMWormhole在WatchKit和iOS之间传递消息。

你可以发送这样的数据..

 - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply { NSLog(@"Username %@",[userInfo objectForKey:@"username"]); NSLog(@"Password %@",[userInfo objectForKey:@"password"]); } - (IBAction)passUserInfo:(id)sender { NSDictionary *userInfo = @{@"username":@"hi",@"password":@"123456"}; [WKInterfaceController openParentApplication:userInfo reply:^(NSDictionary *replyInfo, NSError *error){ }];// userinfo must be non-nil } 

watchOS 2.0开始,您可以在两个设备之间发送消息。 您可以随时发送消息Watch-> iPhone (如果您的iPhone没有运行则发生事件)和iPhone->观看您的手表对手是否正在呈现。 只需检查[WCSession defaultSession].isReachable即可确保您可以发送消息。

对于这两个平台的代码示例:

 @import WatchConnectivity; ... if ([WCSession defaultSession].isReachable) { [[WCSession defaultSession] sendMessage:@{ @"Key" : @"Value" } replyHandler:^(NSDictionary * _Nonnull replyMessage) { NSLog(@"Sent update is OK"); } errorHandler:^(NSError * _Nonnull error) { NSLog(@"Sent update with error %@", error); }]; } 

要对此消息做出反应,您应该在对应的WCSessionDelegate实现:

 - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary *)message; 

要么

 - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary *)message replyHandler:(void(^)(NSDictionary *replyMessage))replyHandler; 

如果数据冗余 && 经常更新数据,最好使用updateApplicationContext ()将数据从Watch发送到iPhone: –

iPhone接收数据

 func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) { let type = applicationContext["watchType"]! DispatchQueue.main.async { self.updateLabel.text = " Type: \(type)" UserDefaults.standard.set(type, forKey: "savedState") //setObject } } 

观察发送数据

 func updateContext(value:String) { let dictionary = [ "watchType" : value ] do { try session?.updateApplicationContext(dictionary) } catch{ } } 

演示应用

替代方案您可以使用sendMessage ()

iPhone发送数据

  @IBAction func sendTextToWatch(_ sender: Any) { print("send text to watch amount") if let textName = textWord.text { session?.sendMessage(["textIndex" : textName as String], replyHandler: nil, errorHandler: nil) } 

注意接收数据

 func session(_ session: WCSession, didReceiveMessage message: [String : Any]) { let message:String = message["textIndex"] as! String textLabel.setText(message) print(message) } } 

演示app2

推荐 使用 openParentApplication:reply: watch OS-2不支持