Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-13-2010, 07:06 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 58
ziophase is on a distinguished road
Default convert string

Hi All,

I need to be able to send data from the iPhone to a php script.

This has been working remarkably until I tried sending a string with an "&" symbol.

Naturally, this works fine:
Code:
mysite.com/test.php?var1=hello world&var2=hello world
but of course this doesn't:
Code:
mysite.com/test.php?var1=hello world & universe&var2=hello world
I have been using
Code:
NSString *addString = [NSString stringWithFormat:@"http://mysite.com/test.php?var1=%@&var2=%@",myVar1,myVar2];
Any ideas?

Cheers,

Tim
ziophase is offline   Reply With Quote
Old 01-13-2010, 12:48 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

You need to encode myVar1 with the percent escape codes first-

Code:
myVar1 = [myVar1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-13-2010, 06:28 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 58
ziophase is on a distinguished road
Default

Thanks Smasher.

In the debugger and console, it appears that with you code, nothing has changed in the string.

Code:
myVar1 = [myVar1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Any other ideas?
ziophase is offline   Reply With Quote
Old 01-13-2010, 07:29 PM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Sorry, I forgot that method doesn't handle ampersands. It should handle spaces and other problem characters though. Try this.

Code:
myVar1 = [myVar1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
myVar1 = [myVar1 stringByReplacingOccurrencesOfString:@"&" withString:@"%26"];
I've seen other solutions using CFURLCreateStringByAddingPercentEscapes if you want to google that.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 336
11 members and 325 guests
2Apps1Day, akacaj, Domele, GraffitiCircus, michelle, NetGuru, NSString, SLIC, Sloshmonster, soohyun, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,886
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:57 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0