小编Sta*_*cky的帖子

获取iOS应用在后台甚至被杀的位置

我的应用程序需要在应用程序处于活动状态以及何时处于非活动状态并被杀死时获取用户的位置.当用户的位置靠近商店时,应用程序必须发送本地通知.

我不确定到底发生了什么,但是我无法让我的应用程序在后台获取位置并在被杀时将其唤醒.

我有一个位置管理器(单例,用于"使用"和"始终"时的两种情况),我在.plist中定义了NSLocationAlwaysUsageDescription和NSLocationWhenInUseUsageDescription

我在做的是:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //The app has been killed/terminated (not in background) by iOS or the user.
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]){

        _locationManager = [CoreLocationManager sharedInstance];
        _locationManager.isAppActive = NO;
        _locationManager.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
        _locationManager.locationManager.activityType = CLActivityTypeOtherNavigation;

        if ([_locationManager.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [_locationManager.locationManager requestAlwaysAuthorization];
        }

        [_locationManager addLocationManagerDelegate:self];
    }
}


- (void)applicationDidBecomeActive:(UIApplication *)application
{
    if (_locationManager.locationManager){
        _locationManager.isAppActive = YES;
        [_locationManager.locationManager stopMonitoringSignificantLocationChanges];
    }

    _locationManager = [CoreLocationManager sharedInstance];

    if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
        [_locationManager.locationManager requestAlwaysAuthorization];
    }

    if ([_locationManager.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [_locationManager.locationManager requestWhenInUseAuthorization];
    } …
Run Code Online (Sandbox Code Playgroud)

objective-c core-location ios appdelegate

7
推荐指数
1
解决办法
3053
查看次数

标签 统计

appdelegate ×1

core-location ×1

ios ×1

objective-c ×1