swiftuiuiactivity

Understanding Which Activity Gets Called in Sharing SwiftUI


On my share sheet I see those activities but I want to understand and fetch which activity is selected to share the url.

For example, if WhatsApp is selected I want to fetch WhatsApp selected bool somehow.

    struct ShareSheet: UIViewControllerRepresentable {
    typealias Callback = (_ activityType: UIActivity.ActivityType?, _ completed: Bool, _ returnedItems: [Any]?, _ error: Error?) -> Void
    
    let activityItems: [Any]
    let applicationActivities: [UIActivity]? = nil
    let excludedActivityTypes: [UIActivity.ActivityType]? = nil
    let callback: Callback? = nil
    
    func makeUIViewController(context: Context) -> UIActivityViewController {
        let controller = UIActivityViewController(
            activityItems: activityItems,
            applicationActivities: applicationActivities)
        controller.excludedActivityTypes = excludedActivityTypes
        controller.completionWithItemsHandler = callback
        return controller
    }
    
    func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
        // nothing to do here
    }
}

What I am trying is

 func fetchActivity() {
    if activityItems.description == "whatsapp" {
        self.service.articleShare(article_id: articlePost.article_id, share_type: "whatsapp")
    }
}

enter image description here


Solution

  •     struct ShareSheet: UIViewControllerRepresentable {
        @ObservedObject var service = ArticleService()
        var articlePost: Article
        typealias Callback = (_ activityType: UIActivity.ActivityType?, _ completed: Bool, _ returnedItems: [Any]?, _ error: Error?) -> Void
        
        let activityItems: [Any]
        let applicationActivities: [UIActivity]? = nil
        let excludedActivityTypes: [UIActivity.ActivityType]? = nil
        let callback: Callback? = nil
        func makeUIViewController(context: Context) -> UIActivityViewController {
            let controller = UIActivityViewController(
                activityItems: activityItems,
                applicationActivities: applicationActivities)
            controller.excludedActivityTypes = excludedActivityTypes
            controller.completionWithItemsHandler = callback
            controller.completionWithItemsHandler = { activity, success, items, error in
                if !success{
                    print("cancelled")
                    return
                }
                if activity == UIActivity.ActivityType.postToTwitter {
                    print("twitter")
                    print(articlePost.article_id)
                    self.service.articleShare(article_id: articlePost.article_id, share_type: "twitter")
                }
                if activity == UIActivity.ActivityType.postToFacebook {
                    print("facebook")
                    self.service.articleShare(article_id: articlePost.article_id, share_type: "facebook")
                }
                if activity == UIActivity.ActivityType.mail {
                    print("mail")
                    print(articlePost.article_id)
                    self.service.articleShare(article_id: articlePost.article_id, share_type: "mail")
                }
                if activity == UIActivity.ActivityType.message {
                }
            }
            return controller
        }
        
        func updateUIViewController(_ uiViewController: UIActivityViewController, context: Context) {
            // nothing to do here
        }
    }