As Apple explains in her guide on programming application extensions , you need to include a JavaScript file in the extension to do any preprocessing. The results of this preprocessing are available through the NSExtensionItem
extension.
A simple example of this file is included in my iOS Extension Demo project on GitHub and looks like this:
var MyPreprocessor = function() {}; MyPreprocessor.prototype = { run: function(arguments) { arguments.completionFunction({"URL": document.URL, "pageSource": document.documentElement.outerHTML, "title": document.title, "selection": window.getSelection().toString()}); } }; var ExtensionPreprocessingJS = new MyPreprocessor;
It simply extracts various details about the current page and passes their completionFunction
. ExtensionPreprocessingJS
var at the end is the hook that the extension infrastructure is looking for.
In the extension, you can get these values ββin the dictionary by kUTTypePropertyList
an element of type kUTTypePropertyList
:
for (NSExtensionItem *item in self.extensionContext.inputItems) { for (NSItemProvider *itemProvider in item.attachments) { if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypePropertyList]) { [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypePropertyList options:nil completionHandler:^(NSDictionary *jsDict, NSError *error) { dispatch_async(dispatch_get_main_queue(), ^{ NSDictionary *jsPreprocessingResults = jsDict[NSExtensionJavaScriptPreprocessingResultsKey];
source share