应用程序扩展(操作扩展)未打开



由于一些我不理解的原因,操作扩展按钮(在共享菜单中(没有响应。在这一点上,操作扩展从Safari(它是从哪里启动的(捕获URL,以便在之后进行一些操作。作为Web和扩展之间的一层,有一个JS文件(可能这里有问题,我只是复制了它(

ViewController:

class ActionViewController: UIViewController {
var SafariURL: NSURL!
override func viewDidLoad() {
super.viewDidLoad()

let extensionItem = extensionContext?.inputItems.first as? NSExtensionItem
let itemProvider = extensionItem!.attachments?.first as? NSItemProvider

let propertyList = String(kUTTypePropertyList)
if itemProvider!.hasItemConformingToTypeIdentifier(propertyList) {
print("I'm here2")
itemProvider!.loadItem(forTypeIdentifier: propertyList, options: nil, completionHandler: { (item, error) -> Void in
let dictionary = item as? NSDictionary
OperationQueue.main.addOperation {
let results = dictionary![NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary
let urlString = results!["currentUrl"] as? String
self.SafariURL = NSURL(string: urlString!)
}
})
} else {
print("error")
}
}
@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)
}

JS文件:

var GetURL = function() {};
GetURL.prototype = {

run: function(arguments) {
arguments.completionFunction({ "currentUrl" : document.URL });
},

finalize: function(arguments) {
var message = arguments["statusMessage"];

if (message) {
alert(message);
}
}

};
var ExtensionPreprocessingJS = new GetURL;

最后,您应该将override func viewDidLoad的内容更改为

super.viewDidLoad()
if let inputItem = extensionContext?.inputItems.first as? NSExtensionItem {
if let itemProvider = inputItem.attachments?.first {
itemProvider.loadItem(forTypeIdentifier: kUTTypePropertyList as String) { [self] (dict, error) in
guard let itemDictionary = dict as? NSDictionary else { return }
guard let javaScriptValues = itemDictionary[NSExtensionJavaScriptPreprocessingResultsKey] as? NSDictionary else { return }
self.Pageurl = javaScriptValues["URL"] as? String ?? ""

JS还好!

相关内容

最新更新