Adding an open In option to the share sheet in ObjectiveC
The "Open in…" options are made available based on the content that's being opened and the document types supported by the device's installed apps. It's not something you can alter from your app.
On the other hand, if the apps that you want to appear in your app's "Open in" list are other apps you have control over, then you can add CFBundleDocumentTypes
keys to those apps' info.plists. This tells iOS that the apps can open certain types of files. You specify supported filetypes using UTIs like public.jpeg
and so on, and when a user tries to open one of those file types from a UIActivityViewController, ios will include the supporting applications in the options.
Here's a good discussion of that: How do I get my application to show up in the Open in... menu
And here's a discussion on UTIs: Uniform Type Identifier Concepts
So, for example, if one of your apps supported opening jpeg images and any text file, you would add this to the info.plist:
<array>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>Images</string>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.jpeg</string>
</array>
</dict>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>text</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>public.plain-text</string>
</array>
</dict>
</array>
But, again, if you're trying to manipulate the apps that are shown as "Open in" options from the app displaying the activity controller, you can't. You can only make your own apps advertise that they support certain document types, and from that iOS builds up its "Open in" options.
How to add extra options to iOS Share Sheet like Safari does
When you initialise a share sheet (aka UIActivityController
), you are asked for an array of applicationActivities
, that is where your custom internal/share actions will go.
You should just subclass UIActivity
, and override the required members as specified by the documentation:
activityType
activityTitle
activityImage
canPerform(withActivityItems:)
prepare(withActivityItems:)
activityCategory
To make your UIActivity
appear as a menu button at the bottom of the share sheet, make sure you return .action
for activityCategory
. If you return .share
, it will appear as a square button at the top.
Example:
// dummy implementation
class Foo: UIActivity {
override var activityTitle: String? { "Foo" }
override var activityType: UIActivity.ActivityType? { UIActivity.ActivityType("Foo") }
override var activityImage: UIImage? { UIImage(systemName: "doc.on.doc.fill") }
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
true
}
override class var activityCategory: UIActivity.Category { .action }
override func prepare(withActivityItems activityItems: [Any]) {
print("Preparing Foo!")
}
override func perform() {
print("Performed Foo!")
}
}
...
let shareSheet = UIActivityViewController(activityItems: [stuffToShare], applicationActivities: [Foo()])
Output:
If I use .share
instead:
My app is not showing up in Share Sheet
There was mistake in My Plist file that's why was facing such issue.
Posting this answer just because may be in future if someone will do such mistake so my answer could help out!
<key>NSExtension</key>
<dict>
<key>NSExtensionAttributes</key>
<dict>
<key>NSExtensionActivationRule</key>
<dict>
<key>NSExtensionActivationSupportsText</key>
<true/>
<key>NSExtensionActivationSupportsWebURLWithMaxCount</key>
<integer>1</integer>
<key>NSExtensionActivationSupportsImageWithMaxCount</key>
<integer>1</integer>
</dict>
</dict>
<key>NSExtensionMainStoryboard</key>
<string>MainInterface</string>
<key>NSExtensionPointIdentifier</key>
<string>com.apple.share-services</string>
</dict>
This is working perfectly fine!
Swift Remove app from the share sheet action
let activityViewController = UIActivityViewController(activityItems: items,
applicationActivities: nil)
activityViewController.excludedActivityTypes = UIActivity.ActivityType.postToFacebook
Related Topics
Swift - How to Get Last Taken 3 Photos from Photo Library
How to Change Color of Uitableviewcell When Selecting
Our App Crashed in iOS 9 Which Upload by Xcode 10
How to Add a Toolbar to the Bottom of a Uitableviewcontroller in Storyboards
Error When Import Zlib in iOS: Symbol(S) Not Found Collect2: Ld
Accidentally Removed Xcassets File from Xcode Project
Create and Store PDF Document Programmatically Using Swift for iOS
Drop Cap with Nsattributedstring
Reverse Geocode Location in Swift
How to Get the 3D View of UI in Xcode 6
Installation Failed "Invalid Argument" When Trying to Run Today Application Extension
Removing Badge from iOS App Icon
Does Non-Renewing Subscription Requires a Restore Button
How to Create Entitlement.Plist File in Xcode
iOS 6 Xcode 4.5 Unsupported Architecture Armv7S