在UIActivityViewController中共享不同类型的数据 [英] Share data with different types in UIActivityViewController

查看:114
本文介绍了在UIActivityViewController中共享不同类型的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下帖子提出了一个非常相似的问题:不同的数据在UIActivityViewController中共享提供程序。但我的问题不同。

There is a extremely similar question asked by the following post: Different data for sharing providers in UIActivityViewController. But my question is different.

我知道如何使用 itemForActivityType 分享具有不同活动的相同类型的不同不同数据。例如:

I know how to share different different data of the same type with different activities by using itemForActivityType. For example:

- (id) activityViewController:(UIActivityViewController *)activityViewController
          itemForActivityType:(NSString *)activityType
{
    if ( [activityType isEqualToString:UIActivityTypePostToTwitter] )
       return @"This is a #twitter post!";
    if ( [activityType isEqualToString:UIActivityTypePostToFacebook] )
       return @"This is a facebook post!";
    if ( [activityType isEqualToString:UIActivityTypeAirDrop] )
       return @"Airdrop message text";
    else
       return nil;
}

然而,我的问题是:如果我有不同类型的数据与不同的活动分享,我该怎么办?。例如,如果我想分享:

However, my question is: what if I have different kind of data to share with different activities, what should I do?. For example, what if I would like to share:


  • Twitter上的字符串;

  • a Facebook上的字符串和图像数组;

  • 使用Airdrop的图像的实际数据(例如NSData)。

PS:

我还查看了以下协议功能:

I also looked at the following protocol function:

- (id)activityViewControllerPlaceholderItem:;

然而,由于我们不知道的价值,我无法使用它activityType 此处。

However, I cannot use it because we don't know the value of activityType here.

推荐答案

您想要创建和共享两个或多个符合 UIActivityItemSource ,其中一个返回字符串,另一个图像,等等。然后,当调用请求项目的委托回调时,检查选择了哪种活动类型(Facebook,Mail,AirDrop等),如果该项目没有,则让ItemSource的一个或多个返回nil'适用于该活动。确保任何选定的活动至少有一个项目返回非零值。

You'd want to create and share two or more objects that conform to the UIActivityItemSource, where one returns the String, another one an Image, etc. Then when the delegate callback requesting the item is called you check which activity type was selected (Facebook, Mail, AirDrop, etc) and have one or multiple of the ItemSource's return nil if that item doesn't apply to that activity. Make sure for any chosen activity that at least one of the item's return a non-nil value.

您可以查看 airdrop示例代码以获取一些示例实现 UIActivityItemSource

You can take a look at the airdrop sample code to get some examples of how to implement UIActivityItemSource

这篇关于在UIActivityViewController中共享不同类型的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