我有一个iOS项目,在调试模式下,根本没有崩溃。 将应用程序发布到TestFlight时,我从Testflight界面启动应用程序(使用打开按钮),我遇到了崩溃。
如果我从iPhone主屏幕启动应用程序,则不会崩溃。
Thread : Crashed: com.apple.main-thread
0 AppName 0x100316b8c specialized AppDelegate.application(UIApplication, didFinishLaunchingWithOptions : [NSObject : AnyObject]?) -> Bool (AppDelegate.swift)
1 AppName 0x100314284 @objc AppDelegate.application(UIApplication, didFinishLaunchingWithOptions : [NSObject : AnyObject]?) -> Bool (AppDelegate.swift)
2 UIKit 0x1863428a8 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 400
3 UIKit 0x186572094 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2904
4 UIKit 0x186576500 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1684
5 UIKit 0x186573674 -[UIApplication workspaceDidEndTransaction:] + 168
6 FrontBoardServices 0x182b237ac __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 36
7 FrontBoardServices 0x182b23618 -[FBSSerialQueue _performNext] + 168
8 FrontBoardServices 0x182b239c8 -[FBSSerialQueue _performNextFromRunLoopSource] + 56
9 CoreFoundation 0x181139124 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
10 CoreFoundation 0x181138bb8 __CFRunLoopDoSources0 + 540
11 CoreFoundation 0x1811368b8 __CFRunLoopRun + 724
12 CoreFoundation 0x181060d10 CFRunLoopRunSpecific + 384
13 UIKit 0x18633b834 -[UIApplication _run] + 460
14 UIKit 0x186335f70 UIApplicationMain + 204
15 AppName 0x100316388 main (AppDelegate.swift:18)
16 libdispatch.dylib 0x180bfe8b8 (Missing)
修改
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
Fabric.with([Crashlytics.self])
log.xcodeColorsEnabled = true
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm:ss.SSS"
dateFormatter.locale = NSLocale.currentLocale()
log.dateFormatter = dateFormatter
#if DEBUG
log.setup(.Debug, showLogIdentifier: false, showFunctionName: true, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, showDate: true, writeToFile: nil, fileLogLevel: nil)
#else
log.setup(.Severe, showThreadName: true, showLogLevel: true, showFileNames: true, showLineNumbers: true, writeToFile: nil)
if let consoleLog = log.logDestination(XCGLogger.Constants.baseConsoleLogDestinationIdentifier) as? XCGConsoleLogDestination {
consoleLog.logQueue = XCGLogger.logQueue
}
#endif
application.applicationIconBadgeNumber = 0
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.mergeChanges(_:)), name: NSManagedObjectContextDidSaveNotification, object: nil)
//MARK: - Notifications registration
remoteToken()
if let launchOpts = launchOptions {
let remoteNotification = launchOpts[UIApplicationLaunchOptionsRemoteNotificationKey] as! NSDictionary
if let notifFrom = remoteNotification["from"] as? Int, notifType = remoteNotification["type"] as? Int {
let userid = notifFrom
let type = notifType
if type != MessageType.NotifyMsgType.rawValue {
loadingChatUserId = userid
}else{
loadingChatUserId = nil
}
}
}else{
loadingChatUserId = nil
}
return true
}
func remoteToken() {
if UIApplication.sharedApplication().isRegisteredForRemoteNotifications() {
//iOS 8 notifications
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: [UIUserNotificationType.Sound, .Alert, .Badge], categories: nil) )
UIApplication.sharedApplication().registerForRemoteNotifications()
}
}
答案 0 :(得分:2)
if let launchOpts = launchOptions {
let remoteNotification = launchOpts[UIApplicationLaunchOptionsRemoteNotificationKey] as! NSDictionary
if let notifFrom = remoteNotification["from"] as? Int, notifType = remoteNotification["type"] as? Int {
let userid = notifFrom
let type = notifType
if type != MessageType.NotifyMsgType.rawValue {
loadingChatUserId = userid
}else{
loadingChatUserId = nil
}
}
}else{
loadingChatUserId = nil
}
以下是您的问题...您强行将展开选项展开为UIApplicationLaunchOptionsRemoteNotificationKey
。改为使用可选的绑定。
if let launchOpts = launchOptions {
if let remoteNotification = launchOpts[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary{
....
}