简体   繁体   中英

Watch App: [WCSession defaultSession].isReachable is always retrieve 'FALSE' status on background app?

I am developing apple watch application. when app on foreground it is working fine [WCSession defaultSession].isReachable and retrieve ON status. Now my watch application is goes to background mode then problem must be create.

so how to resolve this issues? and retrieve ON status on background mode.

My code is as follows.

- (void)willActivate {

    [super willActivate];

    self.locationManager = [[CLLocationManager alloc] init];
    [self.locationManager setDelegate:self];
    [self.locationManager requestLocation];


    if ([WCSession isSupported]) {
        WCSession *session = [WCSession defaultSession];
        session.delegate = self;
        [session activateSession];
    }

    if ([[WCSession defaultSession] isReachable]) {
        NSLog(@"Session Reachable");
    } else {
        NSLog(@"Session Not Reachable");

    }

    if([WCSession defaultSession].iOSDeviceNeedsUnlockAfterRebootForReachability) {

        WKAlertAction *action = [WKAlertAction actionWithTitle:@"OK"
                                                     style:WKAlertActionStyleDefault
                                                   handler:^{
                                                   }];

        NSString *title = @"My App";
        NSString *message = @"Reachability in the Watch app requires the paired iOS device to have been unlocked at least once after reboot";
        [self presentAlertControllerWithTitle:title message:message preferredStyle:WKAlertControllerStyleAlert actions:@[action]];
    }

    if ([[WCSession defaultSession] isReachable]) {
        NSString *strUserId = [[NSUserDefaults standardUserDefaults]
                           stringForKey:@"user_id"];
        if ([strUserId isEqualToString:@""])
        {
            WKAlertAction *act = [WKAlertAction actionWithTitle:@"OK" style:WKAlertActionStyleCancel handler:^(void){
            NSLog(@"ALERT YES ");
            }];

            NSArray *testing = @[act];

            [self presentAlertControllerWithTitle:@"My App" message:@"You are not login" preferredStyle:WKAlertControllerStyleAlert actions:testing];

        }else{
            [self addTrackingdata];
            [self loadPairList];
        }
    }


    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdateData) name:@"refreshData" object:nil];


    ExtensionDelegate *del = (ExtensionDelegate *)[WKExtension sharedExtension].delegate;
    if (del.strReminderTime == 0 && del.isTimerFlag) {
        [self UpdateData];
        isReminderFlag = NO;
        del.isTimerFlag = NO;
        NSTimer * CheckTimer =  [NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(CheckConnectation) userInfo:nil repeats:YES];
        [CheckTimer fire];
    }



}

- (void)CheckConnectation
{
    WCSession *session = [WCSession defaultSession];
    if([WCSession isSupported]) {
        session.delegate = self;
        [session activateSession];
    }

    if([WCSession defaultSession].isReachable){
        [_lblPairedStatus setText:@"connected"];
    }
    else
    {
        [_lblPairedStatus setText:@"disconnected"];
    }
}

Appreciate if any suggestion or idea.

I just had a similar problem. The solution for me was to check isReachable in the didAppear function instead of willActivate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM