【发布时间】:2016-03-12 04:30:01
【问题描述】:
我对 NSUrlSession 和 API 感到非常困惑。这是我第一次尝试使用 API,所以请以最简单的形式解释这一点。
我找到了一个获取天气的 API,我为天气位置创建了一个字符串。然后做了所有的 NSUrl / nsurlrequest。我的目标是输出所有内容,以便我可以看到该 API 的密钥。这是我到目前为止所拥有的,但它显示的只是“程序以退出代码 0 结束”
我真的不知道 NSUrlsession 期间发生了什么,因为我通过 youtube 视频了解了如何将 API 与 NSUrlConnection 一起使用。
NSString *location = @"London";
NSString *weatherString = [NSString stringWithFormat:@"https://api.openweathermap.org/data/2.5/weather?q=%@", location];
NSURL *weatherURL = [NSURL URLWithString:weatherString];
NSURLRequest *request = [NSURLRequest requestWithURL:weatherURL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error){
NSDictionary *weatherDictionary = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
NSLog(@"%@", [weatherDictionary description]);
}];
【问题讨论】:
标签: objective-c iphone xcode api