2

I working on mail sending feature in my app. Here in message body of Gmail app while sending mail from my app. It is not displaying & and ' which are special characters.

enter image description here

Here is the code.

    //Suject 
    NSString *strSubject = @"Subjet";
    // Message Body
    __block NSMutableString *messageBody = [[NSMutableString alloc] init];

    // gmmmailto
    //googlegmail:///co?subject=<subject text>&body=<body text>
    NSURL *checkGmailAppURL  = [NSURL URLWithString:[@"googlegmail:///co?to=&subject=hi&body=hi" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    if ([[UIApplication sharedApplication] canOpenURL:checkGmailAppURL])
    {
        [messageBody appendFormat:@"%@\n\n", @"Hi I want sell this application & want be free"];
        NSString *urlFinal = [NSString stringWithFormat: @"googlegmail:///co?subject=%@&body=%@", strSubject, messageBody];
        NSURL *emailUrlFinal = [NSURL URLWithString:[urlFinal stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]];

        [[UIApplication sharedApplication] openURL:emailUrlFinal];
    }
alexburtnik
  • 7,661
  • 4
  • 32
  • 70
Mehul Chuahan
  • 752
  • 8
  • 19

2 Answers2

0

Use below method to replace special character from your string.

Pass your string with special characters to below method:

-(NSString*)replaceSpecialCharsFromString:(NSString*)str
{
    str = [str stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"];
    str = [str stringByReplacingOccurrencesOfString:@"<" withString:@"&lt;"];
    str = [str stringByReplacingOccurrencesOfString:@">" withString:@"&gt;"];
    str = [str stringByReplacingOccurrencesOfString:@"'" withString:@"&#39;"];

    return str;
}

Now, use str in mail message body and check if it solve your problem.

Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
-1

You should use MFMailComposeViewController, which was designed for sending emails from apps. Here is a code example of how you can use it:

#import "ViewController.h"
#import <MessageUI/MFMailComposeViewController.h>

@interface ViewController () <MFMailComposeViewControllerDelegate>

@end

@implementation ViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self sendEmail];
}

- (void) sendEmail {
    if ([MFMailComposeViewController canSendMail]) {
        MFMailComposeViewController* mailController = [[MFMailComposeViewController alloc] init];
        mailController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
        mailController.mailComposeDelegate = self;
        [mailController setSubject:NSLocalizedString(@"Feedback", @"")];
        [mailController setTitle:NSLocalizedString(@"Feedback", @"")];
        [mailController setToRecipients:@[@"example@gmail.com"]];
        [mailController setMessageBody:@"Hello & ampersand" isHTML:NO];
        if (mailController) {
            [self presentViewController:mailController animated:YES completion:nil];
        }
    }
    else {
        //show UIAlertView
    }
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    //Handle completion
}

@end

There wouldn't be any issues with & or '

Edit:

If you still want to use gmail app try the code below, it worked for me:

NSString *messageBodyRaw = @"Hi I want sell this application & want be free\n\n";
NSString *messageBody = [messageBodyRaw urlEncodeUsingEncoding:NSUTF8StringEncoding];

NSString *urlFinal = [NSString stringWithFormat: @"googlegmail:///co?subject=%@&body=%@", strSubject, messageBody];
NSURL *emailUrlFinal = [NSURL URLWithString:urlFinal];

[[UIApplication sharedApplication] openURL:emailUrlFinal];

NSString+URLEncoding.h:

#import <Foundation/Foundation.h>
@interface NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;
@end

NSString+URLEncoding.m:

#import "NSString+URLEncoding.h"

@implementation NSString (URLEncoding)    
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
    return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
                                                 (CFStringRef)self,
                                                 NULL,
                                                 (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                 CFStringConvertNSStringEncodingToEncoding(encoding)));
}    
@end
alexburtnik
  • 7,661
  • 4
  • 32
  • 70
  • Its working fine in default mail sending functionality. – Mehul Chuahan Oct 25 '16 at 11:11
  • If it was working fine, you wouldn't ask a question about not working `&`, right? The thing is that the approach you're using is not "default" as you said and moreover it will fail if there is no google mail app on a device, while the default approach which I posted will work. – alexburtnik Oct 25 '16 at 11:15
  • My question contains "in message body of gmail app" which clarify that it's not working for gmail app. I have installed gmail and its opening but the encoding also not working for & and '. And adding %26 and %E2%80%99 simultaneously. – Mehul Chuahan Oct 25 '16 at 12:00
  • Couldn't find the method 'urlEncodeUsingEncoding' which encode string and returns string as well. – Mehul Chuahan Oct 26 '16 at 09:53
  • @MehulChuahan My bad, sorry. Include the required method in NSString category. Check the updated answer. – alexburtnik Oct 26 '16 at 10:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126713/discussion-between-mehul-chuahan-and-alexburtnik). – Mehul Chuahan Oct 26 '16 at 10:52
  • If any user don't use native mail app you cant send mail. – Ariel Antonio Fundora Mar 13 '19 at 17:09
  • @ArielAntonioFundora You're right, and in that case `canSendMail` check will fail and you just need to handle that case. Anyway this is `a standard interface for managing, editing, and sending an email message` according to documentation. – alexburtnik Mar 13 '19 at 17:28