谁可以帮助我,代码是......
// iOS客户端
static int i = 0;
- (void) session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary<NSString *,id> *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> * __nonnull))replyHandler{
dispatch_async(dispatch_get_main_queue(), ^{
i++;
NSString *temKey = [message.allKeys objectAtIndex:0];
NSString *temStr = [NSString stringWithFormat:@"%@_%d",temKey,i];
dispatch_async(dispatch_get_main_queue(), ^{
self.statusLabel.text = temStr;
});
if([message objectForKey:@"check"]){
NSString *temStr = [NSString stringWithFormat:@"Rcheck_%d",i];
replyHandler(@{@"check":temStr});
}
});
}
// AppleWatch客户端
static int i = 0;
- (void)willActivate {
[super willActivate];
i++;
if ([WCSession isSupported] ) {
WCSession* session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
[self performSelector:@selector(checkAction) withObject:nil afterDelay:0.1];
}
else{
[self.lblLockState setText:@"WATCH NOT support"];
}
}
- (void) checkAction{
WCSession* session = [WCSession defaultSession];
[session sendMessage:@{@"check":@"I'm Back"} replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyHandler) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *temStr;
if ([replyHandler objectForKey:@"check"]) {
temStr = [replyHandler objectForKey:@"check"] ;//[NSString stringWithFormat:@"CheckSucc_%d",i];
}
else{
temStr = [NSString stringWithFormat:@"CheckSucc??_%d",i];
}
[self.lblLockState setText:temStr];
});
} errorHandler:^(NSError * _Nonnull error) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *temStr = [NSString stringWithFormat:@"CheckFail_%d",i];
[self.lblLockState setText:temStr];
});
}];
}