standard way to implement share as function

2019-07-07 05:15发布

问题:

I need to implement a "share as..." function in iOS. For example a button names "share as..." and popup a dialog which includes items like Email, SMS, Facebook, Twitter. I wonder if there have a standard dialog do this job.

回答1:

After searching, I found a way seems quite "standard" way in iOS6 by using UIActivityViewController.

Following is the description from developer.apple.com:

The UIActivityViewController class is a standard view controller that you can use to offer various services from your application. The system provides several standard services, such as copying items to the pasteboard, posting content to social media sites, sending items via email or SMS, and more. Apps can also define custom services.

And following is a dialog I managed to produce by using UIActivityViewController

And following is the source code I use:

NSArray *activityItems = [NSArray arrayWithObjects: share_text.text, share_image.image , nil];

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
    [self presentViewController:activityController animated:YES completion:nil];


回答2:

In your ViewController.m :

NSString *message = @"The Flyer http://flyerdream.tumblr.com";
UIImage *image = [UIImage imageNamed:@"flyer"];
NSArray *arrayOfActivityItems = [NSArray arrayWithObjects:message, image, nil];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
                                         initWithActivityItems:arrayOfActivityItems applicationActivities:nil];

[self.navigationController presentViewController:activityVC animated:YES completion:nil];


回答3:

After using UIActivityViewController, I believe it is a much better solution than what I've posted below. I've left my answer because the individual sharing methods may be of use to someone.

This is how I've implemented share features in my projects:

Email: Use MFMailComposeViewController. See documentation here: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html

Facebook & Twitter: Use SLComposeViewController. See documentation here: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html

Example:

// prepare and present SLComposeViewController if service is available
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
    SLComposeViewController *socialPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [socialPost setInitialText:@"TEXT FOR POST"];
    [socialPost addImage:[UIImage imageNamed:@"yourImage.png"]];

    [self presentViewController:socialPost animated:YES completion:nil];
}

// set up a completion handler
[socialPost setCompletionHandler:^(SLComposeViewControllerResult result) {
    switch (result) {
        case SLComposeViewControllerResultDone:
            // post was completed
            break;
        case SLComposeViewControllerResultCancelled:
            // post was cancelled
            break;
        default:
            break;
    }
}

Note: If you want to post for Twitter, replace SLServiceTypeFacebook with SLServiceTypeTwitter in the code sample above.



标签: ios share