ios NSURLConnection 以错误结束 - 代码 -1022

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47865216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:39:08  来源:igfitidea点击:

NSURLConnection finished with error - code -1022

ioswebview

提问by Gangani Roshan

Guys i try to learn webview it can't load give me error like:

伙计们,我尝试学习无法加载的 webview 给我错误,例如:

NSURLConnection finished with error - code -1022

NSURLConnection 以错误结束 - 代码 -1022

- (void)viewDidLoad {
        [super viewDidLoad];
        NSString *urlString = @"http://www.sourcefreeze.com";
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        [_webView loadRequest:request];

    }

回答by EricZhao

I think it is about App Transport Security.Because your url is not https.Try to change like this in the info.plist file

我认为这是关于应用程序传输安全。因为你的 url 不是 https。尝试在 info.plist 文件中像这样更改

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

回答by iVarun

You are getting this error because you are not using https url. To fix follow below steps:

您收到此错误是因为您没有使用 https url。要修复请按照以下步骤操作:

  • Right click on info.plist file.
  • Open As Source code.
  • Add below line just before /dict:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    
  • 右键单击 info.plist 文件。
  • 作为源代码打开。
  • /dict之前添加以下行:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    

回答by Onat Korucu

I was having a similar issue,

我遇到了类似的问题,

NSURLConnection finished with error - code -1200

I was trying to reach a blocked port in my company. Changing the port let me connect normally.

我试图到达我公司的一个阻塞端口。更改端口让我正常连接。

As pointed out below, disabling Firewall could also solve the issue.

正如下面所指出的,禁用防火墙也可以解决这个问题。

回答by vilas deshmukh

Few silly things but give a try as below:

一些愚蠢的事情,但请尝试如下:

  1. First, you can try to check the issue on the simulator i.e run the app in the simulator and check whether still there is an issue.

  2. In-network class or where your API call function check whether this is implemented

    request.allHTTPHeaderFields = ["Content-Type" : "application/json"]

  3. Right-click on info.plist file. select open as source code.

  1. 首先,您可以尝试在模拟器上检查问题,即在模拟器中运行应用程序并检查是否仍然存在问题。

  2. 网络内类或您的 API 调用函数在哪里检查是否已实现

    request.allHTTPHeaderFields = ["Content-Type" : "application/json"]

  3. 右键单击 info.plist 文件。选择作为源代码开放。

check and find below whether lines are present

检查并在下面找到是否存在线条

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

if not present add above lines just before /dict:

如果不存在,请在 /dict 之前添加以上几行:

  1. Also, check if you are URL is from the local server and you are switching to the different live URL.

  2. Check the internet connection of the device.

  1. 此外,请检查您的 URL 是否来自本地服务器,并且您要切换到不同的实时 URL。

  2. 检查设备的互联网连接。

回答by Seliver

To give an answer on your question we need to see full logs. But I guess the problem in the App Transport Security. Since iOS 9 all connection should be done with https and the destination url should be backed with TLS version greater or equal 1.2, until you'll not specify exceptions in info.plist file in App Transport security section. Also you can "Allow Arbitrary Loads" for all connections, but this way isn't preferred. You can read more details about this here https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html(in the NSAppTransportSecurity section)

要回答您的问题,我们需要查看完整日志。但我猜是 App Transport Security 中的问题。由于 iOS 9 所有连接都应该使用 https 完成,并且目标 url 应该使用大于或等于 1.2 的 TLS 版本支持,直到您不会在应用传输安全部分的 info.plist 文件中指定例外。您也可以为所有连接“允许任意加载”,但这种方式不是首选。您可以在此处阅读更多详细信息https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html(在 NSAppTransportSecurity 部分)