UIActivityViewController Gmail Share subject and body going empty? UIActivityViewController Gmail Share subject and body going empty? ios ios

UIActivityViewController Gmail Share subject and body going empty?


[_activityViewController setValue:subject forKey:@"subject"]; Is undocumented way to set subject of email.
Correct way to set body and subject (iOS 7.0 and later) - implement UIActivityItemSource protocol on item to share.

//  EmailItemProvider.h@interface EmailItemProvider : NSObject <UIActivityItemSource>@property (nonatomic, strong) NSString *subject;@property (nonatomic, strong) NSString *body;@end//  EmailItemProvider.m#import "EmailItemProvider.h"@implementation EmailItemProvider- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {    return _body;}- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {    return _body;}- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {    return _subject;}@end

And than present it:

EmailItemProvider *emailItem = [EmailItemProvider new];emailItem.subject = @"Subject";emailItem.body = @"Body";UIActivityViewController *activityViewController =[[UIActivityViewController alloc] initWithActivityItems:@[emailItem]                                  applicationActivities:nil];[self presentViewController:activityViewController animated:YES completion:nil];

This will set body and subject on mail app, but seems like Gmail app ignores subject and set it equal to body.

Mail.app Gmail

Important: Seems like there is a bug in Gmail App. Passing & character make message subject and body to be empty. Use & instead. Other special characters are not tested.


For Swift(4) lover :

activityViewController(_:subjectForActivityType:) allows us to put a subject to the sharing method.

extension YOURViewController: UIActivityItemSource {    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {        return "YOUR_EMAIL_BODY"    }    func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {        if activityType == .mail {            return "YOUR_EMAIL_SUBJECT"        } else {            return "SUBJECT_FOR_OTHER_SHARING_METHOD"        }    }    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {        if activityType == .mail {            return "YOUR_EMAIL_BODY"        } else {            return "OTHER_SHARING_MESSAGES"        }     }}


UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[@"Your body String to share"] applicationActivities:nil];

[activityViewController setValue:@"Your email Subject" forKey:@"subject"];

activityViewController.completionHandler = ^(NSString *activityType, BOOL completed) { // ...};


matomo