ios - 从 Testflight 打开按钮启动应用程序时崩溃

标签 ios swift appdelegate testflight

我有一个 iOS 项目,处于 Debug模式,根本没有崩溃。 当将应用程序发布到 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()
    }
  }

最佳答案

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。请改用可选绑定(bind)。

if let launchOpts = launchOptions {
    if let remoteNotification = launchOpts[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary{
        ....
    }

关于ios - 从 Testflight 打开按钮启动应用程序时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36420046/

相关文章:

Swift:iOS:从容器 View 内部调用 segue

swift - 关于 swift 中的 AppDelegate 窗口

iphone - UITextField 不显示键盘

iOS Metal——在写入纹理时读取旧值

ios - 如何以正确的方式跟踪 set locationManager.delegate?

ios - 带有 Swift UIApplication 子类的 Objective-C 项目。错误 : class not loaded

objective-c - 对象在 View 中和设备方向更改后位置不正确

swift - ForEach 与 swiftui 中 ViewBuilder 内的自定义 View 数组

ios - 如果它在 TabBarController 中,如何从 AppDelegate 访问 ViewController 的属性?

objective-c - appdelegate 共享实例委托(delegate)