我构建了一个在我的一些应用程序中适用的函数。以 pdf 文件形式共享文本。 在一个新项目(iOS15)中,我想重用这个函数,但收到了贬值的消息。我不明白应该如何将代码更改为新的 UIWindow.Scene.windows。
该消息显示在我的代码中的两个位置(此处标记为<---)
--> iOS 15.0 中已弃用“windows”:在相关窗口场景上使用 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()
关于swift - iOS 15.0 中已弃用“windows”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70686543/