1

I am trying to share images via UIActivityViewController similar to this. However if I share several images, Twitter and Facebook will disappear in the UIActivityViewController.

Is there a way to share one image for both Twitter and Facebook, several images for mail as attachment?

EDIT:

// return different string depends on the type
CustomActivityItemProvider *textProvider = [[CustomActivityItemProvider alloc] initWithText:textContent url:url title:textTitle];

NSMutableArray *applicationActivities = [NSMutableArray array];
NSMutableArray *activityItems = [@[
                                   textProvider,
                                   image,
                                   url
                                   ] mutableCopy];

// custom applicationActivities
...

// If add multiple images, facebook and twitter will not show up
for(int i = 0; i < [images count]; ++i)
{
    if(images[i] != image) [activityItems addObject:images[i]];
}

UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems
                                                                                 applicationActivities:applicationActivities];

[activityController setValue:textTitle forKey:@"subject"];
activityController.excludedActivityTypes = excludeActivities;

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

Is there a way similar to the UIActivityItemProvider?

EES
  • 1,584
  • 3
  • 20
  • 38

1 Answers1

0

A similar way to textActivityItemProvider I've end up using:

ImageActivityItemProvider.h

#import <UIKit/UIKit.h>

@interface ImageActivityItemProvider : UIActivityItemProvider

@property (nonatomic, strong, readonly) UIImage *image;
@property (nonatomic, readonly) NSInteger index;
@property (nonatomic, readonly) NSInteger shouldShowIndex;

- (instancetype)initWithImage:(UIImage*)image index:(NSInteger)index shouldShowIndex:(NSInteger)shouldShowIndex;

@end

ImageActivityItemProvider.m

#import "ImageActivityItemProvider.h"

@interface ImageActivityItemProvider ()

@property (nonatomic, strong) UIImage *image;
@property (nonatomic) NSInteger index;
@property (nonatomic) NSInteger shouldShowIndex;

@end

@implementation ImageActivityItemProvider

- (instancetype)initWithImage:(UIImage*)image index:(NSInteger)index shouldShowIndex:(NSInteger)shouldShowIndex
{
    // make sure the placeholder is nil instead of the image
    self = [super initWithPlaceholderItem:nil];
    if (self)
    {
        self.image = image;
        self.index = index;
        self.shouldShowIndex = shouldShowIndex;
    }
    return self;
}

- (id)item
{
    if (
        [self.activityType isEqualToString:UIActivityTypeMail] ||
        self.index == self.shouldShowIndex
        )
    {
        return self.image;
    }
    return self.placeholderItem;
}

@end
EES
  • 1,584
  • 3
  • 20
  • 38