swift - iOS 15.0 中已弃用“windows”

标签 swift uikit

我构建了一个在我的一些应用程序中适用的函数。以 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/

相关文章:

iphone - 是否可以说名称包含 new、alloc、create 或 copy 的方法不会自动释放它们创建的对象?

ios - Google 登录错误 `swift 3`

arrays - 如何根据名称数组过滤数组?

ios - 如何在 dispatch_async 中调试当前队列?

ios - 垂直居中 View 、自动调整大小和 iPhone 5 4"屏幕

ios - UISegmentedControl iOS 13 清晰配色

ios - 是否可以在 UIPageViewController 中以编程方式翻页?

objective-c - 识别透明 UITableView 背后的手势

ios - iPad 上的 UIPopoverPresentationController

ios - 没有那个文件或目录,NSFilePath=~/Library