I want to send instant message to iOS app from watch app. Implemented the following code in XCode7 beta 4 version and keeping the application in foreground in both simulators. here is the code I implemented
In watchkit interfaceController
-(void)willActivate { [super willActivate]; if ([WCSession isSupported]) { WCSession *session = [WCSession defaultSession]; session.delegate = self; [session activateSession]; } } -(IBAction)buttonClicked { NSDictionary *applicationDict = [[NSDictionary alloc] initWithObjects:@[@"Hi"] forKeys:@[@"key"]]; if([[WCSession defaultSession] isReachable]) { [[WCSession defaultSession] sendMessage:applicationDict replyHandler:^(NSDictionary *reply) { NSLog(@"%@",reply); } errorHandler:^(NSError *error) { NSLog(@"%@",error); }]; } }
In iOS app class
-(void)viewDidLoad { [super viewDidLoad]; if ([WCSession isSupported]){ WCSession *session = [WCSession defaultSession]; session.delegate = self; [session activateSession]; } } -(void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary *)message replyHandler:(nonnull void (^)(NSDictionary * __nonnull))replyHandler { dispatch_async(dispatch_get_main_queue(), ^{ self.testLbl.text = [message objectForKey:@"key"]; [self.view setNeedsDisplay]; }); }
Do you have to use the sendMessage
APIs? I found them unreliable and unpredictable as well. I ended up using the applicationContext
API's. The watch doesn't have to be reachable, but if it is, it arrives immediately, If not reachable, it gets delivered on app launch. Each time you update the application context, it overwrites the previous version, which might not be what you are looking for.
I found in an iPhone app I am currently working on that I needed to have the WCSession activation code in both the AppDelegate and the current View Controller. ...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([WCSession isSupported]) {
WCSession *session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
...
Like you, that does not marry with my understanding of what is supposed to be required, but it is what got session.reachable
(Swift) to equal true
First you should check if the Watch Connectivity Framework is linked correctly, also check your code. After that try with "Reset content and settings" from both simulators, this worked for me. In case it doesn't work yet, try uninstalling and reinstalling both apps from simulators. If it still doesn't work, try with removing the watch app extension from settings on the Watch app installed on the phone. Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With