views:

46

answers:

2

HI, i am creating an login screen to my iphone application. i am sending the username and password to php using NSURLConnection method. i can successfully send login details to php. My php page returning status values based on the login details.

(status =OK / Fail , redirectionUrl=http://www.balalaa.com)

In DidReceiveData method if i convert the NSData into string i am getting following string for successful login

"STATUS=OK&url=http://www.balalaa.com".

Is there any way to fetch the values of STATUS and url, without using NSPredict. Is there any other way to conert the NSData into NSDictionary to fetch values for STATUS key?

Thanks in advance. Ram

+1  A: 
NSString *urlDataString = //Whatever data was returned from the server as an NSString
NSArray *parameters = [urlDataString componentsSeparatedByString:@"&"];
NSMutableDictionary *parameterDictionary = [NSMutableDictionary dictionary];
for (NSString *parameter in parameters) {
   NSArray *parameterComponents = [parameter componentsSeparatedByString:@"="];
   [parameterDictionary setObject:[parameterComponents objectAtIndex:1]
                           forKey:[parameterComponents objectAtIndex:0]];
}

On the other hand, NSScanner will give you a much more efficient way of doing this, and either way you will have to un-escape whatever values and keys you get from the dictionary.

Jared P
A: 

You can use the php implementation of plists which can be found here

http://code.google.com/p/cfpropertylist/

It allows you to crete plists, you then add a NSDictionary to it and then add the strings, arrays, data or more dictionaries into the initial dictionary. You can echo the plist in either an xml format or binary, binary is better though, its smaller so less data heavy. Some googling should provide some good examples and the doco is pretty good. Hope this helps.

octermircty