PIXEL
DOCK

I like the smell of Swift in the morning…

How to show your iOS Share Extension only in Safari (or other browsers)

Posted: | Author: | Filed under: iOS | Tags: , | 8 Comments »

When you develop a Share Extension for your iOS App many times the Extension is not suitable for all places where it could be shown to the user. For example if your Share Extension can only share URLs it does not make sense to offer it to the user when he is using the Photos App.

This is why Apple implemented the NSExtensionActivationRule field in the Extension’s Info.plist. That’s quite straightforward and not exactly rocket science, but still many tutorials on Extension do not mention this and it took me a while to find out how this works, so I’ll share this with you.

So, when you create a new Extension Xcode creates a Info.plist in the ‘YOUR_EXTENSION/Supporting Files’ folder. In there you’ll find a Dictionary with the key NSExtension.

NSExtension

    NSExtensionAttributes
    
        NSExtensionActivationRule
        TRUEPREDICATE
    
    NSExtensionMainStoryboard
    MainInterface
    NSExtensionPointIdentifier
    com.apple.share-services

The interesting part is the String with the key NSExtensionActivationRule. By default Xcode sets its value to TRUEPREDICATE. That’s just for development purposes and makes sure that your Extension is shown as a sharing option everywhere. This has to be removed before you submit your app the the AppStore of your app will be rejected.

So in our case we can only share an URL so we want to make sure that it does not get added to the sharing options in Apps that do not offer an URL to share (like the Photos App).

So you have to change the type of the NSExtensionActivationRule field from String to Dictionary and add one or more keys that Apple provides to define what data types your Extension supports:

NSExtensionActivationSupportsAttachmentsWithMaxCount
NSExtensionActivationSupportsAttachmentsWithMinCount
NSExtensionActivationSupportsFileWithMaxCount
NSExtensionActivationSupportsImageWithMaxCount
NSExtensionActivationSupportsMovieWithMaxCount
NSExtensionActivationSupportsText
NSExtensionActivationSupportsWebURLWithMaxCount
NSExtensionActivationSupportsWebPageWithMaxCount

In our case we only support URLs so we need the NSExtensionActivationSupportsWebURLWithMaxCount key. So the NSExtension Dictionary should look like this:

NSExtension

    NSExtensionAttributes
    
        NSExtensionActivationRule
	
	    NSExtensionActivationSupportsWebURLWithMaxCount
	    1
	
    
    NSExtensionMainStoryboard
    MainInterface
    NSExtensionPointIdentifier
    com.apple.share-services

And now your Share Extension is not activated in the Photo App anymore. Only in apps that offer a URL will now show your Share Extension as one of the sharing options.

8 Comments

  • 1

    Hitusaid at

    It is not working on Chrome, it works on Safari. Any idea for the same ?

  • 2

    Jörnsaid at

    Hi Hitu,

    thanks for your comment. I have not tried Chrome but pleas have look at this SO question: http://stackoverflow.com/questions/31855779/share-extension-is-not-working-in-chrome-ios8

    Hope this helps,

    Jörn

  • 3

    Shivanisaid at

    Nice Article,it really helped

  • 4

    Shivanisaid at

    I have used NSExtensionActivationRule as
    NSExtensionActivationSupportsFileWithMaxCount
    NSExtensionActivationSupportsImageWithMaxCount
    NSExtensionActivationSupportsMovieWithMaxCount and tried to share office file like word(.docx) PowerPoint presantation but my application is not added in share extension share menu. Share Extension is working fine for videos and Images.
    Do i need to do anything extra to share office suite files like word and excel.

  • 5

    Jörnsaid at

    Hi Shivani,
    I have never tried to share office suite files, so I am afraid I cannot help you on this topic.
    Best regards,
    Jörn

  • 6

    Muthusaid at

    I can able to share 5 photos from Photos app to my app. But I can’t able to share any image from Safari browser. My app is not visible (I have set NSExtensionActivationRuleImageWithMaxCount as 5)

    When I set NSExtensionActivationRule to be TRUEPREDICATE, I am able to get the images from Safari and also from Photos app. In this case, any number of photos are shared which is not required in my case

  • 7

    Jörnsaid at

    Hi Muthu,

    You have to also add NSExtensionActivationSupportsWebURLWithMaxCount to your NSExtensionActivationRule Dictionary and set it to 1. Then your extension will also be visible in Safari.

    Best regards,
    Jörn

  • 8

    Tulzsaid at

    thank you very much


Leave a Reply