【问题标题】:iPhone App crashes when receiving push notificationiPhone 应用程序在收到推送通知时崩溃
【发布时间】:2017-01-15 06:25:39
【问题描述】:

我有一个应用程序,当我发送推送通知时,它会向我发送设备的位置。但是,当应用在前台运行时,应用会在收到推送通知时崩溃。

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler:(UIBackgroundFetchResult) -> Void ) 
{
    NSLog("\(userInfo)")
    if (managedConfig["locationTrackingDisabled"] ?? false) as! Bool == false {
    locationManager.startUpdatingLocation()
    }
    let seconds = 4.0
    let delay = seconds * Double(NSEC_PER_SEC)  // nanoseconds per seconds
    let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

    dispatch_after(dispatchTime, dispatch_get_main_queue(), {
        completionHandler(UIBackgroundFetchResult.NewData)
    })
}

【问题讨论】:

  • 你遇到了什么错误?
  • 因为我正在使用 APNS,所以我正在创建一个 .ipa 并安装在我的设备上。不生成任何日志。
  • 为什么不创建开发证书,然后在设备上进行测试?

标签: ios iphone swift push-notification apple-push-notifications


【解决方案1】:

这里没有足够的信息来找出问题所在,但如果我戴上我的通灵调试帽,这里的 if 语句很容易因为强制转换为 Bool if 而崩溃:

  1. managedConfig 是一个NSDictionary
  2. 它有一个locationTrackingDisabled 键,用于存储Bool 以外的其他内容(可能是NSString

更好的检查方法是使用if let 语句来安全地确定字典中的值是否具有预期的类型。

if let trackLocation = managedConfig["locationTrackingDisabled"] as? Bool, trackLocation {
    locationmanager.startUpdatingLocation()
}

请注意,您可以通过 Xcode 在设备上运行您的应用程序,但仍会收到推送通知。此外,如果您要通过应用商店分发应用,强烈建议您采用某种方式来跟踪崩溃,即通过 HockeyApp、Crashlytics、Crittercism 等。有很多选择。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多