How to show your iOS Share Extension only in Safari (or other browsers)
Posted: | Author: Jörn | Filed under: iOS | Tags: Extension, iOS8 | 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.
1
Hitusaid atIt is not working on Chrome, it works on Safari. Any idea for the same ?
2
Jörnsaid atHi 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 atNice Article,it really helped
4
Shivanisaid atI 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 atHi 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 atI 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 atHi 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 atthank you very much