ios - AFNetworking 使用 HTTPS 而不是 HTTP

标签 ios afnetworking afnetworking-2

我正在开发 iOS 应用程序并尝试使用 AFNetworking。我试图访问一个 http 站点,但我的请求失败,表明它作为 https 请求失败

NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&APPID=%@",lat, lon, [plistData objectForKey:@"weather"]];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

和错误响应

Printing description of error:
Error Domain=NSURLErrorDomain Code=-1004
"Could not connect to the server."
UserInfo=0x7fa104a253d0 {
NSUnderlyingError=0x7fa104820950 "Could not connect to the server.", 
NSErrorFailingURLStringKey=https://api.openweathermap.org/data/2.5/weather?lat=[removed]&lon=[removed]&APPID=[removed], 
NSErrorFailingURLKey=https://api.openweathermap.org/data/2.5/weather?lat=[removed]&lon=[removed]&APPID=[removed], _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61,
NSLocalizedDescription=Could not connect to the server.}

在测试站点时,它的 https 部分使站点失败。因为我没有将其作为 https 并明确地放入 http,所以我不知道出了什么问题。

我正在使用 AFNetworking 2.5.1

需要说明的是,我并没有尝试通过 https 进行连接!我不知道为什么它假设我是。请在发布答案或建议重复之前阅读整个问题。

最佳答案

由于Apple改变了App构建的要求,我需要将URL列入白名单

    <key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>api.openweathermap.org</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

以上表示将链接列入白名单所需的 info.plist 的添加 这使我能够使用 HTTP 链接。您还可以允许任意加载,但 Apple 不鼓励这样做。如果您想了解更多信息,请查看 Apple 的开发者 session “Networking with NSURLSession”

关于ios - AFNetworking 使用 HTTPS 而不是 HTTP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31634223/

相关文章:

ios - 在 iOS 的 AFNetworking2 中尝试示例应用程序但被取消错误

ios - 从一对多关系核心数据的实体中获取对象

ios - 如何让背景音乐继续播放?

iphone - 检查 NavigationStack 是否包含 View Controller 错误

iphone - AFNetworking+UIImage 先下载低分辨率图片

ios - 使用 AFNetworking 下载多个文件时收到内存警告

ios - 处理 JSON 时动画卡住

ios - AFNetworking空请求正文

swift - 在 AFNetworking swift 的 header 响应中获取 token 的值

objective-c - 受信任证书的 AFNetworking pin 公钥