How do I use UIActivityItemProvider to send an email with attachment with UIActivityViewController?

RawMean picture RawMean · Dec 14, 2013 · Viewed 21.7k times · Source

I am trying to use UIActivityItemProvider to share a file from within my app via email attachment. I also need to populate the subject line of the email and to specify the name of the attachment to be something different than the name of the file stored on the device.

Here is the code that I'm using. The problem is that the attachment is missing from the email.

@interface ItemProvider:UIActivityItemProvider
@property (nonatomic, strong) NSURL *filepath;
@property (nonatomic, strong) NSString *emailBody;
@property (nonatomic, strong) NSString *emailSubject;
@end

@implementation ItemProvider

- (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
    return [NSDictionary dictionary];
}

// The following are two methods in the UIActivityItemSource Protocol
// (UIActivityItemProvider conforms to this protocol) - both methods required
#pragma mark UIActivityItemSource

//- Returns the data object to be acted upon. (required)
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{


    if ([activityType isEqualToString:UIActivityTypeMail]) {
        return @{@"body":self.emailBody, @"url":self.filepath};
    }


    return @{@"body":self.emailBody, @"url":self.filepath};
}

//- Returns the placeholder object for the data. (required)
//- The class of this object must match the class of the object you return from the above method
- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    return @{@"body":self.emailBody, @"url":self.filepath};
}

-(NSString *) activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
    return self.emailSubject;
}

@end

And then in my viewController I do this:

      ItemProvider *provider = [[ItemProvider alloc] initWithPlaceholderItem:@{@"body":emailBody, @"url":filePath}];
    provider.emailBody = emailBody;
    provider.emailSubject = info.title;
    provider.filepath = filePath;
    NSArray *activityItems = @[provider];

    // Build a collection of custom activities (if you have any)
//    NSMutableArray *customActivities = [[NSMutableArray alloc] init];


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

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

Answer

dbart picture dbart · May 30, 2014

For those still stumbling upon a solution for this, there is a more elegant solution for customizing UIActivityViewController. To address the original question, the reason the attachment is not showing up is because it is supposed to be a separate UIActivityItemProvider object.

So the solution is to create two UIActivityItemProvider subclasses, one to wrap the 'emailBody' and 'emailSubject' and another to wrap the attachment. The benefit to using a UIActivityItemProvider for the attachment is that you have the opportunity to delay processing the attachment until it is needed, rather than doing so before presenting UIActivityViewController.

Implement the AttachmentProvider class to provide the attachment like so:

@implementation AttachmentProvider : UIActivityItemProvider

- (id)item {
    if ([self.activityType isEqualToString:UIActivityTypeMail]) {

        /* Replace with actual URL to a file. Alternatively
         * you can also return a UIImage.
         */

        return [NSData dataWithContentsOfURL:dataURL];
    }
    return nil;
}

@end

Implement EmailInfoProvider class to provider the email body and subject class like so:

@implementation EmailInfoProvider : UIActivityItemProvider

- (id)item {
    return @"Your email body goes here";
}

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
    if ([activityType isEqualToString:UIActivityTypeMail]) {
        return @"Your subject goes here";
    }
    return nil;
}

@end

You can then create a UIActivityViewController with both these items in your viewController like so:

- (void)shareAction {

    AttachmentProvider *attachment  = [[AttachmentProvider alloc] init];
    EmailInfoProvider *emailContent = [[EmailInfoProvider alloc] init];

    // You can provider custom -(id)init methods to populate EmailInfoProvider

    UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[attachment, emailContent] applicationActivities:nil];
    [self presentViewController:activityController animated:YES completion:nil];
}