iOS 7中的默认共享设置

55

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


请查看这个教程:http://www.appcoda.com/ios7-airdrop-programming-tutorial/ - Sakiboy
6个回答

53
你要找的是 UIActivityViewController
由于你提出的是一个普遍性问题,我只能给你提供一个指向文档的链接。

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

4
您发布的图片中的控制器是UIActivityViewController,这是一个链接到类文档。

3

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");
    }
}

希望这是您正在寻找的内容。如有任何疑虑,请回复我。 :)

1

UIActivityViewController 是您所寻求的。

您可以指定要分享的项目或应用程序。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接