""

Another post on phonegap and my not so called “adventure”. This time something interesting happened. I am working on phonegap project targeting both iphone and android devices. Its mostly been REST based. So i was testing on various android and iphone devices, things went well except few things like static footer and header not working from android 2.x and below phones but i’l come to this issue on my next post. I hadn’t tested on android 4.0 and above, so yeah when i ran the app on icecream sandwich(Sony Xperia U and Samsung galaxy s2) mighty exception punched me on my face. Here is the long excpetion trace

10-26 10:19:45.998: E/AndroidRuntime(23917): FATAL EXCEPTION: main
10-26 10:19:45.998: E/AndroidRuntime(23917): android.os.NetworkOnMainThreadException
10-26

10:19:45.998: E/AndroidRuntime(23917):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-26 10:19:45.998: E/AndroidRuntime

(23917):  at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at java.net.InetAddress.getAllByName

(InetAddress.java:220)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpConnection.(HttpConnection.java:71)
10-26 10:19:45.998:

E/AndroidRuntime(23917):  at libcore.net.http.HttpConnection.(HttpConnection.java:50)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpEngine.sendRequest

(HttpEngine.java:232)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpURLConnectionImpl.getResponse

(HttpURLConnectionImpl.java:273)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpURLConnectionImpl.getInputStream

(HttpURLConnectionImpl.java:168)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at libcore.net.http.HttpsURLConnectionImpl.getInputStream

(HttpsURLConnectionImpl.java:270)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at com.facebook.android.Util.openUrl(Util.java:215)
10-26 10:19:45.998:
.
.

(FbDialog.java:146)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:234)
10-26

10:19:45.998: E/AndroidRuntime(23917):  at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:349)
10-26 10:19:45.998: E/AndroidRuntime(23917):  

at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at android.os.Looper.loop(Looper.java:137)
10-26

10:19:45.998: E/AndroidRuntime(23917):  at android.app.ActivityThread.main(ActivityThread.java:4507)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

java.lang.reflect.Method.invokeNative(Native Method)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at java.lang.reflect.Method.invoke(Method.java:511)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-26 10:19:45.998:

E/AndroidRuntime(23917):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-26 10:19:45.998: E/AndroidRuntime(23917):  at

dalvik.system.NativeStart.main(Native Method)

So exception thrown was NetworkOnMainThreadException, so first thing, i checked out the documentation for the details. So according to reference:

“The exception that is thrown when an application attempts to perform a networking operation on its main thread.

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it’s heavily discouraged”.

So one thing became clear to me that phonegap-facebook plugin is performing networking operation for facebook on main thread rather than separate thread like asynctask. From API level 11, httpclient is not allowed to execute on main thread. So i found the way to remove this restriction by changing the thread policy. Here is the small snippet that goes on onCreate():

if (android.os.Build.VERSION.SDK_INT >= 11) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

initialize that view: Storyboard

This post is about a problem that is basic but starters are likely to fall into if not careful. When using a tableview, there will be a time when there will be a need to use custom table cell class. There will be outlets defined for views inside cell. Now there will be a time for need of initializing views. Now there is a chance of falling into one common problem when trying to initialize views of cell that is being loaded from Storyboard. What could be happening is stuff doesn’t get initialized when cell is created. Only one thing comes to mind is that init is not getting called at all. So lets see how init method looks like when xcode creates UITableViewCell subclass for us:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

Yeah so we have initwithstyle for intializing our stuff. But when we write any initializing code then it doesn’t work. Thing is that when your views are being loaded from storyboard then initWithCoder is used not initwithstyle. So what we need is this:

- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // Initialization code
    }
    return self;
}

Thanks.

I am tweeting: Android

