How to Set UIactivityviewcontroller Gmail Share Subject Different Than Body

UIActivityViewController Gmail share extension duplicated body

The only workaround I've found is to remove every special characters (I was able to replace & with 'and' - it was in text, not in link)

UIActivityViewController not showing body text in gmail

You can use the URL scheme Gmail to create a subclass of UIActivity:

The code below was extracted this answer: https://stackoverflow.com/a/12766330/3726577

//ActivityViewCustomActivity.h
@interface ActivityViewCustomActivity : UIActivity

@end

//ActivityViewCustomActivity.m
@implementation ActivityViewCustomActivity

- (NSString *)activityType {
return @"googlegmail";
}

- (NSString *)activityTitle {
return @"Gmail";
}

- (UIImage *)activityImage {
// Note: These images need to have a transparent background and I recommend these sizes:
// iPadShare@2x should be 126 px, iPadShare should be 53 px, iPhoneShare@2x should be 100
// px, and iPhoneShare should be 50 px. I found these sizes to work for what I was making.

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
return [UIImage imageNamed:@"iPadShare.png"];
}
else
{
return [UIImage imageNamed:@"iPhoneShare.png"];
}
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
NSLog(@"%s", __FUNCTION__);
return YES;
}

- (void)prepareWithActivityItems:(NSArray *)activityItems {
NSLog(@"%s",__FUNCTION__);
}

- (UIViewController *)activityViewController {
NSLog(@"%s",__FUNCTION__);
return nil;
}

- (void)performActivity {
NSString *email = @"googlegmail:///co?subject=Check it out&body=Check the application";
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];

[self activityDidFinish:YES];
}

@implementation ViewController2

- (void)viewDidLoad{
[super viewDidLoad];

NSString *textItem = @"Check the application";

ActivityViewCustomActivity * ca = [ActivityViewCustomActivity new];

UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:@[textItem] applicationActivities:[NSArray arrayWithObject:ca]];

activityVC.excludedActivityTypes = @[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];
[activityVC setValue:@"Check it out" forKey:@"subject"];

activityVC.completionHandler = ^(NSString *activityType, BOOL completed)
{
NSLog(@" activityType: %@", activityType);
NSLog(@" completed: %i", completed);
};

[self presentViewController:activityVC animated:YES completion:nil];
}

See more:
http://www.macstories.net/links/gmail-for-ios-url-scheme/

Swift 4 - UIActivityViewController share to Outlook Subject and Title

Whatever view controller triggered the sharing should conform to UIActivityItemSource protocol in order to setup: preview title, email subject and as well the content of the email.

You can try out your self this example which triggers sharing on a button tap:

class ViewController: UIViewController, UIActivityItemSource {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

@IBAction func sharePressed(_ sender: Any) {
let item = [self, "Preview Title"] as [Any]

let activityViewController = UIActivityViewController(activityItems: item, applicationActivities: nil)

self.present(activityViewController, animated: true, completion: nil)
}

func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return ""
}

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
return "<html><body><p style=\"background-color: red;\">Email body message with red background</p></body></html>"
}

func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {
return "Email Subject"
}

}

To have this expected output:


Some apps like Outlook and Gmail in different iOS Versions (iOS 11, iOS 12..) behave differently: some take the first line of the body and set it as the Subject. Just be sure to test it out for the iOS version you are targeting and the app that you want to target the sharing functionality to behave correctly. You could also set multiple itemForActivityType to target logic for different apps when sharing.



Related Topics



Leave a reply



Submit