With WatchConnectivity framework you can check if your paired device is available and the app is running.
- activate App Groups from "target -> Capabilities"
- try this code on the device that needs to know the companion app is running.
objective C:
#import <WatchConnectivity/WatchConnectivity.h>
yourClass : Superclass <WCSessionDelegate>
WCSession* session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
-(void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(NSError *)error
{
if (activationState == WCSessionActivationStateActivated) {
[[WCSession defaultSession] sendMessage:@{@"fromThisDevice":@"hello"} replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
NSLog(@"reply %@", replyMessage[@"fromOtherDevice"]);
} errorHandler:^(NSError * _Nonnull error) {
NSLog(@"error %@", error.userInfo);
}];
}
}
- (void) session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler{
NSLog(@"message %@", message[@"fromOtherDevice"]);
replyHandler(@{@"fromThisDevice":@"hello"});
}
Swift:
import WatchConnectivity
yourClass : Superclass, WCSessionDelegate
let session = WCSession.default()
session.delegate = self
session.activate()
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if activationState == WCSessionActivationState.activated{
WCSession.default().sendMessage(["fromThisDevice" : "hello"], replyHandler: { (reply:[String : Any]) -> Void in
print(reply["fromOtherDevice"] as Any)
}, errorHandler: { (error) -> Void in
print(error)
})
}
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
print(message["fromOtherDevice"] as Any)
replyHandler(["fromThisDevice" : "hello"])
}