Search code examples
iosswiftios-extensions

iOS Share Extension not displaying/not able to receive URL


I'm having a problem with my app's iOS Share Extension. I have the following code in the Share Extension Info.plist.

<key>NSExtension</key>
<dict>
    <key>NSExtensionAttributes</key>
    <dict>
        <key>NSExtensionActivationRule</key>
        <dict>
            <key>NSExtensionActivationSupportsWebPageWithMaxCount</key>
            <integer>1</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>

The major problem is the extension doesn't show up in Google Chrome, NYTimes, WSJ, and many other apps. All of which should provide either a Web Page or Web URL.

I read somewhere that I have to add the following to the NSExtensionActivationRule dict.

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

If I add then the share extension appears in the apps listed above but just returns a string to my extension. For example if I go to apple.com in Google Chrome and use my share extension it just provides Apple as a string to my extension which my app doesn't really support. It should be providing some type of URL or something along those lines. And in the NYTimes app it would just send the headline of the article to the extension.

The extension works perfectly in Safari. Just not certain 3rd party applications.

Below is the main code for receiving the URL in the swift file of my extension.

if let item = extensionContext?.inputItems.first as? NSExtensionItem {
    if let itemProvider = item.attachments?.first as? NSItemProvider {
        if itemProvider.hasItemConformingToTypeIdentifier("public.url") {
            itemProvider.loadItem(forTypeIdentifier: "public.url", options: nil, completionHandler: { (url, error) -> Void in
                if let shareURL = url as? NSURL {
                    print (shareURL.absoluteString!)

From there I handle the URL as my application should.

When I added the NSExtensionActivationSupportsText option I changed the code above slightly from public.url to public.text along with a few other things to test it and it was just returning super unhelpful strings.

I know this is possible to get the URL from those applications because even in NYTimes it has an option for Google Chrome, which defiantly needs a URL and not just the article headline. Any ideas on how to get that URL?


Solution

  • I would suggest you to use this:

        <key>NSExtension</key>
    <dict>
        <key>NSExtensionAttributes</key>
        <dict>
            <key>NSExtensionActivationRule</key>
            <string>
      SUBQUERY (
          extensionItems,
          $extensionItem,
          SUBQUERY (
              $extensionItem.attachments,
              $attachment,
              ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url"
          ).@count == 1
      ).@count == 1
            </string>
        </dict>
        <key>NSExtensionMainStoryboard</key>
        <string>MainInterface</string>
        <key>NSExtensionPointIdentifier</key>
        <string>com.apple.share-services</string>
    </dict>
    

    Considering your code to get the attachments, you are checking just first inputItems and first attachment, you need to have foreach loop in both cases and check all of them.