观看应用:[WCSession defaultSession] .isReachable总是在后台应用上检索'FALSE'状态?

时间:2017-11-24 05:56:43

标签: ios objective-c background watchkit wcsession

我正在开发苹果手表应用程序。当应用程序在前台工作正常[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"];
    }
}

欣赏是否有任何建议或想法。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。我的解决方案是检查isReachable函数中的didAppear而不是willActivate