使用 Xcode 11.3.1 构建后打开时应用程序崩溃
App crashes when opening after building it with Xcode 11.3.1
升级到 Xcode 11.3.1 后,我们注意到我们的应用在某些设备上打开时开始崩溃。它发生在不同的 iOS 版本和 iPhone 型号上。但是,在具有相同 iOS 版本或 iPhone 型号的某些设备上,该应用程序运行正常。
在这些设备上安装较早版本的应用程序时(使用较早 Xcode 版本构建)不会发生崩溃。
在调试时我们发现当使用我们的登录模块的演示者试图打开 Optional
时会发生崩溃。这个演示者在创建和分配时有一个值,但是在 AppDelegate window.rootViewController = navigationController
中设置我们的 rootViewController
后,执行跳转到下面显示的 viewDidLoad()
函数,此时演示者是 nil
.
class LoginViewController: LoginPasswordResetViewController {
var presenter: LoginPresenterProtocol!
override func viewDidLoad() {
super.viewDidLoad()
updateUI(model: presenter.loadViewModel())
presenter.viewDidLoad()
}
}
AppDelegate.swift中的相关代码:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let env = Environment()
if let app = UIApplication.shared.delegate as? AppDelegate,
let window = app.window,
let loginVC = LoginModule().createModule(moduleData: LoginModuleData(env: env),
service: LoginService(env: env),
storageService: LocalStorageService(env: env)) {
let navigationController = UINavigationController(rootViewController: loginVC)
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
return true
}
我尝试使用此处提到的 SceneDelegate.swift 来设置 RootViewController,但没有成功。
关于这个问题的可能原因有什么想法吗?
附加信息:
createModule
函数初始化并设置 presenter
:
func createModule(moduleData: LoginModuleDataProtocol, service: LoginServiceProtocol, storageService: LocalStorageServiceProtocol) -> LoginViewController? {
guard let viewController: LoginViewController = UIStoryboard.viewController(from: self) else {
return nil
}
let presenter = LoginPresenter()
let interactor = LoginInteractor()
let router = LoginRouter()
// VC
viewController.presenter = presenter
// Presenter
presenter.viewController = viewController
presenter.interactor = interactor
presenter.router = router
// Interactor
interactor.presenter = presenter
interactor.dataManager = LoginDataManager(data: moduleData, service: service, storageService: storageService)
// Router
router.viewController = viewController
return viewController
}
创建模块后,loginVC
变量具有应有的 presenter
值,但是一旦我们分配 window.rootViewController = navigationController
,presenter
的值就是 nil
.
我成功了。我必须使用 SceneDelegate
加载组件才能在 iOS 13 but I only managed to make it work after reading 时启动我的应用程序。
所以这是对我有用的结果:
SceneDelegate.swift:
import UIKit
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let env = Environment()
if let windowScene = scene as? UIWindowScene,
let loginVC = LoginModule().createModule(moduleData: LoginModuleData(env: env),
service: LoginService(env: env),
storageService: LocalStorageService(env: env)) {
self.window = UIWindow(windowScene: windowScene)
self.window!.rootViewController = UINavigationController(rootViewController: loginVC)
self.window!.makeKeyAndVisible()
}
}
}
AppDelegate.swift:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 13.0, *) {} else {
let env = Environment()
if let app = UIApplication.shared.delegate as? AppDelegate,
let window = app.window,
let loginVC = LoginModule().createModule(moduleData: LoginModuleData(env: env),
service: LoginService(env: env),
storageService: LocalStorageService(env: env)) {
window.rootViewController = UINavigationController(rootViewController: loginVC)
window.makeKeyAndVisible()
}
}
return true
}
升级到 Xcode 11.3.1 后,我们注意到我们的应用在某些设备上打开时开始崩溃。它发生在不同的 iOS 版本和 iPhone 型号上。但是,在具有相同 iOS 版本或 iPhone 型号的某些设备上,该应用程序运行正常。
在这些设备上安装较早版本的应用程序时(使用较早 Xcode 版本构建)不会发生崩溃。
在调试时我们发现当使用我们的登录模块的演示者试图打开 Optional
时会发生崩溃。这个演示者在创建和分配时有一个值,但是在 AppDelegate window.rootViewController = navigationController
中设置我们的 rootViewController
后,执行跳转到下面显示的 viewDidLoad()
函数,此时演示者是 nil
.
class LoginViewController: LoginPasswordResetViewController {
var presenter: LoginPresenterProtocol!
override func viewDidLoad() {
super.viewDidLoad()
updateUI(model: presenter.loadViewModel())
presenter.viewDidLoad()
}
}
AppDelegate.swift中的相关代码:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let env = Environment()
if let app = UIApplication.shared.delegate as? AppDelegate,
let window = app.window,
let loginVC = LoginModule().createModule(moduleData: LoginModuleData(env: env),
service: LoginService(env: env),
storageService: LocalStorageService(env: env)) {
let navigationController = UINavigationController(rootViewController: loginVC)
window.rootViewController = navigationController
window.makeKeyAndVisible()
}
return true
}
我尝试使用此处提到的 SceneDelegate.swift
关于这个问题的可能原因有什么想法吗?
附加信息:
createModule
函数初始化并设置 presenter
:
func createModule(moduleData: LoginModuleDataProtocol, service: LoginServiceProtocol, storageService: LocalStorageServiceProtocol) -> LoginViewController? {
guard let viewController: LoginViewController = UIStoryboard.viewController(from: self) else {
return nil
}
let presenter = LoginPresenter()
let interactor = LoginInteractor()
let router = LoginRouter()
// VC
viewController.presenter = presenter
// Presenter
presenter.viewController = viewController
presenter.interactor = interactor
presenter.router = router
// Interactor
interactor.presenter = presenter
interactor.dataManager = LoginDataManager(data: moduleData, service: service, storageService: storageService)
// Router
router.viewController = viewController
return viewController
}
创建模块后,loginVC
变量具有应有的 presenter
值,但是一旦我们分配 window.rootViewController = navigationController
,presenter
的值就是 nil
.
我成功了。我必须使用 SceneDelegate
加载组件才能在 iOS 13
所以这是对我有用的结果:
SceneDelegate.swift:
import UIKit
@available(iOS 13.0, *)
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let env = Environment()
if let windowScene = scene as? UIWindowScene,
let loginVC = LoginModule().createModule(moduleData: LoginModuleData(env: env),
service: LoginService(env: env),
storageService: LocalStorageService(env: env)) {
self.window = UIWindow(windowScene: windowScene)
self.window!.rootViewController = UINavigationController(rootViewController: loginVC)
self.window!.makeKeyAndVisible()
}
}
}
AppDelegate.swift:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_: UIApplication, didFinishLaunchingWithOptions _: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 13.0, *) {} else {
let env = Environment()
if let app = UIApplication.shared.delegate as? AppDelegate,
let window = app.window,
let loginVC = LoginModule().createModule(moduleData: LoginModuleData(env: env),
service: LoginService(env: env),
storageService: LocalStorageService(env: env)) {
window.rootViewController = UINavigationController(rootViewController: loginVC)
window.makeKeyAndVisible()
}
}
return true
}