在iOS应用程序中使用位置服务时,如何确定确切的应用程序重新启动原因?

在iOS应用程序中使用位置服务时,如何确定确切的应用程序重新启动原因?,ios,objective-c,core-location,cllocationmanager,Ios,Objective C,Core Location,Cllocationmanager,我正在iOS应用程序中使用位置服务,它包括显著的位置更改和地理围栏 当用户移动一段距离时,iOS会唤醒我的应用程序。我正在使用AppDelegate中的“UIApplicationLaunchActionsLocationKey”标识应用程序启动,如下所示 if (launchOptions[UIApplicationLaunchOptionsLocationKey]) { NSLog(@"App relaunched because of new location events.");

我正在iOS应用程序中使用位置服务,它包括
显著的位置更改
地理围栏

当用户移动一段距离时,iOS会唤醒我的应用程序。我正在使用AppDelegate中的“
UIApplicationLaunchActionsLocationKey
”标识应用程序启动,如下所示

if (launchOptions[UIApplicationLaunchOptionsLocationKey]) {
    NSLog(@"App relaunched because of new location events.");
} else {
    NSLog(@"Normal app open");
}
但我无法确定它是否是
显著的位置变化
地理围栏

我们是否可以使用“
uiapplicationaunchoptionslocationkey
”确定确切的应用程序重新启动原因

我知道geofence的以下委托方法:

- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLCircularRegion *)region {
} 
但由于某些原因,此方法不会被触发

我正在寻找方法来确定确切的应用程序重新启动原因(SLC或Geofence)

有什么建议吗


提前谢谢。

我不完全确定你的要求,但我会尽力的。 您是使用Geofence还是使用核心位置区域监控

如果您想知道您的应用程序是否被didEnterRegion/didExitRegiondidUpdateLocations 我想您可以让locationManager委托方法使用如下方式显示本地通知:

-(void)showBackgroundNotification:(NSString *) message {
    if (app.applicationState == UIApplicationStateBackground && [app currentUserNotificationSettings].types != UIUserNotificationTypeNone){
        UILocalNotification *note = [[UILocalNotification alloc] init];
        note.alertBody = message;
        note.soundName = UILocalNotificationDefaultSoundName;
        note.fireDate = [NSDate dateWithTimeIntervalSinceNow:0.0];
        [app scheduleLocalNotification: note];
    }
}
通过在每个委托函数中使用不同的字符串消息调用该函数

如果我理解正确,该场景是应用程序处于终止状态,即关闭。然后它得到一个位置更新并唤醒。然后事情发生了

你得到的是地区而不是地区吗?问题可能是您没有像应该的那样手动打开位置服务

我在AppDelegate.m中使用它

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //for wake from terminated on location updates
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
        GeoFence *fence = [GeoFence sharedInstance];
        [fence startMonitoringSignificantLocationChanges];
    }
}
我的Geofence班是单身学生。除locationManager委派功能外,还有以下两个重要功能:

//singleton
+ (GeoFence*) sharedInstance {
    static GeoFence *_sharedInstance = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedInstance = [[GeoFence alloc] init];
    });
    return _sharedInstance;
}

- (instancetype)init{
    self = [super init];
    if (self) {
        self.locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;

        //get authorization
        if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [locationManager requestAlwaysAuthorization];
        }
        [self startMonitoringSignificantLocationChanges];
        }

    }
    return self;
}
我真的希望你能用这个。如果你需要进一步的帮助,请告诉我。 记住在头文件中声明+(GeoFence*)sharedInstance

此外,重要的是要记住,区域监控不依赖于位置更新。文档对此并不清楚,但您可以关闭您的重要更改位置更新,仍然可以获取您的区域边界事件。但那是另一个故事