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