change - iOS 7中的默认共享




webview document title (4)

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



UIActivityViewController是你在找什么。

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

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

只需使用以下代码进行默认共享。 您可以根据您的要求将更多项目添加到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");
    }
}

希望,这是你要找的。 任何关心回到我身边。 :)


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

- (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 ,留下你不想分享的东西。

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




airdrop