watchkit , iOS sending data between watch and ipho

2019-04-17 08:56发布

I want to create one button in watch and while tapping on watch start one process to my ios app. How can I send the data between 2 devices

-(void)viewWillAppear:(BOOL)animated

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

}

plus

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

in my button watch but it doesn't work

7条回答
The star\"
2楼-- · 2019-04-17 09:27

You can send data like this..

- (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
}
查看更多
一纸荒年 Trace。
3楼-- · 2019-04-17 09:28

AFAIK, you can not share data directly, like send some datas between them.
What you can do is write data to same file.

See this blog post:
http://www.atomicbird.com/blog/sharing-with-app-extensions

查看更多
走好不送
4楼-- · 2019-04-17 09:31

Better to use updateApplicationContext() to send data from Watch to iPhone if data is redundant && to update data frequently:-

iPhone to receive data

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

Watch to send data

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

Demo app

Alternative You can use sendMessage()

iPhone to send data

 @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)
            }

Watch to receive data

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

Demo app2

Deprecated openParentApplication:reply: Not supported by watch OS-2

查看更多
可以哭但决不认输i
5楼-- · 2019-04-17 09:35

Code of watch in WatchKit executes directly on the iPhone. See Apple documentation.!

At runtime, you share data between processes by reading and writing files in the shared container directory. To access the container, use the containerURLForSecurityApplicationGroupIdentifier: method of NSFileManager to retrieve the base URL for the directory. Use the provided URL to enumerate the directory contents or create new URLs for files in the directory.

查看更多
我想做一个坏孩纸
6楼-- · 2019-04-17 09:37

If you want to send data to your parent app, use.

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

Calling this method in your watch app will fire the callback in your AppDelegate

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

You will have to define your data in the userInfo dictionary thats sent.

查看更多
三岁会撩人
7楼-- · 2019-04-17 09:40

Unfortunately, NSNotificationCenter not working between apps. Use MMWormhole to pass messages between WatchKit and iOS.

查看更多
登录 后发表回答