0
我正在使用我的当前位置的天气api,使用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"];
任何方式重定向或通过提供城市附近找到。可任何一个请帮助我吗?
也想指出的是,OP取得了同步网络通话。 – 0x8badf00d
同意(当然,假设他正在从主队列中调用geoCodeUsingAddress)。他真的应该使用GCD'dispatch_async'或者创建一个操作队列,然后调用'addOperationWithBlock',将这段代码调度到后台队列中。或者他应该执行'sendAsynchronousRequest'或使用'NSURLConnection'委托方法。 – Rob