开发者

Help to decipher iPhone app code

开发者 https://www.devze.com 2023-01-11 04:53 出处:网络
Can someone please help me decipher this code? It\'s executing开发者_StackOverflow社区 when a user pushed a button to submit the data, or in this case, a \"question\" in the iPhone app.

Can someone please help me decipher this code? It's executing开发者_StackOverflow社区 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.

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消