Sunday, July 16, 2017

UIActivityViewController unable to set subject when sharing to Gmail app

Leave a Comment

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.

1 Answers

Answers 1

[_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]; 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment