Reputation: 2079
I am trying to learn ios and my first project is to make an app that displays weather forecast in the area the of the zip code the user enters.
I have the url which is:
NSString *zipUrl = @"http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%2289448%22&format=json";
The zip code is near the end after 3D%22:
3D%22*89448*%22
I just need to figure out how to insert what the user entered in a text box into this spot. I have tried [NSString StringWithFormat]
but there are to many % signs that it doesn't work.
Please let me know if you need any more info.
Upvotes: 0
Views: 181
Reputation: 47059
You can enter/add/put value from user input by %@
sign, such like
NSString *urlString = [NSString stringWithFormat:@"http://query.yahooapis.com/userInput=%@",textFiled.text];
And also use stringByAddingPercentEscapesUsingEncoding for convert your URL to legal URL
NSString *urlString = [NSString stringWithFormat:@"URL_STRING"];
NSURL *myUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Upvotes: 0
Reputation: 9913
You need to decode zipUrl first because it encoded string.
NSString *zipUrl = @"http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%2289448%22&format=json";
NSString *decodedString= [[NSString stringWithFormat:@"%@",zipUrl] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"decodedString : %@",decodedString);
NSString *myStringURL = [NSString stringWithFormat:@"http://query.yahooapis.com/v1/public/yql?q=select item from weather.forecast where location=%@&format=json",yourtextfield.text];
Hope it helps you.
Upvotes: 1