美文网首页iOS随笔小记iOS开发
iOS App被杀死, 检测到位置发生变化,能够唤醒app发送

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

作者: 七一小月 | 来源:发表于2021-09-17 17:02 被阅读0次
一:首先创建一个CLLocationManager子类GeofenceMonitor(名字随意):
.h文件基本内容,可根据需求添加功能
#import <CoreLocation/CoreLocation.h>

@interface GeofenceMonitor : CLLocationManager <CLLocationManagerDelegate>

+ (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<CLLocation *> *)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发送

    一:首先创建一个CLLocationManager子类GeofenceMonitor(名字随意): .h文件基本内...

  • iOS APP-A 唤醒其它 APP-B

    实现逻辑 iOS支持通过Scheme唤醒APP。 操作步骤 被唤醒的APP端的设置:即B端 在需要被打开的app的...

  • 在webview中唤醒APP或者引导用户下载

    在webview中唤醒APP或者引导用户下载 唤醒APP 下载APP,IOS系统跳App Store,安卓系统可以...

  • App唤醒

    App唤醒 H5页面唤醒App,兼容iOS和安卓的方式是location.href

  • ReactNative App更新下载(Android+iOS)

    APP涉及到版本更新(非热更新),版本检测下载App,Android和iOS实现方式不同 1.Android直接和...

  • push代理失效——热启动和冷启动表现不一致

    冷启动:指app被后台杀死后,在这个状态打开app,这种启动方式叫做冷启动。 热启动:指app没有被后台杀死,仍然...

  • iOS推送原理

    1.由App向iOS设备发送一个注册通知,用户需要同意系统发送推送。2.iOS向APNs远程推送服务器发送App的...

  • js 系统检测

    业务需求上,经常会有移动端添加引导下载app,IOS引导到app store,安卓引导到应用宝等。 检测当前设备是...

  • iOS唤醒app

    今天工作需要,要实现微信、QQ等扫描二维码,唤醒app,跳到指定页面的功能。我去,一想没有做过呀!好吧!今天有时间...

  • 导航

    在iOS系统中,实现导航基本有以下三种方法: 可以将需要导航的位置丢给系统自带的APP进行导航 发送网络请求到服务...

网友评论

    本文标题:iOS App被杀死, 检测到位置发生变化,能够唤醒app发送

    本文链接:https://www.haomeiwen.com/subject/okcsgltx.html