2013-08-16 18 views
0

我想學習ios和我的第一個項目是使一個應用程序,顯示用戶輸入的郵政編碼區域的天氣預報。試圖把用戶輸入在雅虎api URL

我有URL是:

NSString *zipUrl = @"http://query.yahooapis.com/v1/public/yql?q=select%20item%20from%20weather.forecast%20where%20location%3D%2289448%22&format=json"; 

的郵政編碼有靠近後端3D%22:

3D%22 * *%22

我只需要弄清楚如何將用戶在文本框中輸入的內容插入到這個位置。我試過[NSString StringWithFormat],但有很多%的跡象表明它不起作用。

如果您需要更多信息,請讓我知道。

回答

1

您需要zipUrl解碼首先因爲它編碼了字符串。

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]; 

希望它可以幫助你。

0

可以輸入/通過%@標誌添加來自用戶的輸入/放值,例如像

NSString *urlString = [NSString stringWithFormat:@"http://query.yahooapis.com/userInput=%@",textFiled.text]; 

,還可以使用stringByAddingPercentEscapesUsingEncoding您的URL轉換爲legal URL

NSString *urlString = [NSString stringWithFormat:@"URL_STRING"]; 
NSURL *myUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];