延迟的位置更新不可用

时间:2013-12-09 05:33:01

标签: ios objective-c cllocationmanager

我正在尝试在前台获取用户位置&背景。我得到一个locaion更新后,我必须调用api。要在后台工作,我想使用延迟方法。我按照Apple WWDC中描述的相同过程进行操作。我在iPhone 5(iOS 7)上查看应用程序。当我在前台时它工作正常但在我将应用程序发送到后台后没有给我更新。下面是我用来在后台获取位置的代码。

#import "AppDelegate.h"
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{
    self.locationArray = [[NSMutableArray alloc] init];

    self.locationErrorArray = [[NSMutableArray alloc] init];
  self.manager_loc = [[CLLocationManager alloc] init];

    self.manager_loc.activityType = CLActivityTypeFitness;

    self.manager_loc.delegate = self;

    [self.manager_loc setDesiredAccuracy:kCLLocationAccuracyBest];

    [self.manager_loc startUpdatingLocation];
return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application

{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (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.

}



- (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.

}

- (void)applicationDidBecomeActive:(UIApplication *)application

{

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application

{

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}


#pragma mark - Location Manager Delgate
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{
    NSLog(@"update failed");
}


-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

    [self.locationArray addObject:locations];

    NSLog(@"udate locations  %f  %f", manager.location.coordinate.latitude, manager.location.coordinate.longitude);



    if (!self.deferredStatus)

    {

        self.deferredStatus = YES;

        [self.manager_loc allowDeferredLocationUpdatesUntilTraveled:100 timeout:30];

    }
 [self.manager_loc stopUpdatingLocation];

}



-(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error

{if (manager.location != nil)

    { [self.locationArray addObject:manager.location];

    }


    if (error != nil)

    {

        [self.locationErrorArray addObject:error.description];

    }

    self.deferredStatus = NO;



    NSLog(@"deffered success %f  %f", manager.location.coordinate.latitude, manager.location.coordinate.longitude);

}

@end

如果我没有在didUpdateToLocations Delegate中停止位置更新,则位置箭头(在状态栏上)不会停止。在那种情况下,它给了我一些位置。我希望在特定时间或特定距离之后更新位置,以便我可以使用用户位置点击服务器。请帮帮我。

3 个答案:

答案 0 :(得分:0)

使用LocationManager distanceFilter属性获取特定行进距离的更新位置。

self.manager_loc.distanceFilter= 100;// In meters 

答案 1 :(得分:0)

如果您想在Backggriound中更新位置,请注册您的背景更新。 Youca可以用plist来做。

将位置管理器设置为:

 if ([self.locationManager respondsToSelector:@selector(pausesLocationUpdatesAutomatically)]) {
        self.locationManager.pausesLocationUpdatesAutomatically = NO;
 }

然后,如果您希望在一段时间或距离后更新位置,请使用:

- (void)allowDeferredLocationUpdatesUntilTraveled:(CLLocationDistance)distance
                      timeout:(NSTimeInterval)timeout // No guaranty it will work exactly or not

如果您希望根据距离更新位置,则可以使用 期望的精确和distanceFilter财产。

self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;// Use other accurarcy as your need
self.locationManager.distanceFilter = 3000; //100, 200, etc

如果您将活动类型设置为CLActivityTypeFitness,则以上所有设置都将被覆盖,并且位置管理员会根据此活动进行更新,根据我的知识,该活动会吃掉电池。

使用CLLocation Manager时,你应该接受它,不会让所有updart都100%准确。

答案 2 :(得分:0)

请参阅我对这篇文章的回答:StartUpdateLocations in Background, didUpdatingToLocation only called 10-20 times

如果您需要在iOS 7下的后台进行位置更新,则必须在应用程序位于前台时调用startUpdatingLocation。当您的应用程序处于后台时,您无法再执行此操作,因此您只有在需要时才能在需要时注册位置更新。在你的应用程序运行的整个过程中(在前台和后台),你被迫注册它们,所以你不得不浪费很多精力。

当您不需要位置更新时,可以通过将精度设置为kCLLocationAccuracyThreeKilometers来减少电池使用量,并仅在需要更新时将其设置为kCLLocationAccuracyBest。但是,这将比预期的更快地耗尽电池。

请向Apple写一个错误报告并询问iOS 4,5和6的“旧”行为,您可以在后台调用“startUpdatingLocation”以在后台获取位置更新。如果Apple得到足够的要求将此行为更改回iOS 5/6中的实现方式,Apple更有可能将其更改回来。

目前的情况非常糟糕。对于被迫浪费能源或放弃他们的应用程序的开发人员来说不好,对用户来说很糟糕,他们的设备需要更早地插入电源,或者不能再使用某些应用程序。