I want to send an instant message to an iOS app from a watch app. The following code has been introduced in the version of XCode7 beta 4 and saving the application in the 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 the 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];
});
}
source share