MKaro
MKaro

Reputation: 156

How to share selected text with my application?

I would like to make my app appear in the UIActivityViewController for text sharing, like in Mail, iMessage, Notes, Gmail etc etc.

For example, when user tapback on selected text and hit the 'Share' button from any app like in the attachment:

For example, when user tapback on selected text and hit the 'Share' button like in the attachment.

I would like my app to appear in the UIActivityViewController and when the user selects my app, to launch it with the ability to handle that selected text.

So what I have tried: Search in Apple documentation.

So any solutions? Thanks!

Upvotes: 3

Views: 1188

Answers (1)

Paul B
Paul B

Reputation: 5125

Assume that you already have some app.

  1. Add Target File -> New -> Target. In the left pane, select Application Extension from the iOS section, choose Action extension, and click Next.
  2. Set the Product Name. Make sure the Action Type is Presents user interface. Click Finish.
  3. In the Project Navigator, expand the extension group and click on MainInterface.storyboard. Select the image view and replace it with UITextView. Create and bind @IBOutlet weak var to it.
  4. Select Info.plist of the extension, navigate to NSExtension -> NSExtensionAttributes -> NSExtensionActivationRule. Change the NSExtensionActivationRule's type from String to Dictionary. With the dictionary expanded, click the + button next to it. This will add a child key. Set its name to NSExtensionActivationSupportsText, its type to Boolean, and the value to YES. This ensures that the action extension is only visible when at least one input item contains text.
  5. Put this code to ActionViewController.swift:

_

import UIKit
import MobileCoreServices
class ActionViewController: UIViewController {

    @IBOutlet weak var textView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
    
        // Get the item[s] we're handling from the extension context.
        
        var textFound = false
        for item in self.extensionContext!.inputItems as! [NSExtensionItem] {
            for provider in item.attachments! {
                if provider.hasItemConformingToTypeIdentifier(kUTTypePlainText as String) {
                    // This is an plain Text.
                    weak var weakTextView = self.textView
                    provider.loadItem(forTypeIdentifier: kUTTypePlainText as String, options: nil, completionHandler: { (textItem, error) in
                        OperationQueue.main.addOperation {
                            if let strongTextView = weakTextView {
                                if let gotText = textItem as? String {
                                    strongTextView.text = gotText
                                    // do what you need with the text
                                }
                            }
                        }
                    })
                    
                    textFound = true
                    break
                }
            }
            
            if (textFound) {
                // We only handle one text, so stop looking for more. You can do as you need.
                break
            }
        }
    }

    @IBAction func done() {
        // Return any edited content to the host app.
        // This template doesn't do anything, so we just echo the passed in items.
        self.extensionContext!.completeRequest(returningItems: self.extensionContext!.inputItems, completionHandler: nil)
    }
}

Upvotes: 1

Related Questions