ios8 - 如何显示默认共享表

时间:2014-11-17 23:55:08

标签: ios

我想呈现并使用默认"分享到其他服务"表格,以便分享到Twitter,Facebook,电子邮件等

我无法弄清楚如何从我的应用中显示此视图 - 我该怎么做?

3 个答案:

答案 0 :(得分:20)

您可以使用简单活动控制器显示默认共享应用程序:

 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];

你也可以使用它的完成处理程序:

[activityViewController setCompletionHandler:^(NSString *act, BOOL done)
     {
               //Code here when the action performed.

     }];

这将显示所有默认共享应用。

答案 1 :(得分:3)

以下是从UIBarButtonItem触发的“共享”弹出式解决方案,适用于iPhone和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:^{}];
}

答案 2 :(得分:0)

这是一个简单的例子:

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)

此处提供完整处理程序的完整文档: https://developer.apple.com/documentation/uikit/uiactivityviewcontrollercompletionwithitemshandler