Someone might be looking for integrating twitter to their android application. I found few alternatives(more than enough) on the web. I’l be talking about few of them on my upcoming posts. All this libraries are unofficial, not provided by twitter itself as there isn’t any official twitter sdk available for android. In this post i’l be talking about one library that i recently used, its Twitter4J. I am again not writing any tutorial for using it. I am here to share some pitfall i fell into, which many people can face for first time. But don’t worry, there is THIS BLOG which has full detail on setting up Twitter4J with your project and get you started tweeting in no time(techically 30 minutes :P).

So i suppose now you have setup the project as detailed in that blog. Now if everything is working than Good Job, if its not working for you yet then go through the steps again and see if you are not missing something. But if you are having problem that i faced and which i will be talking about it here then “GOOD JOB” too 😛

I faced two problems which are very basic but critical.

“oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.”

This error gave me lotsa pain since it was very difficult to find the reason. I spent many hours figuring out what was happening. Error is very specific which states that consumer key might not be matching. But i cross checked keys and everything, everything was in place, so what could be the possible reason ? Then all of a sudden, somehow i just fixed the issue like hitting my hand everywhere or shooting the arrow in dark. I was so desperate. So thing that was causing this issue was, during resgistration of app on dev.twitter.com, Callback URL value wasn’t provided. Yeah i didn’t provide any callback url because it was optional, so i skipped it. So i provided a callback url, you can provide anything there. Now you’l find that above error doesn’t happens anymore and you can start tweeting from your app.

“error – Read-only application cannot POST”

Second issue i faced was this, and this one is very easy to figure out and fix it. It means that your registered twitter app is read only. So go to settings page of your application and make it “read and write”. Thats it.

I am showing you the screenshot of how settings will look like.

Adios.

That’s my unique identity: IPhone

Recently i had a requirement to send unique identifier for device to my web service. I was going through the documentation of UIDevice class at apple’s developers site. Apple has depreceated the “uniqueIdentifier” property from IOS 5 onwards. My application project traget was IOS 5 onwards. I did a search on alternatives on web and forums, got many options. In this post i am not giving any tutorial, just about what option i chose and what silly mistake i made while integrating it into my project. One good option was recommended to me by many sources and after using it i would myself recommend this to others. I read Unique Identifier Is Dead, Long Live Unique Identifier”, author has stated the recommendation provided by apple, its pros and cons and better way to use them. Author has implemented the class that encapsulated all the work of getting and storing identifier. So i would recommend to read that blog.

It was very simple to integrate into project, just need to copy and reference one header and implementation file. So now time to state what silly mistake i made, after integrating those implementation files, i ran my project and got so many errors. I am posting some of errors below:

Undefined symbols for architecture i386:
“_SecItemUpdate”, referenced from:
+[BPXLUUIDHandler storeUUID:] in BPXLUUIDHandler.o
“_SecItemAdd”, referenced from:
+[BPXLUUIDHandler storeUUID:] in BPXLUUIDHandler.o
“_SecItemCopyMatching”, referenced from:
+[BPXLUUIDHandler UUID] in BPXLUUIDHandler.o
+[BPXLUUIDHandler reset] in BPXLUUIDHandler.o
“_SecItemDelete”, referenced from:
+[BPXLUUIDHandler reset] in BPXLUUIDHandler.o
“_kSecAttrAccount”, referenced from:
_CreateKeychainQueryDictionary in BPXLUUIDHandler.o
“_kSecAttrService”, referenced from:
_CreateKeychainQueryDictionary in BPXLUUIDHandler.o
“_kSecClass”, referenced from:
_CreateKeychainQueryDictionary in BPXLUUIDHandler.o
“_kSecClassGenericPassword”, referenced from:
_CreateKeychainQueryDictionary in BPXLUUIDHandler.o
“_kSecReturnAttributes”, referenced from:
+[BPXLUUIDHandler UUID] in BPXLUUIDHandler.o
+[BPXLUUIDHandler reset] in BPXLUUIDHandler.o
“_kSecReturnData”, referenced from:
+[BPXLUUIDHandler UUID] in BPXLUUIDHandler.o
“_kSecValueData”, referenced from:
+[BPXLUUIDHandler storeUUID:] in BPXLUUIDHandler.o

