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"];
Breaking it down makes it a bit easier.
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:
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.