17

I see via sharing content from other apps that it is possible to set a different subject and body when using share sheet to share into the Gmail Mail app. I have implemented it and it works fine on the native mail app but not Gmail.

Going into Yelp and sharing a business then choosing gmail from the share sheet, I see that the subject and body are different. The subject contains the address of the business while the body contains the address + a link to the business on Yelp.

I have tried to replicate this logic with success on the native Mail app but not in the Gmail app.

I have tried the following:

Implementing UIActivityItemSource methods

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[self] applicationActivities:nil];

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController {
    return @"";
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType {   
    return @"body";
}

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType {
    return @"subject";
}

Result

Apple Mail Subject set to "subject", Body set to "body"

Gmail Subject set to "body", Body set to "body"

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType 

Is never called when sharing into the Gmail app.

I then try the more hack way of doing it

UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[@"body"] applicationActivities:nil];
[activityViewController setValue:@"subject" forKey:@"subject"];

Result

Apple Mail Subject set to "subject", Body set to "body"

Gmail Subject set to "body", Body set to "body"

Any way to make Gmail Behave like Apple Mail?

Again, I have seen that other applications like Yelp and Safari have gotten the proper behavior out of the Gmail app through share sheet. Any advice would be appreciated, thanks.

Kris Gellci
  • 9,539
  • 6
  • 40
  • 47

1 Answers1

-3

[_activityViewController setValue:subject forKey:@"subject"];-Not supported way.

Correct way to set body and subject (iOS 7.0 and later) - implement UIActivityItemSource protocol on item to share.

//  EmailDataProvider.h

@interface EmailItemProvider : NSObject <UIActivityItemSource>

@property (nonatomic, strong) NSString *subject;
@property (nonatomic, strong) NSString *body;

@end


//  EmailDataProvider.m

    @implementation EmailDataProvider

- (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:

EmailDataProvider *emailItem = [[EmailDataProvider alloc]init];

emailItem.subject = @"This is Subject text.";

emailItem.body = @"This is Body,set by programatically";

UIActivityViewController *activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:@[emailItem]
                                  applicationActivities:nil];

[self presentViewController:activityViewController animated:YES completion:nil];
Community
  • 1
  • 1
Sri Divya
  • 25
  • 3
  • In my question, I post that I already tried the UIActivityItemSource protocol and it was not working for the gmail app, working for the native mail app only. Did you try the implementation yourself while sharing with the Gmail app? – Kris Gellci Jul 05 '16 at 21:27
  • Latest Outlook Appin downloaded iOS 11 sets subject and body as empty. – Pavan Mar 04 '19 at 09:17