ios background location update


About positioning


There are three official recommendations

  • The significant-change location service (Recommended)
  • Foreground-only location services
  • Background location services

  • 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 background
    How 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..

    좋은 웹페이지 즐겨찾기