简体   繁体   中英

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.

The key is in using this line in your info.plist file:

    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionJavaScriptPreprocessingFile</key>
        <string>Action</string>
        <key>NSExtensionActivationRule</key>
        ...your should be fine...
    </dict>

At NSExtensionJavaScriptPreprocessingFile you specify the name of the .js file that contains javascript file that must contain a global object named ExtensionPreprocessingJS . You should then search for the items conforming to kUTTypePropertyList in your code (and it seems that you do already that looking at your code).

This is a short list of what you should do, ask if you need something more and it is easy to find more data on the internet if you start from this too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM