Share Extension is not working in Chrome

2019-03-29 11:25发布

问题:

I am working on Share Extension

Here is code of info.plist file . this is working fine in Safari, But not in Chrome.

 <key>NSExtension</key>
        <dict>
        <key>NSExtensionAttributes</key>
        <dict>
            <key>NSExtensionActivationRule</key>
            <dict>
                <key>NSExtensionActivationSupportsImageWithMaxCount</key>
                <integer>0</integer>
                <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
                <integer>1</integer>
            </dict>
        </dict> 


        <key>NSExtensionMainStoryboard</key>
        <string>MainInterface</string>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.share-services</string>
    </dict>

Any idea? how to enable share extension in Chrome as well

回答1:

You are missing some code . For chrome you need to pass js file as well

<dict>
        <key>NSExtensionAttributes</key>
        <dict>
            <key>NSExtensionActivationRule</key>
            <dict>
                <key>NSExtensionActivationSupportsText</key>
                <true/>
                <key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
                <integer>1</integer>
                <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
                <integer>1</integer>
            </dict>
            <key>NSExtensionJavaScriptPreprocessingFile</key>
            <string>DemoPreprocessor</string>
        </dict>
        <key>NSExtensionMainStoryboard</key>
        <string>MainInterface</string>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.share-services</string>
    </dict>

for more detials Please visit demo extension code from this link



回答2:

in my case, only adding the JS file with "NSExtensionJavaScriptPreprocessingFile" did not solve the problem.

<key>NSExtension</key>
    <dict>
            <key>NSExtensionAttributes</key>
            <dict>
                    <key>NSExtensionJavaScriptPreprocessingFile</key>
                    <string>Action</string>
                    <key>NSExtensionActivationRule</key>
                    <dict>
                            <key>NSExtensionActivationSupportsText</key>
                            <true/>
                            <key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
                            <integer>1</integer>
                    </dict>
            </dict>
            <key>NSExtensionMainStoryboard</key>
            <string>MainInterface</string>
            <key>NSExtensionPointIdentifier</key>
            <string>com.apple.share-services</string>
    </dict>

It's also essential to add the :

<key>NSExtensionActivationSupportsText</key>
<true/>

I currently don't know why.

I've found this in the official documentation : NSExtensionActivationSupportsText : Include this key to indicate to the system and to other apps that your app supports text.

Thanks a lot.



回答3:

only safari use this array NSItemProvider = [[NSExtensionItem attachments] firstObject]; other browser use the API NSItemProvider = [[NSExtensionItem attachments] objectAtIndex:1];



回答4:

No need to edit plist. This works both in Google Chrome and Safari:

override func viewDidLoad() {
    super.viewDidLoad()

    for item in extensionContext!.inputItems {
        if let attachments = item.attachments {
            for itemProvider in attachments! {
                itemProvider.loadItemForTypeIdentifier("public.url", options: nil, completionHandler: { (object, error) -> Void in
                    if object != nil {
                        println(object) //This is your URL
                    }
                })
            }
        }
    }
}