99

一些应用程序在 iOS 6 中显示带有共享选项的默认操作表。

Social Framework只有两个类,一个是作曲类,一个是请求类。

我发现虽然是关于使用 SLComposeViewController 为特定服务编写内容,但在显示此内容之前,我必须手动查询该服务是否可用。然后我还必须使用自己的图标创建自己的操作表。

这些应用程序如何在 iOS 6 中显示此默认共享选项操作表?还是他们使用开源框架?

4

2 回答 2

224

另一个答案中所述的UIActivityViewController使这变得微不足道。您所要做的就是指定要共享的文本/图像/URL,并以模态方式呈现活动视图控制器,iOS 将自动显示所有适用的共享服务。例子:

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

迅速

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)
}
于 2012-11-21T17:54:47.733 回答
41

添加这个以使用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];
}
于 2013-03-08T15:24:36.053 回答