iOS App被杀死, 检测到位置发生变化,能够唤醒app发送通知

一:首先创建一个CLLocationManager子类GeofenceMonitor(名字随意):
.h文件基本内容,可根据需求添加功能
#import 

@interface GeofenceMonitor : CLLocationManager 

+ (instancetype)geofenceMonitor;

@property (nonatomic, strong) CLLocationManager *locationManager;

@end
.m文件基本内容,可根据需求添加功能
#import "GeofenceMonitor.h"
@implementation GeofenceMonitor
+ (instancetype)geofenceMonitor {
  static GeofenceMonitor *shared = nil;

  static dispatch_once_t onceTocken;
  dispatch_once(&onceTocken, ^{
      shared = [[GeofenceMonitor alloc] init];
  });
  return shared;
}

- (instancetype)init {
  self = [super init];
  if (self) {
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    //Set whether to allow the system to automatically pause positioning, (set YES, background positioning will stop after about 20 minutes)
    self.locationManager.pausesLocationUpdatesAutomatically = NO;
    //After 9.0 this must want to add, do not add cannot realize backstage to locate continuously
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >=9.0) {
        self.locationManager.allowsBackgroundLocationUpdates = YES;
    }
    [self.locationManager setDesiredAccuracy: kCLLocationAccuracyHundredMeters];
    [self.locationManager setDistanceFilter: 100];
  }
  return self;
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations{
   NSLog(@"didUpdateLocations: %@",locations);
}
二:AppDelegate初始化,在app被杀死的情况下,地理位置发生重大变化,apple系统会自动帮你唤醒app:
#import "GeofenceMonitor.h"
#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {

  //app was launched in response to a CoreLocation event.
  if( [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey])  {
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    [[GeofenceMonitor geofenceMonitor] requestAlwaysAuthorization];
    #endif
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
    [[GeofenceMonitor geofenceMonitor] setAllowsBackgroundLocationUpdates:YES];
    #endif
    [[GeofenceMonitor geofenceMonitor] startMonitoringSignificantLocationChanges];
  }

 return YES;
}

tip:经测试,发现手机关机重启之后,依旧可以检测到位置变化,发送位置通知,注意:定位转态要选择始终允许

你可能感兴趣的:(iOS App被杀死, 检测到位置发生变化,能够唤醒app发送通知)