So it seemed “SecItemAdd”, “SecItemUpdate” etc were not being processed or recognized. After experiencing similar “Undefined symbols for architecture i386” errors many times in past, i can tell what could be the primary reason. The most primary cause could be that appropriate framework hasn’t been linked up to target. So my first job was to search on SecItemAdd and all above stuff and look for framework they belong to. It seemed that they belong to security farmework. So once again i happened to forgot including the framework. Once everything goes fine then just do:

import "BPXLUUIDHandler.h"

and

[BPXLUUIDHandler UUID]

So anyone who happens to fall into this pitfall like me, don’t forget to link “Security.framework” to your project target.

Thanks.

Phonegap doing facebook

I am here again to write on few more pitfalls that i fell into while setting up facebook-phonegap plugin. Set up is explained in the github project site. Just read the steps carefully and follow them one by one, don’t skip steps just for safety. Steps are clear enough to have setup your facebook plugin with phonegap project on xcode. My story starts when i finished all steps and finished setting up my project. So after everything was covered i built my project and fell straight into issues (not many issues and this particular one wasn’t any big):

1) “SenTestingKit.h not found”, yeah so some files from facebook sdk were using SenTestingKit.h which wasn’t created on my project. So to solve this issue, i had to link SenTestingKit.framework from Build phases tab -> Link Binary with libraries to project target.

Now my above issue was resolved. But real pain came next and new issue arose:

2) Undefined symbols for architecture i386: “_OBJC_CLASS_$_SenTestCase”, this gave me pain in my wherever you can think of, for hours. To be honest i was totally blank on this and didn’t know what to do. So i searched forums for this. It seemed that similar kind of problem was faced by many people. I tried every available answers and fixes but none worked for me. I even created and setup project from scratch again just to make sure if i hadn’t skipped any steps. But again, (yeah there is always a but somewhere) nothing worked. So i dig more deeper into my source files of facebook sdk. It seemed all the source files inside test folder were using this “SenTestingKit”. So i simply deleted that test folder from my project navigator, and it really resolved this issue or i removed the source of issue. No more such errors and no more pain and i don’t know about any gain.

But i wasn’t still sure if it didn’t break up the project as you don’t simply delete files and folders from external libraries without knowing. So i tested my project and performed all facebook activities and it seemed to worked fine. I haven’t found any issue till now but lets see.

I was setting up facebook-phonegap plugin with my phonegap project on xcode. After everything was setup accordingly, i ran the project and many funny things happened after that. Following are the issues that happened:

1) On first attempt run, my xcode stuck on “attaching to myapp” part. I waited and waited and yeah that, but it was stuck.
2) On second attempt i stopped the process and ran the project again. This time app opened up on simulator.

Now i thought it was one time minor issue with simulator or xcode. So i began working on project. After doing my stuff i ran my project again and same above things happened. And this was happening every cycle. I couldn’t understand why xcode build and run process was getting stuck at “attaching to myapp” on first attempt and runs on second attempt after stopping the process. After all this i found third issue:

3) On Application menu of simulator there were two launcher icon for the app, one having image icon(default phonegap icon) and other was blank. When i clicked launcher with image then app closed very quickly after opening, and when i clicked launcher without image then app opened up. This was very strange and brought questions on my mind.

I spent hours and yeah lotsa hours to fix it. I searched stackoverflow for this problem and solution. After lotsa digging around it, i found the answer on one of the forum topic and it did save my life 😛
Solution is:

“Go to Project Navigator -> Select your project name(on top) -> In right pane, select project name under targets -> Select Build Phases Tab -> Expand ‘Copy Bundle Resources'”

Now if you find “Info.plist” within the list then congratulation its your lucky day, Just delete it from list, then clean the project(optional) and run it.

I really hope it works for you too.

Gracias.

