UIActivityViewController Gmail Share subject and body going empty?

Ankit Jain picture Ankit Jain · Apr 21, 2015 · Viewed 8.6k times · Source

Im using UIActivityViewController to show share. In the list when i select Mail app the subject and body is set properly and where as in Gmail app its empty.

- (void)shareAVideoWithSubject:(NSString*)subject Link:(NSString *)string onViewController:(UIViewController *)viewController fromView:(UIView *)view {

    _activityViewController =
    [[UIActivityViewController alloc] initWithActivityItems:@[string]
                                      applicationActivities:nil];
    _activityViewController.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypeAddToReadingList, UIActivityTypeAirDrop];
    [_activityViewController setValue:subject forKey:@"subject"];


    UIWindow *window = [[[UIApplication sharedApplication] delegate]window];

    //if iPhone
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        if(!viewController){
            viewController = ((SWRevealViewController*)window.rootViewController).presentedViewController;
        }
        [viewController presentViewController:_activityViewController
                                     animated:YES
                                   completion:nil];

    }   
    //if iPad
    else
    {
        // Change Rect to position Popover
        popup = [[UIPopoverController alloc] initWithContentViewController:_activityViewController];
        UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:view];
        [popup presentPopoverFromBarButtonItem:barButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    }
}

I have check the below two questions on StackOverFlow.

  1. UIActivityViewController not showing body text in gmail

  2. UIActivityViewController not showing body text in gmail

These are not answer to my question because they add up new activity in list, rather i want the iOS shows the all shareable apps. In that case Gmail share body is going empty.

Thanks in advance. Adding the screenshots, UIActivityViewController showing Gmail app to share

Gmail window open with empty subject and body. Cropped the image because of company policy can show the email address

Answer

Sergey Kuryanov picture Sergey Kuryanov · Apr 28, 2015

[_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 &amp; instead. Other special characters are not tested.