在UIActivityViewController中共享不同类型的数据

时间:2014-10-24 15:41:21

标签: ios objective-c ios7

以下帖子提出了一个非常相似的问题:Different data for sharing providers in UIActivityViewController。但我的问题不同。

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

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

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

  • Twitter上的字符串;
  • Facebook上的字符串和图像数组;
  • 使用Airdrop的图像(例如NSData)的实际数据。

P.S:

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

- (id)activityViewControllerPlaceholderItem:;

但是,我无法使用它,因为我们在这里不知道activityType的价值。

2 个答案:

答案 0 :(得分:3)

您希望创建和共享两个或多个符合UIActivityItemSource的对象,其中一个返回String,另一个返回Image,等等。然后,当请求该项的委托回调被调用时您检查选择了哪种活动类型(Facebook,Mail,AirDrop等),并且如果该项目不适用于该活动,则让一个或多个ItemSource返回nil。确保任何选定的活动至少有一个项目返回非零值。

您可以查看airdrop sample code,了解如何实施UIActivityItemSource

的示例

答案 1 :(得分:1)

对于仍然在Objective-c中寻找解决方案的任何人来说,这是为了共享不同的数据源,返回多个对象,它适用于最新的应用程序共享。在我的情况下,我想要所有itemForActivityType的图片和文字:

FIRST:创建您的UIActivityItemSource,1表示文本,1表示图像

MyShareImage.h

    @protocol giveMeImageToShare
    - (UIImage*)imageToShare;
   @end

   @interface MyShareImage : NSObject<UIActivityItemSource>

   @property (weak,nonatomic) id<giveMeImageToShare> delegate;

   @end

MyShareImage.m

  #import "MyShareImage.h"
  @implementation MyShareImage
  - (id)activityViewControllerPlaceholderItem:(UIActivityViewController*)activityViewController{
   return @"";
   }

   - (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType{
return [[self delegate] imageToShare];
   }

然后, MyShareText.h

  @protocol givemetextToShare

   - (NSString*)textToShare;

   @end

   @interface MyShareText : NSObject<UIActivityItemSource>

   @property (weak,nonatomic) id<givemetextToShare> delegate;

   @end

MyShareText.m

  @implementation MyShareText
  - (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController{
      return @"";
  }

   - (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType{
       if ([activityType containsString:@"net.whatsapp.WhatsApp.ShareExtension"]) {
          return nil;
       }
       return [[self delegate] textToShare];

}

现在是activityController:

   - (void)shareAllPossible:(id)sender withThisImage:(UIImage*)immagineShare andThisText:(NSString*)testoShare{

       immagine = immagineShare;
       testo = testoShare;
       MyShareText *myShareText = [MyShareText new];
       myShareText.delegate = self;
       MyShareImage *myShareImage = [MyShareImage new];
       myShareImage.delegate = self;
       NSAssert(immagineShare, @"The image must be loaded to share.");
       if (immagineShare) {
           UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[myShareImage ,myShareText] applicationActivities:nil];

   activityController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
        if (completed)
        {
            //NSLog(@"The Activity: %@ was completed", activityType);
                      }
        else {
            //NSLog(@"The Activity: %@ was NOT completed", activityType);
        }

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

}

希望它有所帮助。 *从https://stackoverflow.com/a/37548529/1907742获得灵感 Mchurch