This way or that way. When working on web service based application, we make a request to a service and expect a response. So this time i have something to share on this. Most common way to make a request to service is using POST. How would you send an image to service ? In my case i was using php based web service and client is iphone application. We generally send image by writing data to http message, where we mainly mention header and body of message. Our image data goes with the message body of http request. This is how normal http message
is composed in objective-c.

NSString *boundary = @"---------------------------14737809831466499882746641449";
        
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// end of what we've added to the header
            
NSMutableData *body = [NSMutableData data];
    
// the body of the post
UIImage *contactImage = self.originalImage;
NSData *imageData = UIImagePNGRepresentation(contactImage);
            
// Now we need to append the different data 'segments'. We first start by adding the boundary.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now append the image
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\";filename=\"picture.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// We now need to tell the receiver what content type we have
// In my case it's a png image. If you have a jpg, set it to 'image/jpg'
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now we append the actual image data
[body appendData:[NSData dataWithData:imageData]];
            
// We now need to tell the receiver what content type we have
            
// and again the delimiting boundary
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// adding the body we've created to the request
[request setHTTPBody:body];

That is straight-forward and there is nothing very different with code. Now suppose you have to send some other information data along with image using same above process, so for first and few attempt i tried to write other data to message body too but i failed. I was missing something and couldn’t get it to work. So at that time i had only one solution which was working for me. The idea was to send my other data like strings and all in query string and image data via post message. So what i did was made an query string with my name balue pairs and hit this new url via post message and post message body had my image data. Ofcourse i should have been sending everything in post body but somehow it wasn’t working since i was writing all data in some wrong way as i could send only image at that time. But definitely i found the correct way and i made it right, but i’l come to that part later. For now i want to show where exactly my problem came and where i found the solution. Now my requirement was to send json object/string representation and image to server via post. Good way to do this was again writing both json and image to post message body but i wasn’t able to write both so i decided to send json string representation with query string and image data on post message body. It was definitely working. This is how i was making my json string for my array of objects and creating query string url.

