What I want
I\'m trying to achieve the following user flow:
I've spent an embarrassingly large part of an afternoon reading the docs on this and trying different permutations of extensions, as I was looking to do exactly (I think) what you were trying to do.
I've concluded that this exact flow cannot be achieved on iOS. If the user selects text and uses the context menu (i.e. "Copy", "Look Up", "Share"...), the only thing your extension will ever receive is an NSItemProvider
with the text that they selected, i.e. not a plist with the results of the preprocessing javascript. When they select Share from that menu, the extension shows up if and only if you've got NSExtensionActivationSupportsText
set to YES
in the extension's Info.plist
file.
In order to run the preprocessing javascript, an extension has to have NSExtensionActivationSupportsWebPageWithMaxCount
set to a value greater than 0, per the docs. If an extension gets called via the selected text context menu, that javascript file never runs.
However, it's possible to get pretty close to the desired flow. If the user is in Safari, and selects some text, and then, instead of tapping "Share" in the context menu, taps the Share icon at the bottom of the Safari UI, then the NSItemProvider
comes back as a plist and the NSExtensionJavaScriptPreprocessingFile
gets run. My javascript file looks like the following:
var Share = function() {};
Share.prototype = {
run: function(arguments) {
arguments.completionFunction({"URL": document.URL, "selectedText": document.getSelection().toString()});
},
finalize: function(arguments) {
// alert shared!
}
};
var ExtensionPreprocessingJS = new Share
which means that the plist object returned to the extension has both the URL of the page and the selectedText.
If the only purpose of an extension is the share URLs, and plain text without a URL isn't a sensical use case, you should probably not have NSExtensionActivationSupportsText
set to YES
. For example, an app like Pocket has it enabled, but if a user selects some text in Safari and then tries to share via the context menu, Pocket can't do anything meaningful with just the plaintext and no page URL, so it just pops up a pretty cryptic error message.
I've published the code for my extension as well if you want to have a look.
Swift 3
Try something along the lines of:
override func didSelectPost() {
if let item = extensionContext?.inputItems.first as? NSExtensionItem,
let itemProvider = item.attachments?.first as? NSItemProvider,
itemProvider.hasItemConformingToTypeIdentifier("public.url") {
itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil) { (url, error) in
if let shareURL = url as? URL {
// do what you want to do with shareURL
}
self.extensionContext?.completeRequest(returningItems: [], completionHandler:nil)
}
}
}
"public.url"
can be replaced with the kUTTypeURL
String imported from MobileCoreServices