iosswiftshare-extension

Accessing Website URL and Title in ShareExtension


I'm trying to access the url and title of the page the user is currently on via a ShareExtension to my app.

After setting everything up I can't seem to get the code inside the completionHandler to run.

        if let item = extensionContext?.inputItems.first as? NSExtensionItem {
        if let attachments = item.attachments as? [NSItemProvider] {

            for attachment: NSItemProvider in attachments {

                print(attachment)

                    attachment.loadItem(forTypeIdentifier: kUTTypePropertyList as String, options: nil, completionHandler: {(dict, error) in


                        let itemDictionary = dict as! NSDictionary
                        let javaScriptValues = itemDictionary[NSExtensionJavaScriptPreprocessingResultsKey] as! NSDictionary
                        print(javaScriptValues)
                    })

            }
        }
    }

Having dug deeper I know the code executes as far as the for loop. The attachment is of type PropertyList.

enter image description here

However the loadItem(forIdentifier:options:completionHandler:) method seems to never run, even more so the code inside it.

Has anyone encountered this issue? I'm sure I'm missing something but cannot figure it out.


Solution

  • Change your "loadItem" to "loadItemAsync" and run it that way. I ran into literally the EXACT same problem of the completion block not running, but when I call "loadItemAsync" instead of just "loadItem," it runs fine and I get all the output from my JS file.