WCSession on Apple Watch does not work properly

I have Xcode 7, iPhone6 ​​with iOS 9.1, Apple Watch with WatchOS 2.0 (now I upgrade to version 2.0.1)

I am trying to establish a connection between Watch and iPhone.

On iPhone, I launched my singleton

- (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;
    }
}

in AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //custom code
    (void)[AppConnectivityHandler instance]; //start App connectivity with Apple Watch
    return YES;
}

And everything's good

I am processing a message like this

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

In my hourly ad, I start loading when a function is called in my main interface controller

- (void)willActivate {
    [super willActivate];
    if ([WatchConnectivityHandler instance].isConnectivityAvailable) {
        [[WatchConnectivityHandler instance] loadDayInfoWithCompletion:^(NSError * _Nullable error, WatchDayInfo * _Nullable dayInfo) {
             //code
        }];
    } else {
        //error
    }
}

My singleton watch

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

So it works great for the first time, and I get a response. But then I start a lot of mistakes, such as

Error Domain=WCErrorDomain Code=7007 "WatchConnectivity session on paired device is not reachable." 


Error Domain=WCErrorDomain Code=7014 "Payload could not be delivered." 

I am testing the Watch, and often there is a problem with getting the response form on my iPhone, it takes a long time. But on the iPhone, I test that when he receives a message from Watch, he sends a response very quickly, but I do not see the answer to Watch.

, . ? , ?

+4
1

transeruserinfo sendmessage didreceiveuserinfo doreceiveapplicationcontext.

-2

Source: https://habr.com/ru/post/1614696/