Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 8, 2009, 03:12 PM   #1
kwjohns
macrumors 6502a
 
kwjohns's Avatar
 
Join Date: Jul 2007
Ampersand cutting off NSString?

I have an NSString with an ampersand (&) in it being sent to the Mail app and it keeps cutting off the rest of my string. I've tried looking for a solution and using &&, %& and /ampersand but none seem to work. This is making me pull my hair out!

Edit: This should have gone into the iPhone forum but I'm sure the issue is the same.

Last edited by kwjohns; Apr 8, 2009 at 03:23 PM.
kwjohns is offline   0 Reply With Quote
Old Apr 8, 2009, 03:42 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
You need to encode the & since normally it's used to separate parameters in the URL. Normally NSString's stringByAddingPercentEscapesUsingEncoding method would work in most cases, but here you need to use Core Foundation to encode the ampersand:

Code:
NSString *body = @"This is an ampersand & test.";
CFStringRef encodedBody = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)body, NULL, CFSTR("&"), kCFStringEncodingUTF8);
NSString *urlString = [NSString stringWithFormat:@"mailto:user@domain.com?body=%@", (NSString *)encodedBody];
CFRelease(encodedBody);
NSURL *url = [NSURL URLWithString:urlString];
kainjow is offline   0 Reply With Quote
Old Apr 8, 2009, 04:04 PM   #3
kwjohns
Thread Starter
macrumors 6502a
 
kwjohns's Avatar
 
Join Date: Jul 2007
Quote:
Originally Posted by kainjow View Post
You need to encode the & since normally it's used to separate parameters in the URL. Normally NSString's stringByAddingPercentEscapesUsingEncoding method would work in most cases, but here you need to use Core Foundation to encode the ampersand:

Code:
NSString *body = @"This is an ampersand & test.";
CFStringRef encodedBody = CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)body, NULL, CFSTR("&"), kCFStringEncodingUTF8);
NSString *urlString = [NSString stringWithFormat:@"mailto:user@domain.com?body=%@", (NSString *)encodedBody];
CFRelease(encodedBody);
NSURL *url = [NSURL URLWithString:urlString];
That did the trick. Thank you very much!
kwjohns is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
php mysql = NSString IDMah iPhone/iPad Programming 9 Dec 20, 2013 02:35 PM
C Function from NSString? ArtOfWarfare Mac Programming 20 Jun 16, 2013 11:55 AM
NSString to NSSize larswik Mac Programming 1 May 28, 2013 07:54 PM
NSString and NSRange Tutorials nashyo iPhone/iPad Programming 3 Aug 24, 2012 10:08 PM
(NSString*) ?? thedon1 iPhone/iPad Programming 6 Jul 2, 2012 08:52 PM

Forum Jump

All times are GMT -5. The time now is 01:04 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC