【问题标题】:watchkit , iOS sending data between watch and iphonewatchkit , iOS 在手表和 iphone 之间发送数据
【发布时间】:2014-11-20 01:38:07
【问题描述】:

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

-(void)viewWillAppear:(BOOL)animated

{
 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(sayHello:) name: @"sayHelloNotification" object: nil];

}

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

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

【问题讨论】:

    标签: ios objective-c nsnotificationcenter nsnotifications watchkit


    【解决方案1】:

    如果您想将数据发送到您的父应用,请使用。

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

    在手表应用中调用此方法将触发 AppDelegate 中的回调

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

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

    【讨论】:

    • 有没有办法做相反的事情?就像我了解如何为 ios 应用程序分配任务以获取位置等信息,但是您如何将响应发送回 watchkit 应用程序?目前我正在更新 NSUserDefaults,但 watchkit 应用程序无法知道更新发生了。
    • 我相信你可以只发布一个本地通知,不幸的是,这现在在模拟器中不起作用。
    • 向您的手表发送推送通知应该会唤醒它(相反,它会让您的用户有机会决定通过一瞥来唤醒它)。在我们拿到硬件之前,很难确定这将如何“真正”工作。
    • 糟透了,因为本地通知很容易做到。 =[ 另外,atomicbird.com/blog/sharing-with-app-extensions 似乎在 ios 应用程序和扩展程序之间继续支持 Darwin Notifications 方面存在一些争论。
    • @SeanDunford,“我想知道我们是否能够从 iOS 中“唤醒”watchkit 应用程序? No, it's not possible
    【解决方案2】:

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

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

    【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      很遗憾,NSNotificationCenter 无法在应用之间工作。使用MMWormhole 在 WatchKit 和 iOS 之间传递消息。

      【讨论】:

      • 应用程序在后台时不起作用。我该如何解决?
      • 您可以使用 Kronusdark 建议的解决方案将通知从手表发送到 iOS,并使用 MMWormhole 向后发送通知。 AFAIK Watch Kit 应用程序没有后台模式。
      【解决方案5】:

      你可以这样发送数据..

      - (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
      }
      

      【讨论】:

        【解决方案6】:

        watchOS 2.0 开始,您可以只在两台设备之间发送消息。您可以随时发送消息 Watch->iPhone(如果您的 iPhone 对应设备未运行,则发送消息),如果您的手表对应设备正在显示,则发送 iPhone->Watch。只需检查[WCSession defaultSession].isReachable 以确保您可以发送消息。

        以下是两个平台的代码示例:

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

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

        - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message;
        

        - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler;
        

        【讨论】:

          【解决方案7】:

          如果数据冗余 && 频繁更新数据,最好使用 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{
              }
          }
          

          Demo app

          替代方案 你可以使用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)
             }
            }
          

          Demo app2

          已弃用 openParentApplication:reply: watch OS-2 不支持

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-06-13
            • 1970-01-01
            • 1970-01-01
            • 2015-10-26
            • 2023-03-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多