gpt4 book ai didi

ios - 发送同步请求 :urlrequest returningResponse:&responce error:&error is unavilable in WatchOS2

转载 作者:可可西里 更新时间:2023-11-01 17:05:18 26 4
gpt4 key购买 nike

我想从 WatchOS 上的 API 获取数据,因为我使用 NSURLConnection 但我得到错误 is not available in WatchOS2,在这里我添加了我使用的代码,请查看并帮助我,谢谢

NSURLRequest *urlrequest =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0"]];
NSURLResponse *responce = nil;
NSError *error = nil;
NSData* data = [NSURLConnection sendSynchronousRequest:urlrequest returningResponse:&responce error:&error];
NSMutableDictionary *allData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSString *currentWeather = nil;
NSArray* weather = allData[@"weather"];
for (NSDictionary *theWeather in weather)
{
currentWeather = theWeather[@"main"];
}
self.lbl.text = currentWeather;

最佳答案

NSURLConnection 从 iOS9 开始被弃用。因此,您应该查看 NSURLSession API。至于这个特定的错误,这个 API(sendSynchronousRequest :) 是 WatchOS 禁止的。 command+click 在此 API 上,您将看到 __WATCHOS_PROHIBITED 标志。

NSURLSession 提供 dataTaskWithRequest:completionHandler: 作为替代。但是,这不是同步调用。因此,您需要稍微更改代码并在到达 completionHandler 后执行工作。使用下面的代码

NSURLRequest *urlrequest =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=2de143494c0b295cca9337e1e96b00e0"]];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:urlrequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *allData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//Here you do rest of the stuff.
}] resume];

关于ios - 发送同步请求 :urlrequest returningResponse:&responce error:&error is unavilable in WatchOS2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com