How to add multiple email attachments using UIActivityItemProvider

337 views Asked by At

I'm working on an iOS app in which I want to add MULTIPLE attachments to an email using UIActivityItemProvider. I want to do it using UIActivityItemProvider because I do not want to incur the overhead of processing the record before I display the UIActivtyViewController to the user. If I pass in one image using an NSData or an NSURL object to the "url" element of the returned object, then the inline image shows fine. If I pass in an array of these objects then nothing shows up. I believe that passing an array will work if I use the ActivityItems parameter when initializing an NSActivityViewController, but again, I do not want to do this because I want to take advantage of the delayed processing available by using the UIActivityItemProvider. Below is my code

@implementation NoteRecordActivityProvider

- (id)initWithPlaceholderItem:(id)placeholderItem
{
    //Initializes and returns a provider object with the specified placeholder data
    return [super initWithPlaceholderItem:placeholderItem];
}

- (id)item
{
//    //Generates and returns the actual data object
    NSData *imageFile = [[NSData alloc]init];
    NSString *imageFileName;
    NSURL *url;
    NSString* exportPath;
    NSMutableArray* imageArray = [[NSMutableArray alloc]initWithCapacity:0];
    NSInteger photoCount = self.noteRecord.photoCount;
    for (NSInteger i = 0; i < photoCount; i+=1)
    {
        //Add File Attachment
        PhotoObject *po = (PhotoObject*)[self.noteRecord photoObjects:i];
        NSString *photoGUID = [(PhotoObject*)[self.noteRecord photoObjects:i]GUID];
        imageFile = ImageDataReturningMethodHere;
        imageFileName = [[NSArray arrayWithObjects:@"Image", [NSString stringWithFormat:@"%ld", (long)i], @".png", nil] componentsJoinedByString:@""];
        exportPath = [[FileSystemProvider exportPath] stringByAppendingPathComponent:imageFileName];
        [imageFile writeToFile:exportPath atomically:YES];
        url = [NSURL fileURLWithPath:exportPath];
        [imageArray addObject:url];
    }
    if ([self.activityType isEqualToString:UIActivityTypeMail])
        return imageArray;
    else
        return nil;
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    return @{@"body":@"", @"url":[[NSURL alloc]init]};
}

-(NSString *) activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
    return [NSString stringWithFormat:@"Attached Record:  %@", self.noteRecord.title];
}

@end
1

There are 1 answers

0
KeithTheBiped On

I did find the answer to this question. First I created an PhotoAttachmentActivityProvider that had a property for the source document which contains the photo I wanted to attach, and an index to the attachment in that document. I'm pasting my code here which uses a custom document called a NoteRecord:

@interface EMailPhotoAttachmentItemProvider : UIActivityItemProvider
@property (nonatomic, readwrite) NSInteger photoIndex;
@property (nonatomic, strong) NoteRecord* noteRecord;
@end

Then when I am showing the UIActivityViewController I add 1 of these custom UIActivityItemProvider objects for each attachment:

for (NSInteger i = 0; i < self.noteRecord.photoCount;  i++)
{
    EMailPhotoAttachmentItemProvider* photoProvider =     [[EMailPhotoAttachmentItemProvider alloc]initWithPlaceholderItem:@{@"body":textToShare, @"url":url}];
    photoProvider.photoIndex = i;
    photoProvider.noteRecord = self.noteRecord;
    [activityProviders addObject:photoProvider];
}

//Initialize the ActivityViewController
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityProviders applicationActivities:applicationActivities];

Then in the custom UIActivityItemProvider I check for whether I'm processing a EMAIL, and then I create a URL for the image using the document and image index provided:

#import "EMailPhotoAttachmentItemProvider.h"
#import "MiscUtilities.h"
#import "FileSystemProvider.h"

@implementation EMailPhotoAttachmentItemProvider

- (id)initWithPlaceholderItem:(id)placeholderItem
{
    //Initializes and returns a provider object with the specified placeholder data
    return [super initWithPlaceholderItem:placeholderItem];
}

- (id)item
    {    
    if ([self.activityType isEqualToString:UIActivityTypeMail])
    {
        // Code here gets the image file from the NoteRecord at the PhotoIndex provided to  
        // the UIActivityItemProvider at the imageIndex, creates a URL for that image and returns it here.  
        // Your implementation will vary

        PhotoObject *po = (PhotoObject*)[self.noteRecord photoObjects:self.photoIndex];
        NSString *photoGUID = [(PhotoObject*)[self.noteRecord photoObjects:self.photoIndex]GUID];
        NSData *imageFile = [[[MiscUtilities getApplicationDelegate]imageProvider]imageDataWithCaptionFromGUID:photoGUID caption:po.caption maxResolution:600];
        NSString *imageFileName = [[NSArray arrayWithObjects:@"Image", [NSString stringWithFormat:@"%ld", (long)self.photoIndex], @".png", nil] componentsJoinedByString:@""];
        imageFileName = [[FileSystemProvider exportPath] stringByAppendingPathComponent:imageFileName];
        [imageFile writeToFile:imageFileName atomically:YES];
        NSURL *url = [NSURL fileURLWithPath:imageFileName];
        return url;
    }
    else
    {
        return nil;
    };

}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    NSString* defaultImagePath = [[FileSystemProvider imagePath]stringByAppendingPathComponent:@"default.png"];
    NSURL *url = [[NSURL alloc]initWithString:defaultImagePath];
    return @{@"body":@"", @"url":url};
}
@end