KasemJaffer / receive_sharing_intent

A Flutter plugin that enables flutter apps to receive sharing photos, text and url from other apps.
Apache License 2.0
334 stars 395 forks source link

Use data from configuration options in flutter #311

Open koromkorom opened 4 months ago

koromkorom commented 4 months ago

Hi,

I only enabled text sharing and I simply want to add a title to the shared text. Here is my ShareViewController.swift. The UI part works. I can put a title and click on Post but the title is then not part of the value in ReceiveSharingIntent.instance.getMediaStream().listen((value). Can somebody please help me or provide a minimal working example of how something like this would work? Thank you!

import UIKit
import Social
import UniformTypeIdentifiers
import receive_sharing_intent

class ShareViewController: RSIShareViewController {
    var titleConfigurationItem: SLComposeSheetConfigurationItem?
    private var textString: String?
    private var titleItem: NSExtensionItem?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func shouldAutoRedirect() -> Bool {
        return false
    }

    override func isContentValid() -> Bool {
        return true
    }

    override func didSelectPost() {
        super.didSelectPost()
    }

    override func configurationItems() -> [Any]! {
        if titleConfigurationItem == nil {
            titleConfigurationItem = SLComposeSheetConfigurationItem()
            titleConfigurationItem?.title = "Title"
            titleConfigurationItem?.tapHandler = { [unowned self] in
                let alertController = UIAlertController(title: "Title", message: nil, preferredStyle: .alert)
                alertController.addTextField { textField in
                    textField.placeholder = "Enter title"
                    textField.textAlignment = .center
                }

                // A cancel action
                let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in
                    print("Canelled")
                }

                let confirmAction = UIAlertAction(title: "OK", style: .default) { _ in
                    if let text = alertController.textFields?.first?.text {
                        self.titleItem = NSExtensionItem()
                        if let titleItem = self.titleItem {
                            let itemProvider = NSItemProvider(item: text as NSSecureCoding, typeIdentifier: UTType.plainText.identifier)
                            titleItem.attachments = [itemProvider]
                        }
                    }
                }

                alertController.addAction(cancelAction)
                alertController.addAction(confirmAction)
                self.present(alertController, animated: true, completion: {
                    titleConfigurationItem?.value = alertController.textFields?.first?.text ?? "None"
                } )
                }
        }
        return [titleConfigurationItem!]
    }

}