我正在使用wunderground将天气 api 用于我当前的位置。当当前位置城市在 API 中不可用时,应用程序会崩溃。现在我想在当前位置城市不可用时重定向附近的天气位置城市。
这是我的代码,
-(void) geoCodeUsingAddress:(NSString *)address
{
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *urlString =[NSString stringWithFormat:@"http://api.wunderground.com/api/8e2edc55aaf7cfa7/geolookup/conditions/forecast/q/%@.json",esc_addr];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *alldicdata=[NSJSONSerialization JSONObjectWithData:response1 options:0 error:nil];
NSDictionary *forecast=[alldicdata objectForKey:@"forecast"];
NSDictionary *simpleforecast=[forecast objectForKey:@"simpleforecast"];
NSArray *arrofforecastday=[simpleforecast objectForKey:@"forecastday"];
NSMutableArray *dataStore1=[[NSMutableArray alloc]init];
for(NSDictionary *dict in arrofforecastday)
{
NSMutableDictionary *data=[[NSMutableDictionary alloc] init];
[data setObject:[dict objectForKey:@"conditions"] forKey:@"conditions"];
有任何方法可以重定向或找到附近的城市。有人可以帮我吗?