25

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

Community
  • 1
  • 1
Ankit Jain
  • 1,858
  • 22
  • 35
  • Could you please elaborate more? The activity view shows a gmail tab? – Ashraf Tawfeeq Apr 21 '15 at 08:05
  • Hi Ashraf, Yes activity tab is show in the UIActivityViewController and on tap of it Gmail mail window opens with empty subject and body. Will attach the screenshots if possible. – Ankit Jain Apr 21 '15 at 08:23
  • That's because Gmail might be using a different key for the subject. Can you check to see if Gmail sharing works in other apps? – Gasim Apr 27 '15 at 14:52
  • I have tested Gmail sharing with Notes and Safari app, it works fine. And other apps i have not seen Gmail option. – Ankit Jain Apr 28 '15 at 06:54
  • @AnkitJain, How did you show Gmail app icon in sharing screen? are you customised Gmail with UIActivity class or is there any simple way that we can show by just writing less code? can you please help me on it. – Ganesh G Nov 30 '16 at 12:30

3 Answers3

13

[_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.

Sergey Kuryanov
  • 6,114
  • 30
  • 52
  • Thanks for the reply. The reason why Gmail body is going empty for me is I'm sharing the URL. And i have tried to share the URL as NSString and NSUrl both has failed in my case. – Ankit Jain Apr 28 '15 at 13:14
  • You can share url string in body in sample code above. – Sergey Kuryanov Apr 28 '15 at 14:37
  • Thanks @Sergey In my url query contains '&' symbol which is causing problem. Just for reference "http://www.host.com/path/query?name=XYZ&age=14" So in query component name and age is separated by '&' because of which its going empty. Suggest me the way to replace/escape that character. – Ankit Jain Apr 28 '15 at 14:46
  • Try to replace `&` symbol with `&` – Sergey Kuryanov Apr 28 '15 at 14:59
  • 3
    @SergeyKuryanov even though I use $amp; instead of & , I get body text repeated 2 times in body text area and also repeated in subject . Any solution for that? – Pooja Shah Jun 24 '15 at 12:23
  • I think you should post new question with your code example. – Sergey Kuryanov Jun 24 '15 at 16:43
  • @SergeyKuryanov writes: "Important: Seems like there is a bug in Gmail App. Passing `&` character make message subject and body to be empty." Does anyone know if this bug has been reported to the Gmail app developers? Don't immediately see relevant results for searches like https://www.google.com/search?q=gmail+ios+share+extension+bug -- It's really inconvenient to have share via Gmail broken and would be great to know whether the bug has been acknowledged and whether they're working on a fix. – user161642 Aug 05 '15 at 16:16
  • 1
    Same thing is happening with React Native. The Gmail app is just putting the body into the subject and ignoring the subject. – Greg Blass Jun 06 '17 at 16:29
  • iOS12 is just around the corner and it still isn't working on iOS 11 – PanxShaz Jun 28 '18 at 21:32
  • @PanxShaz Did you find any solution? regarding gmail subject issue? – Umair_UAS Jan 18 '19 at 11:35
  • This is still the same to me actually....I found out that Reddit managed somehow to clear the subject text at least. I've posted more details here: https://stackoverflow.com/questions/30350465/how-to-set-uiactivityviewcontroller-gmail-share-subject-different-than-body/56646569#56646569 – Carlos Zinato Jun 18 '19 at 11:47
  • Dude! This guy found how they do it on Reddit for iOS (and I noticed that Slack also does it) https://stackoverflow.com/a/51451433/1272263 They add a lot of empty spaces in the beginning of the body text! Here is a screenshot of it. Mail app on the left and Gmail on the right: https://i.stack.imgur.com/ThsY1.jpg – Carlos Zinato Jun 18 '19 at 12:27
0

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"
        } 
    }


}
Allen
  • 2,979
  • 1
  • 29
  • 34
-1

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) { // ... };

Faran Ghani
  • 277
  • 4
  • 17