執(zhí)行兩次的原因:
出現(xiàn)兩次執(zhí)行的情況一般都是用代碼創(chuàng)建window并設(shè)置RootViewController的情況,猜想是因為有了StoryBoard之后,系統(tǒng)會默認(rèn)優(yōu)先加載Main.storyboard文件,這時會執(zhí)行一次viewDidLoad方法,之后再執(zhí)行代碼window加載RootViewController時再執(zhí)行一次viewDidLoad方法(如果猜想有錯,請各位大佬指出)
處理方案
如果使用StoryBoard做RootViewController,一般不會出現(xiàn)兩次加載viewDidLoad的情況,所以說一下使用代碼創(chuàng)建window 和 RootVC的情況,只需要關(guān)閉Xcode使用StoryBoard加載RootVC的設(shè)置就可以了。處理方案可見下面文章:
https://blog.csdn.net/weixin_33916814/article/details/109809711
刪除AppDelegate中的下面兩個方法:
swift:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
//
// @available(iOS 13.0, *)
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
OC:
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)){
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions API_AVAILABLE(ios(13.0)){
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
在刪除info.plist文件中如下圖中的item0這一條

20201119135307828.jpeg