ios background location update
About positioning
There are three official recommendations
Generally, the first two are mixed, and judgment is made in
- (void)applicationDidEnterBackground:(UIApplication *)application
(turn off gps, turn on signal positioning), provided that you need the program to locate when it is in the backgroundHow to be able to get information at intervals in the background without showing the location icon
Because apple generally only allows programs to run in the background for a maximum of 600 seconds, the solutions I can think of at the moment are:
Allow background positioning in plist
An app that provides continuous location updates to the user (even when in the background) can enable background location services by including the UIBackgroundModes key (with the location value) in its Info.plist file.
Set the code that needs to be run in the background in applicationDidEnterBackground- (void)applicationDidEnterBackground:(UIApplication *)application
{
self.inBackground = YES;
[self.locationManager stopUpdatingLocation];
UIBackgroundTaskIdentifier __block bgTask;
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
while (self.inBackground) {
[self startLocationServices];
[NSThread sleepForTimeInterval:(550)];
}
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
The switch is positioned here, otherwise the icon will always be displayed..
- (void)startLocationServices{
[self.locationManager startUpdatingLocation];
DLOG(@"timer start location service here");
DLOG(@"%f",[UIApplication sharedApplication].backgroundTimeRemaining);
}
concluding remarks
Why do I feel so pained every time I want to implement a function...
I don't know if this can pass the review. At present, I still tend to use the background signal monitoring and positioning method recommended by Apple..
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Swift의 패스트 패스Objective-C를 대체하기 위해 만들어졌지만 Xcode는 Objective-C 런타임 라이브러리를 사용하기 때문에 Swift와 함께 C, C++ 및 Objective-C를 컴파일할 수 있습니다. Xcode는 S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.