NSString *jsonStr = [self.studentArray JSONRepresentation];
NSMutableString *urlString =[NSMutableString stringWithString:urlStr];
[urlString appendString:@"?student_details="];
[urlString appendString:[NSString stringWithFormat:@"%@", [jsonStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURL *url = [NSURL URLWithString:urlString];

I was hitting this url and sending image data via post, things were going fine until one big bang. If i was making json representation of 40 students inside students array and each student object has 5 members, so json representation comes out really long and big. Sending this long very long string to query string always failed. My nsurlconnection was always getting lost. Now i was left with only one option at that time, it was that i had to send both image and json on post request message body. So after many trials i finally had it working.

responseData = [[NSMutableData data] retain];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url 
                                                                cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                            timeoutInterval:60]; 
    
[request setHTTPMethod:@"POST"];
    
NSString *boundary = @"---------------------------14737809831466499882746641449";
        
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// end of what we've added to the header
            
NSMutableData *body = [NSMutableData data];
    
// the body of the post
UIImage *contactImage = self.originalImage;
NSData *imageData = UIImagePNGRepresentation(contactImage);
            
// Now we need to append the different data 'segments'. We first start by adding the boundary.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now append the image
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\";filename=\"picture.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// We now need to tell the receiver what content type we have
// In my case it's a png image. If you have a jpg, set it to 'image/jpg'
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            
// Now we append the actual image data
[body appendData:[NSData dataWithData:imageData]];
            
// and again the delimiting boundary
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"student_detail\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/json\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", jsonString] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        
// adding the body we've created to the request
[request setHTTPBody:body];
        
//[connection start];
conn = [[NSURLConnection alloc] initWithRequest:request delegate:self ];
    
[request release];

So that was it. Keep one thing in mind that “name=student_detail” matches with your request variable in web service, so whatever request variable you have in web service, just put that name here. You can write other data to post request message body too now.
Hope this works for you too.

Gracias.

Display Object: Origins

When i started corona, i felt as3 programming days back again. It is so much like actionscript 3. Though corona is a framework which is very similar to flash api. So here i am again to talk what i have been doing. I won’t be giving any tutorials, just wanted to share some concepts of things that i came to know when falling in trouble. In corona everything that is drawn or you see in screen are display objects. So i was creating a image display object and just rendering on screen.

local background = display.newImage("purple.png");

It gave the output something like this.

Well seems fine. First width and height of image was 480 * 480. Second, in corona, cartesian was positive 4th quadrant too, i.e top left was the origin (0,0) of a stage. So what it seemed was that image of 480*480 dimension is being rendered at point (0,0) aka top left point of image, at origin of stage. But i don’t know, what made me to print the position coordinates of that image and so i did, i found that position coordinates of image was (240, 240). I was lke wow, my assumption turned out to be wrong. So if position coordinate was (240, 240) then it meant that image was being rendered at point (240, 240) but registration point or reference point of image was definetly at center of image, beacuse come on! look at the output above. Just print the coordinates aaargh.

background.x = 0;
background.y = 0;

Output is now this, since registration point is at center of image.

So now according to documentation

display.newImage( filename [, baseDirectory] [, left, top] ) return image object; if we don't provide [left, top] then the image will be centered about its local origin.

So if you want your registration point on top left of image, then you can provide [left, top] parameter while creating or you can call setReferencePoint later.

Thanks.

I learned new concept, so this post will be about something…lets move on. I have used (retain) @property for objects before, on many tutorial codes i have seen (copy) semantics used for NSMutableArray, NSString etc. I wanted to know the difference between (retain) and (copy) and when to use on what situation? I did search online forums, tutorials etc for informaion and found that:
(retain) retains the object before assignment and takes the ownership. If i assign object (setter) and change the state of object, it will reflect on same original object that was being assigned. But (copy) creates a new(another copy) of object, if i change state then it won’t reflect on first object that was assigned but to new(another copy) object. For example:

NSMutableString *name = @"Leonard";
//@property (retain, nonatomic) NSMutableString *characterName;
self.characterName = name;
[characterName setString:@"Sheldon"];

name object will have value “Sheldon”, since characterName is taking the ownership of the object by retaining it before assignment.

//@property (retain, nonatomic) NSMutableString *characterName;

name object will remain “Leonard”, since characterName has been assigned another copy upon assignment.

There is more but i’l come to problem that i faced. So i had an mutable array member of a my class, I used (copy) semantics for creating setter and getter, so as usual i assigned an mutable array to my member (synthesizer). Now later on i was adding object to my mutable array member, and program crashed. Yes program crashed, and it was very intersting find when i saw the nature of crash. Nature of crash was that i was adding object to NSArray not NSMutableArray, and as we know NSArray is immutable which cannot be modified hence a crash. So later i found that since i am using (copy) semantics so my implicit synthesizer makes a copy instead of retaining it but the real thing is that copy returns a immutable object, even if copy is being called on mutable. So the object whom i was thinking as mutable turned up as immutable and my program crashed.

Using (retain) makes it work if u want mutable, but there is a mutablecopy also, i haven’t tried that yet but you can write your own mutator, use mutablecopy and see if it return mutable or immutable. The implicit synthesizer will always use copy and assign immutable object.

Thanks.

Bad Url: Objective C

I was sending few data(strings) to server via query string. Everything was going fine then one time suddenly program crashed. It was “bad url error”. I didn’t know what happened at that time. I thought i was hitting bad url so i checked it again but it was correct. I run my program again, it was running fine. I followed this again n again to replicate that crash. Then it crashed one time again, and i noticed that it crashed only when i was sending string with space between on query string like london olympics. The crash was genuine since you can’t expect query string to have any “space”. So solution was to encode my string with percent(%) escapes as you see in regular query strings. There is a simple built-in method to do this:

[myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

This will make you string something like this: london%20olympics

Thanks.