Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

social framework - How to display the default iOS 6 share action sheet with available share options?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...