0

For some reason that I don't understand, the Action Extension Button (in Share menu) doesn't respond. Action extension, at this point, catches the URL from Safari (where it was launched from) to make some things after. As a layer between Web and extension there is JS file (maybe something wrong here, i just copied it)

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 File:

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;
Dmitry S.
  • 1,544
  • 2
  • 13
  • 22

1 Answers1

0

Finally, you should change a content of override func viewDidLoad to

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 is ok!