当应用程序因位置变化而启动时,如何实现didFinishLaunchingWithOptions方法?

时间:2014-04-08 08:41:54

标签: ios objective-c geolocation location core-location

我正在我的应用程序中使用区域监控,当我在应用程序启动时在城市中行走时,它的工作非常顺利。

我的问题是在我的应用终止时收到这些位置更改通知。我的应用程序因位置变化而被唤醒时崩溃,当我尝试在应用程序中设置rootViewController时崩溃:didFinishLaunchingWithOptions:method。

  1. 当我的应用程序因位置变化而在后台启动时,我应该以不同方式实现此方法吗?

  2. 当我的应用程序在进入/激发区域时被唤醒时,我在后台执行任务需要多长时间?

1 个答案:

答案 0 :(得分:0)

要回答您的第二个问题,iOS / Apple表示您有大约10秒的时间来完成必要的工作。

第一个问题:

在你的app delegate.h中,添加:CLLocationManagerDelegate

application:didFinishLaunchingWithOptions:方法中添加:

locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];

并补充说:

- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    [notification setSoundName:UILocalNotificationDefaultSoundName];
    [notification setApplicationIconBadgeNumber:0];

    if(state == CLRegionStateInside) {
        [notification setAlertBody:@"CLRegionStateInside"];
    }
    else if(state == CLRegionStateOutside) {
        [notification setAlertBody:@"CLRegionStateOutside"];
    }

    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

然后,如果您有区域代码(可能在某个视图控制器中某处),请添加:[yourRegion setNotifyEntryStateOnDisplay:YES];

在您最初致电之前

[locationManager startMonitoringForRegion:yourRegion];

这就是全部!