4
我的XCode 7,iPhone6與iOS 9.1,蘋果手錶WatchOS 2.0(現在我更新到2.0.1)WCSession蘋果手錶無法正常工作
我儘量讓觀看和iPhone之間的通信。
在iPhone上我初始化我單身
- (instancetype)init {
self = [super init];
if (self.isConnectivityAvailable) {
session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
return self;
}
- (BOOL)isConnectivityAvailable {
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
return [WCSession isSupported];
} else {
return NO;
}
}
中的AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//custom code
(void)[AppConnectivityHandler instance]; //start App connectivity with Apple Watch
return YES;
}
它就是這樣
- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *,id> *)message replyHandler:(void (^)(NSDictionary<NSString *,id> * _Nonnull))replyHandler {
LOG(@"receive message");
NSString *request = message[kRequestKey];
__block NSDictionary *reply = @{};
dispatch_sync(dispatch_get_main_queue(), ^{
if ([request isEqualToString:kRequestDayInfo]) {
//formirate reply dictionary
}
});
LOG(@"send reply");
replyHandler(reply);
}
在我的收藏都是不錯
我處理消息我開始loa在我的主接口控制器
- (void)willActivate {
[super willActivate];
if ([WatchConnectivityHandler instance].isConnectivityAvailable) {
[[WatchConnectivityHandler instance] loadDayInfoWithCompletion:^(NSError * _Nullable error, WatchDayInfo * _Nullable dayInfo) {
//code
}];
} else {
//error
}
}
我的手錶單身調用函數時d
+ (nonnull instancetype)instance {
static WatchConnectivityHandler *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [WatchConnectivityHandler new];
});
return instance;
}
- (instancetype)init {
self = [super init];
if (self.isConnectivityAvailable) {
session = [WCSession defaultSession];
session.delegate = self;
[session activateSession];
}
return self;
}
- (BOOL)isConnectivityAvailable {
return [WCSession isSupported];
}
- (void)loadDayInfoWithCompletion:(void(^ _Nonnull)(NSError * _Nullable error, WatchDayInfo * _Nullable dayInfo))completion {
[session sendMessage:@{kRequestKey : kRequestDayInfo} replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {
NSLog(@"reply");
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"%@", replyMessage);
//custom code
completion(nil, /*custom object*/);
});
} errorHandler:^(NSError * _Nonnull error) {
NSLog(@"error");
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"%@", error);
completion(error, nil);
});
}];
}
因此,在第一次工作得很好,我也得到答覆。但後來我開始有很多錯誤的像
Error Domain=WCErrorDomain Code=7007 "WatchConnectivity session on paired device is not reachable."
Error Domain=WCErrorDomain Code=7014 "Payload could not be delivered."
我的觀察測試,這是經常的問題得到答覆,形成我的iPhone,它等待很長。但在iPhone上測試時,當它收到來自Watch的消息時,它會很快發送回覆,但我在Watch上看不到該回復。
每當手錶啓動我的應用時,我都需要更新我的信息。什麼問題?也許我使用不正確的功能?
字面上恰恰相反。 – bkwebhero