6
我有更新用戶的位置奇怪的問題。有時我的應用更新位置,但不時更新。我不知道在哪裏的問題,我的代碼(從AppDelegate.m)低於:ios,startMonitoringSignificantLocationChanges - 有時會中斷監視
- (void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(@"Went to Background");
// Only monitor significant changes
[locationManager startMonitoringSignificantLocationChanges];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Start location services
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Only report to location manager if the user has traveled 1000 meters
locationManager.distanceFilter = 1000.0f;
locationManager.delegate = self;
locationManager.activityType = CLActivityTypeAutomotiveNavigation;
[locationManager stopMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// Check if running in background or not
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground) {
isInBackground = YES;
}
if (isInBackground) {
// If we're running in the background, run sendBackgroundLocationToServer
[self sendBackgroundLocationToServer:[locations lastObject]];
} else {
// If we're not in the background wait till the GPS is accurate to send it to the server
if ([[locations lastObject] horizontalAccuracy] < 100.0f) {
[self sendDataToServer:[locations lastObject]];
}
}
}
-(void) sendBackgroundLocationToServer:(CLLocation *)location
{
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
// Send the data
[self sendDataToServer:location];
if (bgTask != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}
}
-(void) sendDataToServer:(CLLocation *)newLocation
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSString *lat = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.latitude];
NSString *lng = [NSString stringWithFormat:@"%.8f", newLocation.coordinate.longitude];
[APIConnection SaveMyPositionWitLat:lat withLng:lng];
});
}
我需要更新位置,每隔一小時,如果用戶改變位置更然後千米。我選擇了第二種方式來節省電池,但是我的解決方案無法按需要工作。
我將不勝感激任何幫助,提前致謝!
Thx,這解決了我的問題,但我應該改變我的代碼每小時獲取位置? – Nizzre
你會認爲這將需要後臺模式,並設置一個完全可能的計時器,但我不確定蘋果會批准它。爲了讓您的用戶免於使用瘋狂的電池,我會再次建議您注意一點。如果您有重大改變概念和區域監測工作,您可以對設備位置保持合理的想法。如果他們沒有移動,沒有意義不斷檢查。無論如何,這是另一個我想到的問題。 –
偉大的工作在這裏@DeanDavids。剛剛檢查你的圖書館! – wprater