SwiftUI ActivityViewController 使用陈旧的 activityItems 数组 - 解决方法?

SwiftUI ActivityViewController uses stale activityItems array - workarounds?

正在尝试了解如何在 SwiftUI 中使用 UIActivityViewController。

当用户点击“分享...”时,该操作会清除项目数组,为其分配一个新值(“请使用这个!”),然后将 isPresenting 布尔值设置为 true(从而导致要显示的 UIActivityVC)。

问题是这样的:第一次点击“分享...”按钮会忽略新信息的清除和分配 - 而是显示项目的初始化值(“为什么不清除这个?” )

第二次点击“分享...”按预期工作。

这是一个演示问题的小例子...

import SwiftUI

struct ContentView: View {
    @State private var isSharePresented: Bool = false
    @State var items: [Any] = ["Why isn't this cleared out?"]

    var body: some View {
        Button("Share...") {
            items = []                      //  Why doesn't this clear out the items array?
            items = ["Use this, please!"]   //  Why is this string used only on the 2nd button press?
            print("share - items=\(items)")
            self.isSharePresented = true
        }
        .sheet(isPresented: $isSharePresented, onDismiss: {
            print("Dismiss")
        }, content: {
            ActivityViewController(activityItems: items)
        })
    }
}
import UIKit
import SwiftUI

struct ActivityViewController: UIViewControllerRepresentable {
    var activityItems: [Any]
    var applicationActivities: [UIActivity]? = nil
    @Environment(\.presentationMode) var presentationMode
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<ActivityViewController>) -> UIActivityViewController {
        print("ActivityViewController.makeUIViewController() - activityItems=\(activityItems)")
        let controller = UIActivityViewController(activityItems: activityItems, applicationActivities: applicationActivities)
        return controller
    }
    
    func updateUIViewController(_ uiViewController: UIActivityViewController, context: UIViewControllerRepresentableContext<ActivityViewController>) {}
}

最重要的是,我收到了大量关于无法映射数据库等的错误文本 - none 其中对我来说很有意义...

share - items=["Use this, please!"]
ActivityViewController.makeUIViewController() - activityItems=["Why isn\'t this cleared out?"]
2022-01-14 10:36:59.087584-0600 ShareBot[14256:5211305] [default] LaunchServices: store (null) or url (null) was nil: Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=264, _LSFunction=-[_LSDReadClient getServerStoreWithCompletionHandler:]}
2022-01-14 10:36:59.087674-0600 ShareBot[14256:5211305] [default] Attempt to map database failed: permission was denied. This attempt will not be retried.
2022-01-14 10:36:59.087728-0600 ShareBot[14256:5211305] [db] Failed to initialize client context with error Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=264, _LSFunction=-[_LSDReadClient getServerStoreWithCompletionHandler:]}
2022-01-14 10:36:59.139279-0600 ShareBot[14256:5211305] [default] -imageForImageDescriptor: can do IO please adopt -imageForDescriptor: for IO free drawing or -prepareImageForDescriptor: if IO is allowed. (This will become a fault soon.)
2022-01-14 10:36:59.228125-0600 ShareBot[14256:5211305] <CATransformLayer: 0x2836dcc40> - changing property backgroundColor in transform-only layer, will have no effect
2022-01-14 10:36:59.306430-0600 ShareBot[14256:5211305] [default] LaunchServices: store (null) or url (null) was nil: Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=264, _LSFunction=-[_LSDReadClient getServerStoreWithCompletionHandler:]}
2022-01-14 10:36:59.306506-0600 ShareBot[14256:5211305] [default] Attempt to map database failed: permission was denied. This attempt will not be retried.
2022-01-14 10:36:59.306559-0600 ShareBot[14256:5211305] [db] Failed to initialize client context with error Error Domain=NSOSStatusErrorDomain Code=-54 "process may not map database" UserInfo={NSDebugDescription=process may not map database, _LSLine=264, _LSFunction=-[_LSDReadClient getServerStoreWithCompletionHandler:]}
2022-01-14 10:36:59.306744-0600 ShareBot[14256:5211305] [default] -imageForImageDescriptor: can do IO please adopt -imageForDescriptor: for IO free drawing or -prepareImageForDescriptor: if IO is allowed. (This will become a fault soon.)
2022-01-14 10:36:59.321436-0600 ShareBot[14256:5211305] [LayoutConstraints] Changing the translatesAutoresizingMaskIntoConstraints property of a UICollectionReusableView that is managed by a UICollectionView is not supported, and will result in incorrect self-sizing. View: <_UIActivityContentFooterView: 0x103829a20; baseClass = UICollectionReusableView; frame = (16 378.333; 358 52); layer = <CALayer: 0x2836d9400>>

我错过了什么???

使数组成为

@Binding var activityItems: [Any]

ActivityViewController