Search code examples
iphoneiosemailcharacter-encodingurl-encoding

Characters getting cut off by Mail app when launching from within my app


I'm trying to set up a prefilled email for the user to send through the iphone native Mail application. The body of the email contains a link that the user wants to share.

My problem is that the link is getting cut off by the Mail app but when I print out the string in my app the whole link is there.

Here is a sample link:

http://sample.com/Start.asp?tqnm=xe2nbek92057479&bt=xg&o=100925&c=RB&p=2W7TvRx1

In the mail app the link shows up as:

http://sample.com/Start.asp?tqnm=xe2nbek92057479

I am using the standard code for launching the Mail app:

NSString *mailString = [NSString stringWithFormat:@"mailto:?to=%@&subject=%@&body=%@",
                        [to stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
                        [subject stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],
                        [body stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
NSLog(@"Mail String: %@", mailString);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:mailString]];

I've also tried other string encodings but I either get the same result or the body of the email doesn't show up at all.

Any help would be really appreciated. I've been looking for the answer for a while and haven't found anything to even point me in the right direction.


Solution

  • I was able to solve it, I found this function

    NSString* encodeToPercentEscapeString(NSString *string) {
    return (NSString *)
    CFURLCreateStringByAddingPercentEscapes(NULL,
                                            (CFStringRef) string,
                                            NULL,
                                            (CFStringRef) @"!*'();:@&=+$,/?%#[]",
                                            kCFStringEncodingUTF8);      
    

    Then I used that to encode the body, which took care of the ampersand case, which Matthias was referring to. Thank you Matthias for pointing me in the right direction.