0

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

bowlturner
  • 1,968
  • 4
  • 23
  • 35
user2296278
  • 528
  • 1
  • 4
  • 17

7 Answers7

10

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.

Weston
  • 1,481
  • 1
  • 11
  • 31
  • 3
    Is there a way to do the reverse? Like I understand how to task the ios app to get things like location but how do you then send the response back to the watchkit app? Currently I'm updating NSUserDefaults but have no way for the watchkit app to know that the update happened. – Sean Dunford Jan 26 '15 at 03:00
  • 1
    I believe you can just post a Local Notification, unfortunately, this doesn't work in simulator right now. – Weston Jan 26 '15 at 19:01
  • Thanks @Kronusdark I worded my question weirdly. Obviously if your iOs application gets the openParent: call then you can just fire the reply() block and it will respond to the iWatch. You answered the question that I meant to ask though. It seems like you can also update NSUserDefaults and have the iWatch monitor the NSUserDefaults with Notifcation center. The only other alternative is to use KVO but KVO seems to behave quirky in swift. Might have to wait until it's further supported in Swift or just do it in Obj-C. I wonder if we will be able to "wake" the watckit app from the iOS? – Sean Dunford Jan 28 '15 at 06:25
  • 1
    Sending push notifications to your watch should wake it up (rather it will give your user a chance to decide to wake it up via glance). until we get hardware in our hands it will be hard to determine how this will "really" work. – Weston Jan 28 '15 at 20:05
  • 1
    Sucks because Local Notifications are so easy to do. =[ Also, http://www.atomicbird.com/blog/sharing-with-app-extensions There seems to be some debate on the continued support of Darwin Notifications between ios Apps and extensions. – Sean Dunford Jan 29 '15 at 03:08
  • 1
    @SeanDunford, "I wonder if we will be able to "wake" the watckit app from the iOS?" [No, it's not possible](http://www.raywenderlich.com/94672/watchkit-faq) – Iulian Onofrei May 05 '15 at 14:58
3

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

WebOrCode
  • 6,852
  • 9
  • 43
  • 70
1

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.

kojiba
  • 11
  • 2
1

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

kelin
  • 11,323
  • 6
  • 67
  • 104
  • It doesn't work when application is on the background. How can I fix it? – Dmitry Apr 05 '15 at 08:15
  • 1
    You can use the solution, suggested by Kronusdark to send notifications from watch to iOS and use MMWormhole to send notifications backwards. AFAIK ther is no background mode on Watch Kit apps. – kelin Apr 06 '15 at 13:28
0

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
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

Starting from watchOS 2.0 you could just send a message between two devices. You could send a message Watch->iPhone at any time (event if your iPhone counterpart isn't running) and iPhone->Watch if your watch counterpart is presenting. Just check [WCSession defaultSession].isReachable to be sure you could send a message.

For both platform here's code example:

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

To react on this message you should implement in counterpart WCSessionDelegate:

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

or

- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler;
Anton Gaenko
  • 8,929
  • 6
  • 44
  • 39
0

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

Shrawan
  • 7,128
  • 4
  • 29
  • 40