在iOS上没有调用sceneDidEnterBackground,swift

Posted

技术标签:

【中文标题】在iOS上没有调用sceneDidEnterBackground,swift【英文标题】:sceneDidEnterBackground is not called on iOS, swift 【发布时间】:2021-03-05 14:05:21 【问题描述】:

我正在使用协调器模式实现 ios 应用程序,当应用程序进入后台时,我需要显示一些隐私屏幕。

class SceneDelegate: UIResponder, UIWindowSceneDelegate 

    var window: UIWindow?
    var coordinator: MainCoordinator?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) 
        guard let scene = scene as? UIWindowScene else  return 
    
        if let registry = DependencyResolver.shared as? DependencyRegistry 
            DependencyGraph.setup(for: registry)
        
    
        let navController = UINavigationController()
        navController.setNavigationBarHidden(true, animated: false)
        coordinator = MainCoordinator(navigationController: navController)
        coordinator?.start()
        self.window = UIWindow.init(windowScene: scene)
        window?.rootViewController = navController
        window?.makeKeyAndVisible()
    

    func sceneWillEnterForeground(_ scene: UIScene) 
        hidePrivacyProtectionWindow()
    

    func sceneDidEnterBackground(_ scene: UIScene) 
        showPrivacyProtectionWindow()
    

    ...

    // MARK: Privacy Protection
    private var privacyProtectionWindow: UIWindow?

    private func showPrivacyProtectionWindow() 
        guard let windowScene = self.window?.windowScene else 
            return
        

        privacyProtectionWindow = UIWindow(windowScene: windowScene)
        let storyboard = UIStoryboard(name: "LaunchScreen", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "splash")
        privacyProtectionWindow?.rootViewController = vc
        privacyProtectionWindow?.windowLevel = .alert + 1
        privacyProtectionWindow?.makeKeyAndVisible()
    

    private func hidePrivacyProtectionWindow() 
        privacyProtectionWindow?.isHidden = true
        privacyProtectionWindow = nil
    

在进入时,sceneWillEnterForeground() 被调用。在进入后台时,不会调用 sceneDidEnterBackground()。我错过了什么?

【问题讨论】:

【参考方案1】:

在发布问题后 10 分钟内自己发现,我最初使用了错误的方法。这将适用于我想要实现的目标:

func sceneDidBecomeActive(_ scene: UIScene) 
    hidePrivacyProtectionWindow()


func sceneWillResignActive(_ scene: UIScene) 
    showPrivacyProtectionWindow()

【讨论】:

以上是关于在iOS上没有调用sceneDidEnterBackground,swift的主要内容,如果未能解决你的问题,请参考以下文章

在 UIBarButtonItem 上调用 setTitleTextAttributes 对 iOS 11 没有任何影响

Armadillo 在 iOS 上调用内部加速库

在 iOS 上使用 Swift 多次调用 Firebase 'Observe'

在 iOS 13 上未调用 didConnectPeripheral 委托

没有在 MacCatalyst 上调用 didReceiveRemoteNotification

UIButton没有在ios7上注册