[英]After 15 mins Background Location update is not working in ios?
我已經根據計時器(像每1分鍾)在ios中實現了后台位置更新。 它的工作良好,並獲得位置(緯度和經度值),但15點后斷開連接。 我無法再觸發它。 誰能幫幫我。
這是我的代碼。
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[GlobalClass sharedGlobals].locationManager stopMonitoringSignificantLocationChanges];
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
[[GlobalClass sharedGlobals].locationManager requestAlwaysAuthorization];
}
[[GlobalClass sharedGlobals].locationManager startMonitoringSignificantLocationChanges];
int timeInterval = 60; // (i.e., 1 mins )
if (!setTimer) {
setTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target: self
selector: @selector(toMakeServerCall) userInfo: nil repeats: YES];
}
}
#pragma mark - Send Current lat & long to the server
- (void)toMakeServerCall {
NSString *latitude = [NSString stringWithFormat:@"%f",[[GlobalClass sharedGlobals] currentLocationObject].coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f",[[GlobalClass sharedGlobals] currentLocationObject].coordinate.longitude];
NSLog(@"Current Lat is :%@ \n Current Long is :%@ ",latitude,longitude);
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http:www.google.com/myLatitude is: %@ and My Longitude is: %@",latitude,longitude]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
}
提前致謝。
當您使用“重大更改”服務(或坦率地說,是任何位置服務)時,您無需使用計時器。 位置發生變化時,操作系統會打電話給您 ,而不是相反。
關於您的計時器,當應用暫停時,計時器將不再運行。 因此,請勿使用計時器。 但是,借助重大更改服務,當設備檢測到它已經移動了很遠的距離時,您的應用將被喚醒並得到通知。 如《 位置和地圖編程指南:啟動重要更改位置服務 》所述:
如果您使重大更改位置服務保持運行狀態,並且隨后您的iOS應用被暫停或終止,則當新位置數據到達時,該服務會自動喚醒您的應用。 喚醒時,該應用程序將進入后台,並且您會得到一小段時間(大約10秒)來手動重新啟動位置服務並處理位置數據。 (您必須先在后台手動重新啟動位置服務,然后才能交付任何未決的位置更新,如了解何時啟動位置服務中所述 。)由於您的應用程序在后台運行,因此它必須做最少的工作並且避免執行任何任務(例如查詢網絡),可能會阻止它在分配的時間到期之前返回。 否則,您的應用將被終止。 如果iOS應用需要更多時間來處理位置數據,則可以使用
UIApplication
類的beginBackgroundTaskWithName:expirationHandler:
方法請求更多后台執行時間。
即使您將pausesLocationUpdatesAutomatically
自動設置為false,也可能會暫停位置更新。 好消息是,您可以在暫停更新時收到通知,因此可以重新啟動位置更新。
像這樣在您的CLLocationManagerDelegate
定義locationManagerDidPauseLocationUpdates
:
func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) {
manager.startUpdatingLocation()
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.