iPhone 应用程序在收到推送通知时崩溃
iPhone App crashes when receiving 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)
})
}
这里没有足够的信息来找出问题所在,但如果我戴上心理调试帽,这里的 if 语句很容易因为强制转换为 Bool
if:
- managedConfig 是一个
NSDictionary
- 它有一个
locationTrackingDisabled
键,它存储了 Bool
以外的东西(可能是 NSString
)
更好的检查方法是使用 if let
语句来安全地确定字典中的值是否具有预期的类型。
if let trackLocation = managedConfig["locationTrackingDisabled"] as? Bool, trackLocation {
locationmanager.startUpdatingLocation()
}
请注意,您可以通过 Xcode 在设备上 运行 您的应用,并且仍然会收到推送通知。此外,如果您要通过商店分发应用程序,强烈建议您采用某种方式来跟踪崩溃,即通过 HockeyApp、Crashlytics、Crittercism 等。那里有很多选择。
我有一个应用程序可以在我发送推送通知时向我发送设备的位置。但是,当应用程序 运行 在前台时,应用程序在收到推送通知时崩溃。
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)
})
}
这里没有足够的信息来找出问题所在,但如果我戴上心理调试帽,这里的 if 语句很容易因为强制转换为 Bool
if:
- managedConfig 是一个
NSDictionary
- 它有一个
locationTrackingDisabled
键,它存储了Bool
以外的东西(可能是NSString
)
更好的检查方法是使用 if let
语句来安全地确定字典中的值是否具有预期的类型。
if let trackLocation = managedConfig["locationTrackingDisabled"] as? Bool, trackLocation {
locationmanager.startUpdatingLocation()
}
请注意,您可以通过 Xcode 在设备上 运行 您的应用,并且仍然会收到推送通知。此外,如果您要通过商店分发应用程序,强烈建议您采用某种方式来跟踪崩溃,即通过 HockeyApp、Crashlytics、Crittercism 等。那里有很多选择。