xcode NSURLSession/NSURLConnection HTTP 加载在 Swift 4 上失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47167495/
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-09-15 10:25:31  来源:igfitidea点击:

NSURLSession/NSURLConnection HTTP load failed on Swift 4

iosswiftxcodeuiwebviewnsurlconnection

提问by YouSS

I have xcode 9.1 with swift 4 and i'm unable to authenticate using webview anymore. here is my code :

我有带有 swift 4 的 xcode 9.1,我无法再使用 webview 进行身份验证。这是我的代码:

import UIKit

protocol AuthViewControllerDelegate{
    func getAccessToken(_ code: String)
}

class AuthViewController: UIViewController, NSURLConnectionDelegate {


    @IBOutlet weak var navItem: UINavigationItem!
    @IBOutlet weak var webView: UIWebView!

    var delegate: AuthViewControllerDelegate?

    override func viewDidLoad() {
        super.viewDidLoad()

        var link: "https://home.nest.com/login/oauth2?client_id=…"

        if let encodeURL = link.URLEncodedString(), let url = URL(string: encodeURL) {
            webView.loadRequest(URLRequest(url: url))
        }
    }


    override var preferredStatusBarStyle : UIStatusBarStyle {
        return .lightContent
    }

    @IBAction func CancelPressed(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true, completion: nil)
    }

    func webView(_ webView: UIWebView, shouldStartLoadWithRequest request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if let url = request.url?.absoluteString, url.contains("code=") {
            if let code = getQueryStringParameter(url,param: "code"){
                self.dismiss(animated: true, completion: { () -> Void in
                    UIApplication.shared.isNetworkActivityIndicatorVisible = false
                    self.delegate?.getAccessToken(code)
                })
            }
        }
        return true
    }

    func webViewDidStartLoad(_ webView: UIWebView){
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }

    func webViewDidFinishLoad(_ webView: UIWebView){
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }

    func getQueryStringParameter(_ url: String?, param: String) -> String? {
        if let url = url, let urlComponents = URLComponents(string: url), let queryItems = (urlComponents.queryItems) {
            return queryItems.filter({ (item) in item.name == param }).first?.value!
        }
        return nil
    }
}

And here is the error i'm getting in the log :

这是我在日志中遇到的错误:

2017-11-07 20:49:30.836087+0000 TestApp[1851:1259046] TIC TCP Conn Failed [32:0x1c0365280]: 3:-9800 Err(-9800)

2017-11-07 20:49:30.836472+0000 TestApp[1851:1259046] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9800)

2017-11-07 20:49:30.836578+0000 TestApp[1851:1259046] Task <0A675AA1-7110-4FCC-99B2-054380D22F01>.<0> HTTP load failed (error code: -1200 [3:-9800])

2017-11-07 20:49:30.837548+0000 TestApp[1851:1258708] NSURLConnection finished with error - code -1200

2017-11-07 20:49:30.836087+0000 TestApp[1851:1259046] TIC TCP Conn 失败 [32:0x1c0365280]: 3:-9800 Err(-9800)

2017-11-07 20:49:30.836472+0000 TestApp[1851:1259046] NSURLSession/NSURLConnection HTTP 加载失败(kCFStreamErrorDomainSSL,-9800)

2017-11-07 20:49:30.836578+0000 TestApp[1851:1259046] 任务 <0A675AA1-7110-4FCC-99B2-054380D22F01>.<0> HTTP 加载失败(错误代码:-031)-8046

2017-11-07 20:49:30.837548+0000 TestApp[1851:1258708] NSURLConnection 完成错误 - 代码 -1200

And i already have this into my .plist file :

我已经将它放入我的 .plist 文件中:

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

NB: The same code was working perfectly with swift 3.2

注意:相同的代码与 swift 3.2 完美配合

回答by sunchao0427

Adding NSAllowsArbitraryLoadsinto .plist file resolves the error

添加NSAllowsArbitraryLoads到 .plist 文件可解决错误

NSURLConnection error - code - 1200

NSURLConnection 错误 - 代码 - 1200

where my development language is Objective C.

我的开发语言是 Objective C。

回答by Zaporozhchenko Oleksandr

Check if your htpps://is right, had the same problem, fixed after changing url.

检查你htpps://是否正确,有同样的问题,更改 url 后修复。