iOS 7 中的默认共享

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19682814/
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-31 02:40:31  来源:igfitidea点击:

Default Sharing in iOS 7

iosios7sharesocial-networkingairdrop

提问by Spidy

I have seen this format (Image shown below) of share option in most of the iOS applications that support iOS 7. Is there a default code/framework available to implement this share option as it is shown in the image below?

我已经在大多数支持 iOS 7 的 iOS 应用程序中看到了这种共享选项的格式(如下图所示)。是否有默认代码/框架可用于实现此共享选项,如下图所示?

采纳答案by Abizern

What you are looking for is the UIActivityViewController.

您正在寻找的是UIActivityViewController.

Since you asked a general question I can't do more than give you a link to the documentation

既然你问了一个一般性的问题,我只能给你一个指向文档的链接

回答by sjaak bakker

In addition to the accepted answer, a small piece of example code

除了接受的答案外,还有一小段示例代码

- (void)shareText:(NSString *)text andImage:(UIImage *)image andUrl:(NSURL *)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];
    }

Call shareText, leave the things that you don't want to share at nil.

打电话shareText,把不想分享的东西留在nil

[self shareText:@"Hello world" andImage:nil andUrl:nil];

回答by elio.d

The Controller in the image you posted is the UIActivitiyViewController this is a linkto the class documentation

您发布的图像中的控制器是 UIActivitiyViewController这是类文档的链接

回答by GraehamF

some good example code: How to display the default iOS 6 share action sheet with available share options?

一些很好的示例代码: 如何使用可用的共享选项显示默认的 iOS 6 共享操作表?

I know this question is particular to iOS 7, and the code example specifies iOS 6, but AFAICT they are very similar one might find the example code as helpful as I did.

我知道这个问题是 iOS 7 特有的,并且代码示例指定了 iOS 6,但 AFAICT 它们非常相似,人们可能会发现示例代码和我一样有用。

回答by Meet Doshi

Just use following code for Default Sharing.You can able to add more items into shareItemsarray as per your requirement.

只需使用以下代码进行默认共享。您可以根据需要将更多项目添加到shareItems数组中。

NSMutableArray *shareItems = [[NSMutableArray alloc] initWithObjects: 
                                 @"Hello", 
                                 [UIImage imageNamed:@"your_image.png"], 
                                 @"http://google.com/", nil];
[self shareItemToOtherApp:shareItems];

Following method is for default sharing Text or Image into other Apps:-

以下方法用于默认将文本或图像共享到其他应用程序:-

-(void)shareItemToOtherApp:(NSMutableArray *)shareItems{
    UIActivityViewController *shareController = [[UIActivityViewController alloc]
                                                 initWithActivityItems: shareItems applicationActivities :nil];

    [shareController setValue:@"Sharing" forKey:@"subject"];
    shareController.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];

    shareController.completionHandler = ^(NSString *activityType, BOOL completed)
    {
        //NSLog(@" activityType: %@", activityType);
        //NSLog(@" completed: %i", completed);
    };

    [self presentViewController: shareController animated: YES completion: nil];
}

If you want to make Custom Sharingsheet then use following code. For this, you have to import <Social/Social.h>framework.

如果要制作自定义共享表,请使用以下代码。为此,您必须导入<Social/Social.h>框架。

-(void)shareOnFacebook:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
    {
        SLComposeViewController *faceSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        // NSLog(@"%@", messageField.text);//This returns the appropriate string
        [faceSheet setInitialText:@"Hellooooooo"];
        //The facebook VC appears, but initial text is not set to messageField.text
        [self presentViewController:faceSheet animated:YES completion:nil];
    }
    else
    {
        NSLog(@"Please first install Application and login in Facebook");
    }
}

-(void)shareOnTwitter:(id)sender {
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:SLServiceTypeTwitter];
        [tweetSheet setInitialText:@"Hello"];
        [self presentViewController:tweetSheet animated:YES completion:nil];
    }
    else{
        NSLog(@"Please first install Application and login in Twitter");
    }
}

Hope, this is what you're looking for. Any concern get back to me. :)

希望,这就是你要找的。有任何问题都可以回复我。:)

回答by Sumit utreja

UIActivityViewControlleris what you are looking for.

UIActivityViewController就是你要找的。

You can specify either the items or the applications

您可以指定项目或应用程序

UIActivityViewController *actCont = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];