如何使用可用的共享选项显示默认的iOS 6共享操作表?

时间:2012-11-21 17:10:51

标签: ios social-framework

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

Social Framework只有两个类,一个用于撰写,一个用于请求。

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

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

2 个答案:

答案 0 :(得分:223)

另一个答案中陈述的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)
}

答案 1 :(得分:39)

添加此项以使用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];
}