0

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

-(void)viewWillAppear:(BOOL)animated

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

}

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

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

4

7 回答 7

10

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

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

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

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

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

于 2014-12-17T16:07:35.213 回答
3

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

请参阅此博客文章: http:
//www.atomicbird.com/blog/sharing-with-app-extensions

于 2014-11-22T11:38:01.250 回答
1

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

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

于 2014-11-20T12:39:11.540 回答
1

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

于 2015-03-16T10:54:49.330 回答
0

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

- (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
}
于 2015-04-08T14:19:59.837 回答
0

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;
于 2016-04-19T07:52:02.770 回答
0

如果数据是冗余的,最好使用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)
   }
  }

演示应用程序2

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

于 2016-11-29T07:42:18.403 回答