How to display the default iOS 6 share action sheet with available share options?
The UIActivityViewController stated in the other answer makes this trivial. All you have to do is specify the text/image/URL that you want to share and present the activity view controller modally and iOS will automatically display all applicable sharing services. Examples:
Objective-C
- (void)shareText:(NSString *)text andImage:(UIImage *)image andUrl:(URL *)url
{
NSMutableArray *sharingItems = [NSMutableArray new];
if (text) {
[sharingItems addObject:text];
}
if (image) {
[sharingItems addObject:image];
}
if (url) {
[sharingItems addObject:url];
}
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
}
Swift
func share(sharingText: String?, sharingImage: UIImage?, sharingURL: URL?) {
let sharingItems:[AnyObject?] = [
sharingText as AnyObject,
sharingImage as AnyObject,
sharingURL as AnyObject
]
let activityViewController = UIActivityViewController(activityItems: sharingItems.compactMap({$0}), applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
activityViewController.popoverPresentationController?.sourceView = view
}
present(activityViewController, animated: true, completion: nil)
}
ios8 - how to show the default share sheet
You can use the simple activity controller to show default sharing apps using:
NSArray *activityItems = [NSArray arrayWithObjects:shareString, shareImage, shareUrl, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:activityViewController animated:YES completion:nil];
And you can use its completion handler too:
[activityViewController setCompletionHandler:^(NSString *act, BOOL done)
{
//Code here when the action performed.
}];
This will show all default sharing apps.
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:
Xcode : sharing content via Action Sheet
It is not in UIActionSheet
it is in UIActivityController
, which is the default function in iOS.
objective-C
- (void)presentActivityController:(UIActivityViewController *)controller {
// for iPad: make the presentation a Popover
controller.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:controller animated:YES completion:nil];
UIPopoverPresentationController *popController = [controller popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionAny;
popController.barButtonItem = self.navigationItem.leftBarButtonItem;
// access the completion handler
controller.completionWithItemsHandler = ^(NSString *activityType,
BOOL completed,
NSArray *returnedItems,
NSError *error){
// react to the completion
if (completed) {
// user shared an item
NSLog(@"We used activity type%@", activityType);
} else {
// user cancelled
NSLog(@"We didn't want to share anything after all.");
}
if (error) {
NSLog(@"An Error occured: %@, %@", error.localizedDescription, error.localizedFailureReason);
}
};
}
-(void)sendMessage {
//create a message
NSString *theMessage = @"Some text we're sharing with an activity controller";
NSArray *items = @[theMessage];
// build an activity view controller
UIActivityViewController *controller = [[UIActivityViewController alloc]initWithActivityItems:items applicationActivities:nil];
// and present it
[self presentActivityController:controller];
}
Swift
let shareText = "Hello, world!"
if let image = UIImage(named: "myImage") {
let vc = UIActivityViewController(activityItems: [shareText, image], applicationActivities: [])
present(vc, animated: true, completion: nil)
}
Try these links for tutorials
http://nshipster.com/uiactivityviewcontroller/
http://www.codingexplorer.com/add-sharing-to-your-app-via-uiactivityviewcontroller/
http://roadfiresoftware.com/2014/02/how-to-add-facebook-and-twitter-sharing-to-an-ios-app/
Swift
https://www.hackingwithswift.com/example-code/uikit/how-to-share-content-with-uiactivityviewcontroller
Show button on actionsheet, if IOS 6 installed
This is an answer to a follow-up question made by the OP in the comments section.
Under iOS 6+ you can use a UIActivityViewController
while under iOS 5 earlier you can still use a UIActionSheet
.
Class avcClass = NSClassFromString(@"UIActivityViewController");
if (avcClass) {
NSArray *items = @[ xxx ]; // create one or more activity item objects for this
UIActivityViewController *avc = [[avcClass alloc] initWithActivityItems:items applicationActivities:nil];
[self presentViewController:abc animated:YES completion:nil];
} else {
// create and show an action sheet
}
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 get the share sheet like dropbox
The "Open In..." seems to be a custom UIActivity (thus presented by an UIActivityViewController), which simply presents a UIDocumentInteractionController when selected.
Related Topics
Swift - Using Replacerange() to Change Certain Occurrences in a String
<Input Type="Number"/> Is Not Showing a Number Keypad on iOS
Does App Store Reject Submission If Nsallowsarbitraryloads Set to Yes
How to Dismiss Keyboard iOS Programmatically When Pressing Return
Navigating to a New Screen When Stream Value in Bloc Changes
Determine If iOS Device Is 32- or 64-Bit
Showing the File Download Progress with Nsurlsessiondatatask
Fade Out Scrolling Uitextview Over Image
Uibutton Does Not Work When It in Uiscrollview
Reverse an Audio File Swift/Objective-C
Scrolling Slow on Mobile/iOS When Using Overflow:Scroll
Nsdateformatter: Date According to Currentlocale, Without Year
Building iOS Applications Using Xcodebuild Without Codesign
Disable Swipe Back Gesture in Swift
How to Combine Two Dictionary Instances in Swift