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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13498459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:16:40  来源:igfitidea点击:

How to display the default iOS 6 share action sheet with available share options?

iossocial-framework

提问by openfrog

Some apps show a default action sheet in iOS 6 with sharing options.

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

Social Frameworkonly has two classes, one for composing and one for request.

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

What I found though is about composing for a particular service with SLComposeViewController and before showing this I must query by hand if the service is available. And then I also have to create my own action sheet with own icons.

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

How do those apps show this default share options action sheet in iOS 6? Or are they using an open source framework?

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

回答by Mick MacCallum

The UIActivityViewControllerstated in the other answer makes this trivial. All you have to do is specify the text/image/URL that you want to share and present the activity view controller modally and iOS will automatically display all applicable sharing services. Examples:

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

Objective-C

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

Swift

迅速

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({
-(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];
}
}), applicationActivities: nil) if UIDevice.current.userInterfaceIdiom == .pad { activityViewController.popoverPresentationController?.sourceView = view } present(activityViewController, animated: true, completion: nil) }

回答by Davide

Add this to use the UIActivityViewController.

添加此项以使用UIActivityViewController.

##代码##