Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSession/NSURLConnection HTTP load failed on Swift 4

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

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

And i already have this into my .plist file :

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

NB: The same code was working perfectly with swift 3.2

like image 248
YouSS Avatar asked Nov 07 '17 21:11

YouSS


2 Answers

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

like image 164
Zaporozhchenko Oleksandr Avatar answered Sep 20 '22 04:09

Zaporozhchenko Oleksandr


Adding NSAllowsArbitraryLoads into .plist file resolves the error

NSURLConnection error - code - 1200

where my development language is Objective C.

like image 42
sunchao0427 Avatar answered Sep 19 '22 04:09

sunchao0427