views:

129

answers:

2

Can someone please help me decipher this code? It's executing when a user pushed a button to submit the data, or in this case, a "question" in the iPhone app.

I know that it's sending the string to my phpscript.php hosted on the server, but what i'm not familiar with are the long list of commands happening.

NSUserDefaults *p = [NSUserDefaults standardUserDefaults];
[p setObject:[NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://website.com/phpscript.php?user=%@&pass=%@&cat=%@&sub=%@&body=%@",[[p valueForKey:@"user"] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],[[p valueForKey:@"pass"] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding],sport,@"",[[tvQ.text stringByReplacingOccurrencesOfString:@"\n" withString:@" "] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]]]] forKey:@"q"];
+3  A: 

It is creating a string from the tvQ.text that replaces any new lines with a blank space, it is then creating the url looking string using the above string and others it grabs (values in the user defaults, from the looks of things). it is then fixing percent escapes in this url string, and then creating a NSURL object from the NSString. This NSURL is then set as the object for the key "q" in the user defaults.

and it makes my head hurt.

Jesse Naugher
BigMike
+7  A: 

Breaking it down makes it a bit easier.

NSUserDefaults *p = [NSUserDefaults standardUserDefaults];
NSString* string1 = [[p valueForKey:@"user"] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString* string2 = [[p valueForKey:@"pass"] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString* string3 = [[tvQ.text stringByReplacingOccurrencesOfString:@"\n" withString:@" "] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSString* urlString = [NSString stringWithFormat:@"http://website.com/phpscript.php?user=%@&pass=%@&cat=%@&sub=%@&body=%@",string1,string2,sport,@"",string3];
id val1 = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
[p setObject:val1 forKey:@"q"];

So p is a dictionary object loaded from user defaults, looks like some login credentials that are probably saved from last time the app was run.

stringByAddingPercentEscapesUsingEncoding is a standard method that makes it safe to transmit characters like ' ' (space) or '%' in a request. It is applied to the strings to ensure the request will reach the server looking like it was intended.

String1 and String2 are the username and passwords presumably. String3 is the body of the query I guess.

When the URL is built it executes the query represented by urlString (the code will pause at this point while the fetch is executed - hopefully this whole block is already on a secondary thread). The result of the query is stored in dictionary p and can be accessed through a key @"q".

Handling ampersands explicitly:

[myString replaceOccurrencesOfString:@"&" withString:@"%26" options:NSCaseInsensitiveSearch range:wholeString];

This method can be applied to any string - if it is applied to the same string twice then the second time it will do nothing.

Adam Eberbach
BigMike
Adam Eberbach
Adam, sorry for not being up-to-speed, but if I wanted to cover all my bases, with the Roger Nolan example, where do I plug in the "NSMutableString *escaped" code example into the clean code you provided above?
BigMike
Let's say you have some ordinary text. Make it a NSString with something like NSString* myString = [NSString stringWithFormat:@"%s" myOrdinaryASCIITextString]. Then you can use the replaceOccurrencesOfString method as shown. Then the string is ready to send. You would probably have it as part of string3 in the example, or append it to string 3?
Adam Eberbach
Would it look something like NSString* string3 = [[tvQ.text stringByReplacingOccurrencesOfString:@"\n" withString:@" "] [[tvQ.text replaceOccurrencesOfString:@"
BigMike
BigMike
Mike you need to nest them. NSString* myNewString = [[[myString operation1] operation2] operation3];The important thing to know is that the output of these string methods is a string.
Adam Eberbach