UIActivityViewController ne montrant pas le corps du texte dans gmail
j'utilise uiactivityviewcontroller pour afficher l'option de partage. Tout fonctionne sauf lorsque L'utilisateur sélectionne L'option Gmail sharing. Si l'utilisateur choisit e-mail, puis il affiche le contenu correctement. Mais si je choisis gmail alors rien n'est affiché dans le corps du texte.
voici le code:
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";
}
3 réponses
vous pouvez utiliser le schéma D'URL Gmail pour créer une sous-classe D'UIActivity:
le code ci-dessous a été extrait cette réponse: 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];
}
voir plus: http://www.macstories.net/links/gmail-for-ios-url-scheme /
ressemble à un bug dans Gmail Share extension. S'il vous plaît vérifier si votre URL a &, passing & caractère rend le sujet et le corps du message vide. Vous pouvez utiliser & à la place.
PS: je n'ai pas vérifié les autres caractères spéciaux.
C'est un peu délicat dans Swift 3 et iOS 9+.
D'abord, vous devez ajouter googlegmail
à LSApplicationQueriesSchemes
dans Info.plist
Deuxièmement, voici à quoi ressemble ma sous-classe UIActivity
:
C'est un peu délicat dans Swift 3 et iOS 9+.
D'abord, vous devez ajouter googlegmail
à LSApplicationQueriesSchemes
dans Info.plist
Deuxièmement, voici à quoi ressemble ma sous-classe UIActivity
:
final class GmailNewEmailShareActivity: UIActivity {
private let urlScheme = "googlegmail:"
private var activityItems: [Any]? = nil
override class var activityCategory: UIActivityCategory {
return .share
}
override var activityType: UIActivityType? {
return UIActivityType(rawValue: "googlemail")
}
override var activityTitle: String? {
return "Gmail"
}
override var activityViewController: UIViewController? {
return nil
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
guard let url = URL(string: urlScheme) else {
return false
}
return UIApplication.shared.canOpenURL(url)
}
override func prepare(withActivityItems activityItems: [Any]) {
// At this point I store incoming activity items to use them later in `perform()`
self.activityItems = activityItems
}
override var activityImage: UIImage? {
// 60x60, 120x120 and 180x180
return nil
}
override func perform() {
guard let body = activityItems?.first as? String, let encodedBody = body.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
activityDidFinish(false)
return
}
let urlString = "\(urlScheme)///co?body=\(encodedBody)"
guard let url = URL(string: urlString) else {
activityDidFinish(false)
return
}
UIApplication.shared.openURL(url)
activityDidFinish(true)
}
}