监视应用程序: [WCSession defaultSession].isReachable 总是在后台应用程序上检索"FALSE"状态?



我正在开发Apple Watch应用程序。在前景上的应用程序时,它可以正常工作[WCSession defaultSession].isReachable并检索ON状态。现在我的手表应用程序转到了背景模式,然后必须创建问题。

那么如何解决这个问题?并在背景模式下检索ON状态。

我的代码如下。

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

感谢是否有任何建议或想法。

我只是有类似的问题。我的解决方案是在didAppear功能而不是willActivate中检查isReachable

最新更新