Search code examples
swiftuiuiactivityviewcontroller

SwiftUI ActivityViewController uses stale activityItems array - workarounds?


Trying to figure out how to use UIActivityViewController in SwiftUI.

When the user taps "Share...", the action clears the items array, assigns a fresh value to it ("Use this, please!"), and then sets the isPresenting boolean to true (thereby causing the UIActivityVC to show).

The problem is this: the 1st tap on the "Share..." button ignores the clearing & assignment of fresh info - and instead shows the initializing value of items ("Why isn't this cleared out?")

The 2nd tap on "Share..." works as expected.

Here's a tiny example that demonstrates the question...

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

And to top it all off, I get a horrendous amount of error text about not being able to map databases, etc - none of which makes any sense to me... 🙃

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

What am I missing???


Solution

  • Make the array an

    @Binding var activityItems: [Any]
    

    In the

    ActivityViewController