I have a image URL from which I need to fetch the images.
But before hitting the URL I need to mani开发者_如何学Gopulate it. For example:
URL :- http://www.myimages.com/XYZ?wid=50&hei=55&fmt=jpeg&qlt=95&op_sharpen=0&resMode=bicub
I am receiving this URL from a server. How can I best play with these parameters, append them back with updated values, and the go to the newly-built URL?
If you need to replace the get parameters you could store that URL string into a mutable string, and then look for the &yourvar=
or ?yourvar=
patterns using rangeOfString
and then identify where that variable value ends and replace that value.
OR, I would probably split it using
comps = [URLstring componentsSeparatedByString:@"?"]
,
then getParams = [NSMutableArray arrayWithArray:[[comps objectAtIndex:1] componentsSeparatedByString:@"&"]]
and finally iterate over getParams and var = [NSMutableArray arrayWithArray:[[getParams objectAtIndex:counter] componentsSeparatedByString:@"="]]
.
The first step splits between the URI and the GET parameters. The second step splits the GET parameters. The third step gives you access to each of the parameter names and their values. You can modify the variables that you want to modify at this step if they match your search.
As you end each of the processing steps you are going to have to save the result for each of them using [var componentsJoinedByString:@"&"]
for example.
If your url is fixed, then its simple fo r you to ask the question.
NSString * string = [NSString stringWithFormat: ...]; method will help. But seems to me that your url is also not fixed. That being the reason for you posting the question.
You can try to get the index of "=" and "&" chars. Then you can use NSString Class object methods to get the substrings, which you can replace, by your values, which will be again strings.
or you can convert the string to const char * and then try to manipulate it.
If you're using iOS7, you can use NSURLComponents, look this example:
NSString *urlString = @"https://mail.google.com/mail/u/0";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];
[components setQuery:@"shva=1"];
Other examples:
//Example One
NSString *urlString = @"https://mail.google.com/mail/u/0/?shva=1#inbox";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];
NSLog(@"%@ - %@ - %@ - %@", components.scheme, components.host, components.query, components.fragment);
//Example Two
NSString *urlString = @"https://mail.google.com/mail/u/0/?shva=1#inbox";
NSURLComponents *components = [[NSURLComponents alloc] initWithString:urlString];
if (components) {
//good URL
} else {
//bad URL
}
//Example three
NSURLComponents *components = [NSURLComponents new];
[components setScheme:@"https"];
[components setHost:@"mail.google.com"];
[components setQuery:@"shva=1"];
[components setFragment:@"inbox"];
[components setPath:@"/mail/u/0/"];
[self.webview loadRequest:[[NSURLRequest alloc] initWithURL:[components URL]]];
But with NSURLComponents you can do meny other things, check public interface of NSURLComponents, the official documentation nothing is available online yet.
精彩评论