How to display the default iOS 6 share action sheet with available share options?
Asked Answered
X

2

99

Some apps show a default action sheet in iOS 6 with sharing options.

Social Framework only has two classes, one for composing and one for request.

What I found though is about composing for a particular service with SLComposeViewController and before showing this I must query by hand if the service is available. And then I also have to create my own action sheet with own icons.

How do those apps show this default share options action sheet in iOS 6? Or are they using an open source framework?

Xiaoximena answered 21/11, 2012 at 17:10 Comment(3)
i want to open share sheet with custom icon like wechat, weibo, qq and integrate the functionality of it, how can i do this?Francophile
Just check this link also.. https://mcmap.net/q/218384/-default-sharing-in-ios-7Schaaff
Here is a basic example using SwiftOnto
C
224

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)
}
Chavannes answered 21/11, 2012 at 17:54 Comment(5)
Nice edit for the Swift version. I'd change the .append(item) for a += item. Looks more swift-yExcruciation
In the latest version of Swift, replace AnyObject[]() with [AnyObject]()Heir
Its Working, only after logging in into respective accounts in settings(facebook/twitter), But In photos app/ Notes app, the options are available even if the user is not logged in, Could you please explain, why it is so? Or there is some other workaround?Gleeson
I've added an edit for iOS 8 support on iPad – you need to also configure the UIActivityViewController's UIPopoverController or it'll crash on present…. I've had two rejections from three on the edit so far, so assuming it's rejected, perhaps you could see if you want to add the edit yourself. Or not :-)Lap
Oh, the edit cites this answer which explains all.Lap
P
41

Add this to use the UIActivityViewController.

-(IBAction)shareButtonPressed:(id)sender {

    NSLog(@"shareButton pressed");

    NSString *texttoshare = _txt; //this is your text string to share
    UIImage *imagetoshare = _img; //this is your image to share
    NSArray *activityItems = @[texttoshare, imagetoshare];    
    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    activityVC.excludedActivityTypes = @[UIActivityTypeAssignToContact, UIActivityTypePrint];
    [self presentViewController:activityVC animated:TRUE completion:nil];
}
Protostele answered 8/3, 2013 at 15:24 Comment(1)
Preferred answer to me since you also show excluding.Analogue

© 2022 - 2024 — McMap. All rights reserved.