【问题标题】:UILocalNotification in background - iOS 9UILocalNotification 在后台 - iOS 9
【发布时间】:2015-10-20 16:29:02
【问题描述】:

我有一个导航应用,它在获取位置更新时使用UILocalNotification

当应用程序在 iOS 9 设备上进入后台(锁定屏幕/按下主页按钮)时 - 它会停止接收通知。适用于 iOS 8 设备。

当应用程序处于前台而不是后台时,我会收到通知(这当然更重要)。

代码如下所示:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
    CLLocation *currLocation = [locations lastObject];

    CLLocationDistance dist = [currLocation distanceFromLocation:[[CLLocation alloc] initWithLatitude:self.coordinate.latitude longitude:self.coordinate.longitude]];

    BOOL gotToDestination = dist < 60;

    if (gotToDestination) {
        UILocalNotification* n1 = [[UILocalNotification alloc] init];
        n1.alertBody = [NSString stringWithFormat:@"You have arrived!"];
        n1.soundName = UILocalNotificationDefaultSoundName;
        n1.fireDate = [NSDate date];
        [[UIApplication sharedApplication] presentLocalNotificationNow:n1];
    }
}
  • 我一直请求授权
  • 我已检查后台模式 -> 位置更新
  • 我已注册用户通知

感谢您的帮助。

【问题讨论】:

    标签: ios objective-c ios9 cllocationmanager uilocalnotification


    【解决方案1】:

    好的,我知道了。

    问题不在于UILocalNotification,而在于位置管理器。

    从 iOS 9 开始,CLLocationManager 有一个新的 BOOL 属性 - allowsBackgroundLocationUpdates,默认为 NO。

    如果您想继续获取位置更新,您可能需要将其设置为 YES。

    这解决了我的问题。

    【讨论】:

    • 请注意,您的应用程序在后台运行 30 秒后可能会暂停或终止。为防止这种情况发生,您应该明确要求 iOS 允许后台执行:developer.apple.com/library/ios/documentation/iPhone/Conceptual/…。有趣,但 iOS 模拟器不会杀死你的应用,而真实设备会。
    • 谢谢,我知道这一点,但这对未来的读者有好处:)
    猜你喜欢
    • 2017-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多