Share data with different types in UIActivityViewController

后端 未结 2 1887
慢半拍i
慢半拍i 2021-01-03 05:05

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

I

相关标签:
2条回答
  • 2021-01-03 05:41

    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.

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

    0 讨论(0)
  • 2021-01-03 06:04

    For anyone still looking for a solution in objective-c, this is for sharing different datasources, returning more than one object, and it works with whats'app share. In my case I wanted both picture and text for all itemForActivityType:

    FIRST: create your UIActivityItemSource, 1 for text, and 1 for the image

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

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

    }

    And now the 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];
    }
    

    }

    Hope it helps. * got inspiration from https://stackoverflow.com/a/37548529/1907742 Mchurch

    0 讨论(0)
提交回复
热议问题