【问题标题】:Create a share sheet in iOS 15 with swiftUI使用 swiftUI 在 iOS 15 中创建共享表
【发布时间】:2021-12-21 19:30:51
【问题描述】:

我正在尝试创建一个共享表来共享文本,它在 iOS 14 中运行良好,但在 iOS 15 中它告诉我

“'windows' 在 iOS 15.0 中已被弃用:在相关的窗口场景中使用 UIWindowScene.windows”。

如何使用 SwiftUI 使其在 iOS 15 上运行

   Button {
    let TextoCompartido = "Hola ???? "
                                    
    let AV = UIActivityViewController(activityItems: [TextoCompartido], applicationActivities: nil)
        
UIApplication.shared.windows.first?.rootViewController?.present(AV, animated: true, completion: nil)
    
    }

【问题讨论】:

标签: xcode swiftui share ios15


【解决方案1】:

您可以使用来自How to get rid of message " 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead" with AdMob banner?的答案尝试以下操作

请注意,您的代码适用于我,但编译器会发出弃用警告。

public extension UIApplication {
    func currentUIWindow() -> UIWindow? {
        let connectedScenes = UIApplication.shared.connectedScenes
            .filter({
                $0.activationState == .foregroundActive})
            .compactMap({$0 as? UIWindowScene})
        
        let window = connectedScenes.first?
            .windows
            .first { $0.isKeyWindow }

        return window
        
    }
}

struct ContentView: View {
    let TextoCompartido = "Hola ? "
    
    var body: some View {
        Button(action: {
            let AV = UIActivityViewController(activityItems: [TextoCompartido], applicationActivities: nil)
 
 UIApplication.shared.currentUIWindow()?.rootViewController?.present(AV, animated: true, completion: nil)
            
// This works for me, but the compiler give the deprecation warning
// UIApplication.shared.windows.first?.rootViewController?.present(AV, animated: true, completion: nil)
        }) {
            Text("Hola click me")
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 2021-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多