1,voip方式
    1)首先要修改应用的plist配置,添加下面的设置:
        Application does not run in background: NO
        Required background modes: VOIP
    注:添加这些配置之后,程序重启解锁就会自动运行application:              
                    didFinishLaunchingWithOptions方法。
    2)主要测试代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
     __block UIBackgroundTaskIdentifier background_task;
    background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
        //打开后台运行任务
    }];
   
    static dispatch_queue_t _queue ;
    _queue = dispatch_queue_create("sddfgfgwerty456567fre4ghghkjdsfbnjfrtyrt", DISPATCH_QUEUE_SERIAL);
    dispatch_async(_queue, ^{
        while(TRUE)
        {
            //打印进程id
            NSLog(@"processID = %d++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++CurrentThread = %@",[SSProcessInfo processID],[[NSThread currentThread] description]);
            [NSThread sleepForTimeInterval: 5]; //wait for 15 min
        }
    });
    
    sleep(1);
    return YES;
}

注:程序测试必须真机下,观察log信息用Xcode上自带的Organizer工具。
经过分析log信息,发现processID过一段时间就会变,说明程序运行一段时间就会被kill掉,然后程序会自动重启。虽然达到了持续运行的目的,但是不符合程序持续运行要求。

2,GPS方式
    1)首先要修改应用的plist配置,添加下面的设置:
    Application does not run in background: NO
    Required background modes: App registers for location updates
    添加这两项之后可以保证程序在后台运行
    2)主要测试代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([CLLocationManager significantLocationChangeMonitoringAvailable])
     {
        [self.locationManager startUpdatingLocation];
      }
   else
     {
         NSLog(@"Significant location change monitoring is not available.");
     }
     return YES;
}

- (CLLocationManager *)locationManager
{
    
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        
        if([CLLocationManager locationServicesEnabled])
        {
               NSLog(@"无限运行................................................");
            //这个分支进入后台后,运行时间和前台一样,是无限的
            [_locationManager startMonitoringSignificantLocationChanges];
            _locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
            _locationManager.distanceFilter= 1;
            _locationManager.pausesLocationUpdatesAutomatically = NO;
            _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
            [_locationManager startUpdatingLocation];
       }
    }
  return _locationManager;
    
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    while (true) {
        [NSThread sleepForTimeInterval:1];
            NSLog(@"processID = %d++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++CurrentThread = %@",[SSProcessInfo processID],[[NSThread currentThread] description]);
        [self.locationManager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:10];
    }
}

注:程序测试必须真机下,观察log信息用Xcode上自带的Organizer工具。
经过分析log信息,发现processID不会变,说明程序能够持续运行。