当应用程序处于后台时,向服务器发送经纬度

我经历了这么多的链接,甚至在我还没有find获得经纬度的合适解决scheme之后。

定期iOS背景位置更新

具有“位置”后台模式的iOS长时间运行后台计时器

我尝试了一些链接和论坛,但它只工作了3分钟,然后应用程序根本不更新用户的位置。

- (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. //create new uiBackgroundTask __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; //and create new timer with async call: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ [locationManager startUpdatingLocation]; NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(startTrackingBg) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop] run]; }); } -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { // store data CLLocation *newLocation = [locations lastObject]; //tell the centralManager that you want to deferred this updatedLocation if (_isBackgroundMode && !_deferringUpdates) { _deferringUpdates = YES; [locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:10]; } } 

好。

经过三天挣扎之后,即使在3分钟之后,应用程序还在后台,它也在为我发送经纬度。

我检查了我的应用程序,不断发送经过一个多小时的背景。

它至less可以帮助一个人。

首先请在你的pList中添加两个键。

  1.NSLocationAlwaysUsageDescription 2.NSLocationWhenInUseUsageDescription 

Bothe是string,你可以给任何价值。

然后请在项目部分的function下打开后台获取并检查位置更新。

然后导入Corelocation框架并添加下面的代码。

locationManager是一个全局variables。

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //create CLLocationManager variable locationManager = [[CLLocationManager alloc] init]; //set delegate locationManager.delegate = self; app = [UIApplication sharedApplication]; // This is the most important property to set for the manager. It ultimately determines how the manager will // attempt to acquire location and thus, the amount of power that will be consumed. if ([locationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) { [locationManager setAllowsBackgroundLocationUpdates:YES]; } locationManager.desiredAccuracy = 45; locationManager.distanceFilter = 100; // Once configured, the location manager must be "started". [locationManager startUpdatingLocation]; } - (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. [locationManager stopUpdatingLocation]; [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [locationManager setDistanceFilter:kCLDistanceFilterNone]; locationManager.pausesLocationUpdatesAutomatically = NO; locationManager.activityType = CLActivityTypeAutomotiveNavigation; [locationManager startUpdatingLocation]; } - (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 stopUpdatingLocation]; __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ bgTask = UIBackgroundTaskInvalid; }]; NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(startTrackingBg) userInfo:nil repeats:YES]; } -(void)startTrackingBg { [locationManager startUpdatingLocation]; NSLog(@"App is running in background"); } //starts automatically with locationManager -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ latitude=newLocation.coordinate.latitude; longitude=newLocation.coordinate.longitude; NSLog(@"Location: %f, %f",newLocation.coordinate.longitude, newLocation.coordinate.latitude); }