【问题标题】:iOS startMonitoringForRegion didn't work in Sleep mode (black screen)iOS startMonitoringForRegion 在睡眠模式下不起作用(黑屏)
【发布时间】:2012-05-01 11:53:45
【问题描述】:

我正在尝试使用 startMonitoringForRegion 在 iOS 中为 iPhone 4 编写一个应用程序,每次位置委托接收到位置更新时都会触发本地通知。

只要手机处于唤醒状态(屏幕亮起),应用程序运行良好,会触发通知,但是当我将手机置于睡眠状态(屏幕为黑色)时,我将在醒来之前不再收到通知按下home键打开手机

我尝试在 info.plist 中添加“必需的后台模式”-“应用程序注册位置更新”键,但这没有任何意义...

CLLocationManager 委托设置为 AppDelegate。

我需要做什么才能在设备处于睡眠状态时注册位置更新?提前致谢。

【问题讨论】:

    标签: ios


    【解决方案1】:

    可能是应用关闭后CLLocationManager实例被释放了。

    对我来说,我会创建一个带有 CLLocationManager 属性的单例位置管理器,并将委托设置为单例。之后,我会调用startMonitoringSignificantLocationChanges 方法来确保应用程序接收到位置更新。即使应用被杀死,它也能工作。

    如果应用程序被杀死,我需要在应用程序委托didFinishLaunchingWithOptions 方法中实例化位置管理器单例:

    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey]) {
        //instantiate the singleton here
        NSArray *regions = [[[[LocationManager sharedManager] locationManager] monitoredRegions] allObjects];
    }
    

    我在这里详细解释了使用重大位置更改和标准位置服务之间的区别:location based push notification when app is not running?

    【讨论】:

      猜你喜欢
      • 2011-05-01
      • 1970-01-01
      • 2020-07-11
      • 2015-06-03
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      相关资源
      最近更新 更多