簡體   English   中英

位置背景模式在iOS上不起作用

[英]Location Background Mode is not working on iOS

我正在嘗試在應用程序中啟用后台定位模式。 我在我的plist文件中啟用了“位置更新”后台模式。 該應用程序包含一個計時器,該計時器每15秒更新一次。

當應用導航到后台時,我正在執行以下操作

- (void)applicationDidEnterBackground:(UIApplication *)application {
UIApplication*    app = [UIApplication sharedApplication];
self.bgTaskID = [app beginBackgroundTaskWithExpirationHandler:^{
        NSLog(@"background task %lu expired", (unsigned long)self.bgTaskID);
        [app endBackgroundTask:self.bgTaskID];
        self.bgTaskID = UIBackgroundTaskInvalid;
}];


 [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(initializeLocationManager) userInfo:nil repeats:NO];

            if(self.timerLocationBackground)
            {
                [self.timerLocationBackground invalidate];
                self.timerLocationBackground = nil;
            }
            self.timerLocationBackground = [NSTimer scheduledTimerWithTimeInterval:15
                                                                            target:self
                                                                          selector:@selector(initializeLocationManager)
                                                                          userInfo:nil
                                                                           repeats:YES];}`

initializeLocationManager在下面

  -(void)initializeLocationManager
{
    if(!self.locationManager)
        self.locationManager = [[CLLocationManager alloc] init];
    else
        [self.locationManager stopUpdatingLocation];

    if ((![CLLocationManager locationServicesEnabled])
        || ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusRestricted)
        || ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied))
    {
        //user has disabled his location
    }
    else
    {
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        self.locationManager.distanceFilter = kCLDistanceFilterNone;
        [self.locationManager setAllowsBackgroundLocationUpdates:YES];
        [self.locationManager startUpdatingLocation];
    }
}

例如,當我在10分鍾后導航回到該應用程序時,我的計時器將在3分鍾停止,這是該應用程序被暫停的時間。

當應用回到前台時,我的代碼如下:

- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

//
//Remove the baground task
//
if (self.bgTaskID != UIBackgroundTaskInvalid) {
    [[UIApplication sharedApplication] endBackgroundTask:self.bgTaskID];
    self.bgTaskID = UIBackgroundTaskInvalid;
}
[self.locationManager stopUpdatingLocation];

有什么幫助嗎?

應該是這樣的。 我已經改了動身,所以我的Objective-C可能並不完美。

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

    [self keepAwakeInBackground ] ; 

    /// rest of your function to set up timers
}

- (void) keepAwakeInBackground {

    //Remove the old background task, if there was one
    if (self.bgTaskID != UIBackgroundTaskInvalid) {
        [[UIApplication sharedApplication] endBackgroundTask:self.bgTaskID];
        self.bgTaskID = UIBackgroundTaskInvalid;
    }
    /*
     *  You probably want something here to decide that the app should really be suspended here
     *  if ( ) return
     */

    // Set up new background task 
    UIApplication*    app = [UIApplication sharedApplication];
    self.bgTaskID = [app beginBackgroundTaskWithExpirationHandler:^{
        [self keepAwakeInBackground] 
    }];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM