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 n
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];
}
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];