我希望能够共享选定的文本,但我的扩展仅在单击共享图标时出现(然后它使用页面标题填充字段)。我希望我的扩展在用户选择文本并单击“共享...”时出现(如下图所示),然后我希望它用所选文本填充文本区域。
共享视图控制器:
override func viewDidLoad() {
super.viewDidLoad()
customPopup()
let extensionItem = extensionContext?.inputItems.first as! NSExtensionItem
let itemProvider = extensionItem.attachments?.first as! NSItemProvider
let propertyList = String(kUTTypePropertyList)
if itemProvider.hasItemConformingToTypeIdentifier(propertyList) {
itemProvider.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
guard let dictionary = item as? NSDictionary else { return }
OperationQueue.main.addOperation {
if let results = dictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary {
print("RESULTS: \n", results)
}
}
})
} else {
print("error")
}
}
Action.js(JS 预处理)
var MyPreprocessor = function() {};
MyPreprocessor.prototype = {
run: function(arguments) {
arguments.completionFunction({"URL": document.URL, "title": document.title, "selection": window.getSelection().toString()});
}
};
var ExtensionPreprocessingJS = new MyPreprocessor;
信息列表