55

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

4

6 回答 6

53

您正在寻找的是UIActivityViewController.

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

于 2013-10-30T12:59:00.260 回答
16

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

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

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

[self shareText:@"Hello world" andImage:nil andUrl:nil];
于 2016-01-25T10:35:05.860 回答
4

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

于 2013-10-30T12:59:24.767 回答
3

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

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

于 2014-05-21T17:24:12.937 回答
1

UIActivityViewController就是你要找的。

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

UIActivityViewController *actCont = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
于 2013-10-30T13:10:55.183 回答
1

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

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

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

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

如果要制作自定义共享表,请使用以下代码。为此,您必须导入<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");
    }
}

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

于 2016-02-08T10:12:44.990 回答