简体   繁体   English

收到推送通知时,iPhone App崩溃

[英]iPhone App crashes when receiving push notification

I have an app that sends me the location of the device when I send a push notification. 我有一个应用程序,可以在我发送推送通知时向我发送设备的位置。 But, while the app is running in the foreground the app crashes on receiving the push notification. 但是,当应用程序在前台运行时,该应用程序在收到推送通知时崩溃。

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)
    })
}

There's not really enough information here to figure out what is going wrong, but if I put on my psychic debugging hat, the if statement here can easily crash due to force casting to a Bool if: 这里确实没有足够的信息来找出问题出在哪里,但是如果我戴上通俗易懂的调试帽,则if语句很容易崩溃,因为如果强制转换为Bool则if:

  1. managedConfig is an NSDictionary managedConfig是NSDictionary
  2. it has a locationTrackingDisabled key which stores something other than a Bool (perhaps an NSString ) 它有一个locationTrackingDisabled键,该键存储了Bool以外的其他内容(也许是NSString

A better way of checking is to use an if let statement to safely determine if the value in the dictionary has the expected type. 更好的检查方法是使用if let语句安全地确定字典中的值是否具有预期的类型。

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

Note that you can run your app on a device through Xcode and still receive push notifications. 请注意,您可以通过Xcode在设备上运行您的应用,但仍会收到推送通知。 Also, if you're going to distribute an app through the Store, it's highly recommended that you have some way of tracking crashes, ie via HockeyApp, Crashlytics, Crittercism, etc. There are lots of options out there. 另外,如果您打算通过商店分发应用程序,则强烈建议您采用某种方式来跟踪崩溃,即通过HockeyApp,Crashlytics,Crittercism等进行跟踪。那里有很多选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM