简体   繁体   中英

ios8 - how to show the default share sheet

I want to present and use the default "share to other service" sheet in order to allow sharing to twitter, facebook, email, etc.

I can't figure out how to show this view from my app - how can i do that?

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.

Here is a solution for a "share" popup triggered from a UIBarButtonItem , that works on both iPhone and iPad:

// "Share" action
- (IBAction)share:(UIBarButtonItem *)sender {
    NSString* title = "Content Title";
    NSString* link = "http://example.com/content.url";
    NSArray* dataToShare = @[title, link];

    UIActivityViewController* activityViewController =
    [[UIActivityViewController alloc] initWithActivityItems:dataToShare
                                      applicationActivities:nil];


    // This is key for iOS 8+
    activityViewController.popoverPresentationController.barButtonItem = sender; 

    [self presentViewController:activityViewController
                       animated:YES
                     completion:^{}];
}

Here is one simple example:

let activityViewController = UIActivityViewController(activityItems: ["Share Me!"], applicationActivities: nil)
activityViewController.completionWithItemsHandler = { [weak self] activityType, completed, returnedItems, activityError in
  // Note: you won't need returnedItems in most cases 
  // It is included in this example for completion's sake
}
present(activityViewController, animated: true, completion: nil)

Full documentation of the completion handler is available here: https://developer.apple.com/documentation/uikit/uiactivityviewcontrollercompletionwithitemshandler

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM