从 OpenWeatherMap Api 崩溃应用程序获取 NSURLSESSION 数据
Getting NSURLSESSION data from OpenWeatherMap Api crashing app
我正在从 NSURLSession
获取 openweathermap
天气数据。
在英国,该应用程序可以完美启动并按预期加载,但是当我在英国以外加载该应用程序时,该应用程序会因为这种天气而崩溃 API。
即使使用具有美国模拟位置的 Xcode 模拟器,应用程序也会崩溃。
我不知道为什么?任何帮助将不胜感激。
-(void)getWeather{
NSURLSession *session = [NSURLSession sharedSession];
NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?q=%@,%@&APPID=(APIKEYREMOVED)", city, country];
NSURL *weatherURL = [NSURL URLWithString:urlString];
[[session dataTaskWithURL:weatherURL
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
[weatherIcon removeFromSuperview];
[temperature removeFromSuperview];
NSMutableDictionary *allData = [ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
__block NSString* currentWeather = nil;
dispatch_async(dispatch_get_main_queue(), ^{
weatherIcon = [[UIImageView alloc] initWithFrame:CGRectMake(14, 12, 28, 30)];
[weatherIcon setContentMode:UIViewContentModeScaleAspectFit];
[_scrollView addSubview:weatherIcon];
temperature = [[UILabel alloc] initWithFrame:CGRectMake(42, 11, 32, 34)];
temperature.text = [NSString stringWithFormat:@"%.0f%@",[allData[@"main"][@"temp"] floatValue] - 273.15, @"\u00B0"];
[temperature setText:[NSString stringWithFormat:@"%.0f%@",[allData[@"main"][@"temp"] floatValue] - 273.15, @"\u00B0"]];
[temperature setTextAlignment:NSTextAlignmentCenter];
[temperature setFont:[UIFont fontWithName:@"Helvetica-Light" size:21]];
temperature.textColor = [UIColor blackColor];
[_scrollView addSubview:temperature];
[self setWeatherIcon];
});
NSArray* weather = allData[@"weather"];
for (NSDictionary* weatherDictionary in weather)
{
static dispatch_once_t once;
dispatch_once(&once, ^ {
currentWeather = weatherDictionary[@"main"];
weatherType = currentWeather;
});
}
}] resume];
}
错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(
0 CoreFoundation 0x0000000112a58b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000112154141 objc_exception_throw + 48
2 CoreFoundation 0x0000000112ac1625 +[NSException raise:format:] + 197
3 Foundation 0x000000010f311df3 +[NSJSONSerialization JSONObjectWithData:options:error:] + 67
4 Car Mode 0x000000010e036964 __37-[ViewController getLocalWeatherData]_block_invoke + 228
5 CFNetwork 0x0000000116f4787b __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 19
6 CFNetwork 0x0000000116f47095 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 143
7 Foundation 0x000000010f2e5237 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
8 Foundation 0x000000010f2e4f3b -[NSBlockOperation main] + 101
9 Foundation 0x000000010f2e36f7 -[__NSOperationInternal _start:] + 627
10 Foundation 0x000000010f2df47c __NSOQSchedule_f + 198
11 libdispatch.dylib 0x000000011641605c _dispatch_client_callout + 8
12 libdispatch.dylib 0x00000001163f494f _dispatch_queue_serial_drain + 221
13 libdispatch.dylib 0x00000001163f5669 _dispatch_queue_invoke + 1084
14 libdispatch.dylib 0x00000001163f7ec4 _dispatch_root_queue_drain + 634
15 libdispatch.dylib 0x00000001163f7bef _dispatch_worker_thread3 + 123
16 libsystem_pthread.dylib 0x00000001167a8616 _pthread_wqthread + 1299
17 libsystem_pthread.dylib 0x00000001167a80f1 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我知道数据返回 nil,但为什么它只在英国以外返回 nil?
按照建议添加 NSLog
后:
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x61000025f5f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}
您需要用 stringByAddingPercentEncodingWithAllowedCharacters
和 URLQueryAllowedCharacterSet
对 URL 进行编码
我正在从 NSURLSession
获取 openweathermap
天气数据。
在英国,该应用程序可以完美启动并按预期加载,但是当我在英国以外加载该应用程序时,该应用程序会因为这种天气而崩溃 API。
即使使用具有美国模拟位置的 Xcode 模拟器,应用程序也会崩溃。
我不知道为什么?任何帮助将不胜感激。
-(void)getWeather{
NSURLSession *session = [NSURLSession sharedSession];
NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?q=%@,%@&APPID=(APIKEYREMOVED)", city, country];
NSURL *weatherURL = [NSURL URLWithString:urlString];
[[session dataTaskWithURL:weatherURL
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
[weatherIcon removeFromSuperview];
[temperature removeFromSuperview];
NSMutableDictionary *allData = [ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
__block NSString* currentWeather = nil;
dispatch_async(dispatch_get_main_queue(), ^{
weatherIcon = [[UIImageView alloc] initWithFrame:CGRectMake(14, 12, 28, 30)];
[weatherIcon setContentMode:UIViewContentModeScaleAspectFit];
[_scrollView addSubview:weatherIcon];
temperature = [[UILabel alloc] initWithFrame:CGRectMake(42, 11, 32, 34)];
temperature.text = [NSString stringWithFormat:@"%.0f%@",[allData[@"main"][@"temp"] floatValue] - 273.15, @"\u00B0"];
[temperature setText:[NSString stringWithFormat:@"%.0f%@",[allData[@"main"][@"temp"] floatValue] - 273.15, @"\u00B0"]];
[temperature setTextAlignment:NSTextAlignmentCenter];
[temperature setFont:[UIFont fontWithName:@"Helvetica-Light" size:21]];
temperature.textColor = [UIColor blackColor];
[_scrollView addSubview:temperature];
[self setWeatherIcon];
});
NSArray* weather = allData[@"weather"];
for (NSDictionary* weatherDictionary in weather)
{
static dispatch_once_t once;
dispatch_once(&once, ^ {
currentWeather = weatherDictionary[@"main"];
weatherType = currentWeather;
});
}
}] resume];
}
错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(
0 CoreFoundation 0x0000000112a58b0b __exceptionPreprocess + 171
1 libobjc.A.dylib 0x0000000112154141 objc_exception_throw + 48
2 CoreFoundation 0x0000000112ac1625 +[NSException raise:format:] + 197
3 Foundation 0x000000010f311df3 +[NSJSONSerialization JSONObjectWithData:options:error:] + 67
4 Car Mode 0x000000010e036964 __37-[ViewController getLocalWeatherData]_block_invoke + 228
5 CFNetwork 0x0000000116f4787b __75-[__NSURLSessionLocal taskForClass:request:uploadFile:bodyData:completion:]_block_invoke + 19
6 CFNetwork 0x0000000116f47095 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke + 143
7 Foundation 0x000000010f2e5237 __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__ + 7
8 Foundation 0x000000010f2e4f3b -[NSBlockOperation main] + 101
9 Foundation 0x000000010f2e36f7 -[__NSOperationInternal _start:] + 627
10 Foundation 0x000000010f2df47c __NSOQSchedule_f + 198
11 libdispatch.dylib 0x000000011641605c _dispatch_client_callout + 8
12 libdispatch.dylib 0x00000001163f494f _dispatch_queue_serial_drain + 221
13 libdispatch.dylib 0x00000001163f5669 _dispatch_queue_invoke + 1084
14 libdispatch.dylib 0x00000001163f7ec4 _dispatch_root_queue_drain + 634
15 libdispatch.dylib 0x00000001163f7bef _dispatch_worker_thread3 + 123
16 libsystem_pthread.dylib 0x00000001167a8616 _pthread_wqthread + 1299
17 libsystem_pthread.dylib 0x00000001167a80f1 start_wqthread + 13
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
我知道数据返回 nil,但为什么它只在英国以外返回 nil?
按照建议添加 NSLog
后:
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x61000025f5f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}
您需要用 stringByAddingPercentEncodingWithAllowedCharacters
和 URLQueryAllowedCharacterSet