我正在使用uiactivityviewcontroller来显示共享选项.除非用户选择Gmail共享选项,否则一切正常.如果用户选择电子邮件,则会正确显示内容.但如果我选择gmail,那么正文中就不会显示任何内容.
这是代码:
NSString *str = [NSString stringWithFormat:@"%@",@"Check the application"]; NSArray* dataToShare = @[str]; // ...or whatever pieces of data you want to share. UIActivityViewController* activityViewController = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil]; [activityViewController setValue:@"Check it out" forKey:@"subject"]; if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) { activityViewController.popoverPresentationController.sourceView = self.view; activityViewController.popoverPresentationController.sourceRect = CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,400); } [self presentViewController:activityViewController animated:YES completion:nil]; [activityViewController setCompletionHandler:^(NSString *activityType,BOOL completed) { if(completed){ } }]; } - (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType{ return @"Check it out"; }
解决方法
您可以使用URL方案Gmail创建UIActivity的子类:
下面的代码提取了这个答案: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]; }
查看更多:
http://www.macstories.net/links/gmail-for-ios-url-scheme/