2016-05-12 36 views
3

我經歷瞭如此多的鏈接,即使在此之後,我還沒有找到獲取經緯度的適當解決方案。當應用程序處於後臺時向服務器發送經緯度

Periodic iOS background location updates

iOS long-running background timer with "location" background mode

我從一些鏈接和論壇嘗試,但它正在爲僅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]; 
} 
} 

回答

9

好的。

經過三天掙扎後,即使在3分鐘後,應用程序仍在後臺,它仍然可以爲我發送經緯度。

我檢查了我的應用程序,在後臺連續發送超過一個小時的經緯度。

它可以幫助至少一個。

首先請在您的pList中添加以下兩個鍵。

1.NSLocationAlwaysUsageDescription 
2.NSLocationWhenInUseUsageDescription 

它是字符串,你可以給任何值。

然後請打開背景獲取並檢查項目部分功能下的位置更新。

然後導入Corelocation框架並添加下面的代碼。

locationManager是一個全局變量。

- (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); 
} 
+0

@Santo ...請檢查該bro..and試圖幫助我.. http://stackoverflow.com/questions/37338466/sending-current-location-to-server-in-background- as-well-as-in-running-app- –

+0

@SurajSukale首先讓我知道,你在建立時是否獲得了當前位置? – Santo

+0

沒有兄弟...其實我在這個應用程序開發非常新(第一次工作在這個特定的域位置)....你能幫我解決這個 –

相關問題