【问题标题】:NSURLSession/NSURLConnection HTTP load failed on Swift 4NSURLSession/NSURLConnection HTTP 加载在 Swift 4 上失败
【发布时间】:2018-04-20 10:03:52
【问题描述】:

我有 xcode 9.1 和 swift 4,我无法再使用 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
    }
}

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

2017-11-07 20:49:30.836087+0000 TestApp[1851:1259046] TIC TCP 连接 失败 [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] 任务 . HTTP 加载失败(错误 代码:-1200 [3:-9800])

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

我已经在我的 .plist 文件中加入了这个:

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

注意:相同的代码在 swift 3.2 中也能完美运行

【问题讨论】:

  • 我在通过 Apple Watch 播放音频流时遇到了同样的问题。但是 swift 版本是 3.2 我已经花了很多时间来解决这个问题但一无所获。 :( 你解决了吗?
  • @Youssef,已经找到了解决方案。
  • 所以有什么解决办法吗?

标签: ios swift xcode uiwebview nsurlconnection


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    NSURLConnection 错误 - 代码 - 1200

    我的开发语言是 Objective C。

    【讨论】:

      猜你喜欢
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 2016-01-18
      • 2016-10-19
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多