iOS 15.0 中已弃用“windows”

2024-04-04

我构建了一个在我的一些应用程序中适用的函数。以 pdf 文件形式共享文本。 在一个新项目(iOS15)中,我想重用这个函数,但收到了贬值的消息。我不明白应该如何将代码更改为新的 UIWindow.Scene.windows。

该消息显示在我的代码中的两个位置(此处标记为

--> 'windows' 在 iOS 15.0 中已弃用:在相关窗口场景上使用 UIWindowScene.windows

如何更改我的代码以与 iOS 15 兼容?

func sharePDF(pdf: Data) {
    
    let pdfData = pdf
    let printingDate = Datum()
    
    let temporaryFolder = FileManager.default.temporaryDirectory
    let fileName = "Scan2Clipboard " + printingDate + ".pdf"
    let temporaryFileURL = temporaryFolder.appendingPathComponent(fileName)
    
    
    
    do {
        try pdfData.write(to: temporaryFileURL)
        
        let vc = UIActivityViewController(activityItems: [temporaryFileURL], applicationActivities: nil)
        
        if UIDevice.current.userInterfaceIdiom == .pad {
            vc.popoverPresentationController?.sourceView = UIApplication.shared.windows.first <--- here
            vc.popoverPresentationController?.sourceRect = CGRect (
                x: UIScreen.main.bounds.width / 2.1,
                y: UIScreen.main.bounds.height / 2.3,
                width: 300, height: 300)
        }
        
        UIApplication.shared.windows.first?.rootViewController?.present(vc, animated: true, completion: nil) <--- here
        
    } catch {
        print(error)
    }
    
}

这已经针对 iOS 15 进行了更新

UIApplication
.shared
.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first { $0.isKeyWindow }
.rootViewController?.present()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS 15.0 中已弃用“windows” 的相关文章

随机推荐