Tuesday, 2 April 2019

How to get a iOS share extension to get web page content from Safari instead of URL

I have a working share extension written in swift. When I test a share operation from Safari, I always only get the URL type (kUTTypeURL).

What I want is to get some form of rendered version of what the user is looking at (PDF or HTML?). Using the URL and opening it in a webview is not workable due to authentication issues, etc.

I've tried many different activation rules with no change. Here is the current one I am using:

            <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsFileWithMaxCount</key>
            <integer>1</integer>
            <key>NSExtensionActivationSupportsImageWithMaxCount</key>
            <integer>20</integer>
            <key>NSExtensionActivationSupportsMovieWithMaxCount</key>
            <integer>0</integer>
            <key>NSExtensionActivationSupportsText</key>
            <true/>
            <key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
            <integer>1</integer>
            <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
            <integer>1</integer>
        </dict>

My controller looks like this - when run from Safari, it always only has one attachment type - the URL:

   override func didSelectPost() {


    if let item = extensionContext?.inputItems.first as? NSExtensionItem {
        if let attachments = item.attachments {
            for attachment: NSItemProvider in attachments {
                if attachment.hasItemConformingToTypeIdentifier(kUTTypePropertyList as String) {
                    attachment.loadItem(forTypeIdentifier: kUTTypePropertyList as String, options: nil, completionHandler: { (data, error) in
                        // Do stuff with this content now
                        self.extensionContext?.completeRequest(returningItems: [], completionHandler:nil)
                    })
                }
                if attachment.hasItemConformingToTypeIdentifier(kUTTypeURL as String) {
                    attachment.loadItem(forTypeIdentifier: kUTTypeURL as String, options: nil, completionHandler: { (url, error) in
                        if let shareURL = url as? NSURL {
                            // Do stuff with your URL now.
                        }
                        self.extensionContext?.completeRequest(returningItems: [], completionHandler:nil)
                    })
                }
            }
        }
    }
}

Other approaches I've seen use a javascript file to walk the DOM but have seen no good examples and I'm not clear on if this would help me in any case.



from How to get a iOS share extension to get web page content from Safari instead of URL

No comments:

Post a Comment