URL must be url-encoded inside a NSString in Objective-C

November 16, 2009 § Leave a comment

When you consider putting url into a string that contains mailto:, you would have to url-encode the string in Objective-C in iphone app.

iPhone Mail app expects & supports HTML in the email body of mailto:. If you use text formats such as /t /r /n, it won’t support text format in the email body.

[NSString stringWithFormat:@”mailto”me@gmail.com?subject=hellow&body=urlencode];

How to url-encode the string
Reference: http://www.w3schools.com/TAGS/ref_urlencode.asp
1) replace space to %20
2) replace & with %26
3) replace < with %3C
4) replace > with %3E
5) replace ! with %21
6) replace ( with %28
7) replace ) with %29
8 ) replace ; with %3B
9) replace : with %3A
10) replace / with %2F
11) replace = with %3D
12) replace ” with %22
13) . remain the same single dot .
14) – remain the same as single dash –

To display “<” & “>” symbol inside HTML you must use &lt; and &gt;
&lt; => %26lt%3B
&gt; => %26gt%3B

Instruction:
1) url-encode the string
2) escape any entities

Examples
From HTML url:
&lt;http://www.google.com/&gt;
After url-encoding:
%26lt%3Bhttp%3A%2F%2Fgoogle.com%26gt%3B

From HTML url:
&lt;http://www.amazon.com/o/ASIN/0545010225/karelsofwa-20&gt;
After url-encoding:
%26lt%3Bhttp%3A%2F%2Fwww.amazon.com%2Fo%2FASIN%2F0545010225%2Fkarelsofwa-20%26gt%3B

Example of HTML:

Check this out!<br><br>Harry Potter and the Deathly Hallows (Book 7)
by J. K. Rowling.<br><br>&lt;http://www.amazon.com/o/ASIN/0545010225/
karelsofwa-20&gt;<br><br>… or from your iPhone, &lt;http://
TeleMoose.com/d_0545010225&gt;

becomes….:

Check%20this%20out%21%3Cbr%3E%3Cbr%3EHarry%20Potter%20and%20the%
20Deathly%20Hallows%20%28Book%207%29%20by%20J.%20K.%20Rowling.%3Cbr%3E
%3Cbr%3E%26lt%3Bhttp%3A%2F%2Fwww.amazon.com%2Fo%2FASIN%2F0545010225%
2Fkarelsofwa-20%26gt%3B%3Cbr%3E%3Cbr%3E…%20or%20from%20your%20iPhone
%2C%20%26lt%3Bhttp%3A%2F%2FTeleMoose.com%2Fd_0545010225%26gt%3B

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading URL must be url-encoded inside a NSString in Objective-C at Web Builders.

meta

%d bloggers like this: