问题
There is a extremely similar question asked by the following post: Different data for sharing providers in UIActivityViewController. But my question is different.
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:
- a string on Twitter;
- a array of an string and and image on Facebook;
- the actual data of the image (e.g. NSData) with Airdrop.
P.S.:
I also looked at the following protocol function:
- (id)activityViewControllerPlaceholderItem:;
However, I cannot use it because we don't know the value of activityType
here.
回答1:
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
回答2:
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
来源:https://stackoverflow.com/questions/26551408/share-data-with-different-types-in-uiactivityviewcontroller