xcode 使用swiftUI在iOS 15中创建共享表

lhcgjxsq  于 2022-11-18  发布在  Swift
关注(0)|答案(5)|浏览(278)

我正在尝试创建一个共享表来共享文本,它在iOS 14中工作正常,但在iOS 15中它告诉我
““windows”在iOS 15.0中已弃用:请UIWindowScene.windows在相关窗口场景上使用www.example.com”。
我如何使它在iOS 15上与SwiftUI一起工作

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

ivqmmu1c1#

我认为最好直接使用SwiftUI API。一般来说,我会遵循以下步骤。
1.创建名为ActivityView的SwiftUI View,它符合UIViewControllerRepresentable。这将允许您将UIActivityViewController引入SwiftUI。
1.创建一个Identifiable结构来包含你要在ActivityView中显示的文本。创建此类型将允许你使用SwiftUI sheet API并利用SwiftUI状态来通知应用何时显示新的ActivityView
1.创建一个可选的@State变量,用于保存Identifiable文本构造。当此变量更改时,sheet API将执行回调。
1.点击按钮后,更新步骤3中设置的变量的状态。
1.使用sheet API创建将呈现给用户的ActivityView
下面的代码应该可以帮助您入门。

import UIKit
import SwiftUI

// 1. Activity View
struct ActivityView: UIViewControllerRepresentable {
    let text: String

    func makeUIViewController(context: UIViewControllerRepresentableContext<ActivityView>) -> UIActivityViewController {
        return UIActivityViewController(activityItems: [text], applicationActivities: nil)
    }

    func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ActivityView>) {}
}

// 2. Share Text
struct ShareText: Identifiable {
    let id = UUID()
    let text: String
}

struct ContentView: View {
    // 3. Share Text State
    @State var shareText: ShareText?
    
    var body: some View {
        VStack {
            Button("Show Activity View") {
                // 4. New Identifiable Share Text
                shareText = ShareText(text: "Hola 😀")
            }
            .padding()
        }
        // 5. Sheet to display Share Text
        .sheet(item: $shareText) { shareText in
            ActivityView(text: shareText.text)
        }
    }
}
oiopk7p5

oiopk7p52#

未来,iOS 16将提供ShareLink视图,其工作原理如下:

Gallery(...)
   .toolbar {
      ShareLink(item: image, preview: SharePreview("Birthday Effects"))
   }

来源:https://developer.apple.com/videos/play/wwdc2022/10052/
时间代码偏移:二十五分二十八秒

vu8f3i0k

vu8f3i0k3#

要避免出现警告,请更改检索窗口场景的方式。请执行以下操作:

Button {
   let TextoCompartido = "Hola 😀 "
                                
   let AV = UIActivityViewController(activityItems: [TextoCompartido], applicationActivities: nil)
    
   let scenes = UIApplication.shared.connectedScenes
   let windowScene = scenes.first as? UIWindowScene
    
   windowScene?.keyWindow?.rootViewController?.present(AV, animated: true, completion: nil)

}
bvjveswy

bvjveswy4#

您可以使用中的答案尝试以下操作: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")
        }
    }
}
qoefvg9y

qoefvg9y5#

在iOS 15中测试,带SwiftUI

func shareViaActionSheet() {
        if vedioData.vedioURL != nil {
            let activityVC = UIActivityViewController(activityItems: [vedioData.vedioURL as Any], applicationActivities: nil)
            UIApplication.shared.currentUIWindow()?.rootViewController?.present(activityVC, animated: true, completion: nil)
        }
    }

要避免iOS 15方法过时警告,请使用此扩展

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
    }
}

相关问题