如何在 iOS 15 上使用 UIWindowScene.windows? [重复]

问题描述 投票:0回答:1

目前,在 iOS 14.6 中,我可以使用以下代码在应用程序中调用显示共享表的函数:

func share(link: URL) {
    let activityView = UIActivityViewController(activityItems: [link], applicationActivities: nil)
    UIApplication.shared.windows.first?.rootViewController?.present(activityView, animated: true, completion: nil)
}

自 iOS 15 beta 起,Xcode 告诉我 “'windows' 在 iOS 15.0 中已弃用:在相关窗口场景上使用 UIWindowScene.windows”。我如何更新它以便我的共享表能够在这个新版本中正常工作?谢谢!

ios swift uiwindow
1个回答
103
投票

(在 Xcode 13.2.1 上运行的 iOS 15.2 进行测试)

改进Rachid的答案,这是一个Swiftier版本

extension UIApplication {
    
    var keyWindow: UIWindow? {
        // Get connected scenes
        return self.connectedScenes
            // Keep only active scenes, onscreen and visible to the user
            .filter { $0.activationState == .foregroundActive }
            // Keep only the first `UIWindowScene`
            .first(where: { $0 is UIWindowScene })
            // Get its associated windows
            .flatMap({ $0 as? UIWindowScene })?.windows
            // Finally, keep only the key window
            .first(where: \.isKeyWindow)
    }
    
}

如果您想在钥匙

UIViewController
中找到呈现的
UIWindow 
,这里还有另一个
extension
,您可能会发现有用:

extension UIApplication {
    
    var keyWindowPresentedController: UIViewController? {
        var viewController = self.keyWindow?.rootViewController
        
        // If root `UIViewController` is a `UITabBarController`
        if let presentedController = viewController as? UITabBarController {
            // Move to selected `UIViewController`
            viewController = presentedController.selectedViewController
        }
        
        // Go deeper to find the last presented `UIViewController`
        while let presentedController = viewController?.presentedViewController {
            // If root `UIViewController` is a `UITabBarController`
            if let presentedController = presentedController as? UITabBarController {
                // Move to selected `UIViewController`
                viewController = presentedController.selectedViewController
            } else {
                // Otherwise, go deeper
                viewController = presentedController
            }
        }
        
        return viewController
    }
    
}

你可以把它放在任何你想要的地方,但我个人将它添加为

extension
UIViewController

这使我可以添加更多有用的扩展,例如更轻松地呈现

UIViewController

extension UIViewController {
    
    func presentInKeyWindow(animated: Bool = true, completion: (() -> Void)? = nil) {
        DispatchQueue.main.async {
            UIApplication.shared.keyWindow?.rootViewController?
                .present(self, animated: animated, completion: completion)
        }
    }
    
    func presentInKeyWindowPresentedController(animated: Bool = true, completion: (() -> Void)? = nil) {
        DispatchQueue.main.async {
            UIApplication.shared.keyWindowPresentedController?
                .present(self, animated: animated, completion: completion)
        }
    }
    
}
© www.soinside.com 2019 - 2024. All rights reserved.