Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 8, 2009, 02: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 02:23 PM.
kwjohns is offline   0 Reply With Quote
Old Apr 8, 2009, 02: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, 03: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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
NSString from bool array BlobbleGame iPhone/iPad Programming 5 Jun 25, 2011 04:29 PM
Please Help NSString Cutting off Characters sm4ck iPhone/iPad Programming 1 Dec 30, 2009 05:16 PM
A mother cuts off her baby's head wdlove Current Events 46 Aug 24, 2003 09:15 PM
Man Cuts Off *****, Tosses It to His Wife medea Community 22 Feb 28, 2003 09:05 PM
Microsoft to Cut Off Support for Java in 2004 Ouroboros Mac Applications and Mac App Store 25 Jun 27, 2002 08:04 AM


All times are GMT -5. The time now is 06:41 AM.

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

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