'windows' 在 iOS 15.0 中被弃用

'windows' was deprecated in iOS 15.0

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

该消息出现在我的代码中的两个位置(此处标记为 <---)

--> 'windows' 在 iOS 15.0 中被弃用:在相关的 window 场景中使用 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 { [=10=] as? UIWindowScene }
.flatMap { [=10=].windows }
.first { [=10=].isKeyWindow }
.rootViewController?.present()