Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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];
like image 478
RawMean Avatar asked Dec 14 '13 08:12

RawMean


2 Answers

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];
}
like image 199
dbart Avatar answered Sep 30 '22 02:09

dbart


i'm sending email with attachment without ItemProvider. its working well :-)

NSMutableArray *selDocs = [[NSMutableArray alloc] init];
for (Document *theDoc in self.selectedDocs) {
     NSURL *fileUrl = [NSURL fileURLWithPath:theDoc.filePath];       
    [selDocs addObject:fileUrl];
}
NSArray *postItems = [NSArray arrayWithArray:selDocs];

UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:postItems applicationActivities:nil];
[avc setValue:@"Your email Subject" forKey:@"subject"];

avc.completionHandler = ^(NSString *activityType, BOOL completed){
    NSLog(@"Activity Type selected: %@", activityType);
    if (completed) {
        NSLog(@"Selected activity was performed.");
    } else {
        if (activityType == NULL) {
            NSLog(@"User dismissed the view controller without making a selection.");
        } else {
            NSLog(@"Activity was not performed.");
        }
    }
};

[self presentViewController:avc animated:YES completion:nil];
like image 31
thorb65 Avatar answered Sep 30 '22 01:09

thorb65