How to share selected text with my application?

五迷三道 提交于 2020-12-06 05:38:16

问题


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:

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.

  • Searched for relevant UTI but I understood that the UTIs are used only for files and not for a simple NSString (Correct me if I'm wrong).
  • I have tried to implement Share Extension, but this is not the solution that i want, I don't need the post popup and moreover than that I need to launch my app after sharing like in Mail, Notes, iMessage does (Regarding to Apple documentation we can't launch the contain app through Share extension only through Today extension).
  • Of course, I have searched a lot in StackOverFlow.

So any solutions? Thanks!


回答1:


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


来源:https://stackoverflow.com/questions/44994932/how-to-share-selected-text-with-my-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!