应用程序在后台

时间:2018-03-30 09:36:35

标签: background ios11 cllocationmanager xcode9

我在stackoverflow处已经阅读了很多问题,我仍然遇到CLLocationManager的问题。我已经在info.plist (NSLocationAlwaysAndWhenInUseUsageDescription,NSLocationWhenInUseUsageDescription,NSLocationAlwaysAndWhenInUseUsageDescription)中添加了密钥。我的应用程序支持ios 9.0到11.x。

更新: - 我在iphone6 ios 11.0.3物理设备上进行测试

我的方法 -  1.使用应用程序权限后开始更新位置。  2.当应用程序进入后台停止位置管理器删除蓝色横幅(羞耻的横幅)  3.取消30秒的定期计时器并再次启动位置管理器。 这次我从未得到委托回调didUpdateLocation

我有一个名为LocationManager的单例类。 以下是LocationManagerAppDelegate

的代码

的LocationManager

- (void)startLocatingUser {
    //Locate User
    _locationMeasurements = [NSMutableArray array];
    self.geocoder = [[CLGeocoder alloc] init];

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.pausesLocationUpdatesAutomatically = NO;
    self.locationManager.activityType = CLActivityTypeAutomotiveNavigation;


    if ([self.locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
        [self.locationManager setAllowsBackgroundLocationUpdates:YES];
    }

    if(IS_OS_8_OR_LATER) {
        if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [self.locationManager requestAlwaysAuthorization];
        }
    }


        if (@available(iOS 11.0, *)) {
            self.locationManager.showsBackgroundLocationIndicator = NO;
        } 

    [self.locationManager startUpdatingLocation];

}

- (void)stopLocatingUser {

    if(self.locationManager) {
        [self.locationManager stopUpdatingLocation];
    }
}

AppDelegateCode

- (void)applicationWillResignActive:(UIApplication *)application {

    _isBackgroundMode = YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

    LocationManager* locationManager = [LocationManager sharedLocationManager];
    [locationManager stopLocatingUser];



    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    self.bgTimer = [NSTimer scheduledTimerWithTimeInterval:30.0
                                                      target:self
                                                    selector:@selector(startTrackingBg)
                                                    userInfo:nil
                                                     repeats:YES];
}

-(void)startTrackingBg {
    dispatch_async(dispatch_get_main_queue(), ^{
        LocationManager* locationManager = [LocationManager sharedLocationManager];

        [locationManager startLocatingUser];
    });

    NSLog(@"App is running in background");
}

一旦我停止并再次启动位置管理器,我就永远不会在后台获得此委托回调。

- (void)locationManager:(CLLocationManager *)manager

     didUpdateLocations:(NSArray *)locations

我只想要的是每当用户将应用程序置于后台时。我想要隐藏羞耻的旗帜然后我需要在后台定期更新位置并将它们发送到服务器。

0 个答案:

没有答案