Reputation: 156
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.
So any solutions? Thanks!
Upvotes: 3
Views: 1188
Reputation: 5125
Assume that you already have some app.
@IBOutlet
weak var to it.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._
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