1

I have a problem with loading UIWebView, using https request. I have read some related answers, but they have confused me even more. Here is my code: The link, I'm using below will redirect to another page.

class PartnersWebViewControlerViewController: UIViewController, NSURLConnectionDelegate, UIWebViewDelegate {

    @IBOutlet weak var admitadWebView: UIWebView!

    let url = "https://ad.admitad.com/g/54tjzvqfv92260a476c4ffa09d8e84/subid/26/"

    override func viewDidLoad() {
        super.viewDidLoad()
        admitadWebView.delegate = self
        let requestURL = URL(string:url)
        let request = URLRequest(url: requestURL!)
        admitadWebView.loadRequest(request)

    }
}

However everything is loading fine, when I change https to http With the help of this answer UIWebView to view self signed websites (No private api, not NSURLConnection) - is it possible?

I tried to conform those protocols NSURLConnectionDelegate, UIWebViewDelegate in a following way

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    let urlConnection: NSURLConnection = NSURLConnection(request: request as URLRequest, delegate: self)!
        urlConnection.start()
    return true
}

func connection(_ connection: NSURLConnection, canAuthenticateAgainstProtectionSpace protectionSpace: URLProtectionSpace) -> Bool {
    return true
}

func connection(_ connection: NSURLConnection, didReceive challenge: URLAuthenticationChallenge) {
    let host = "www.ad.admitad.com"

    if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust &&
        challenge.protectionSpace.host == host {
        let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
        challenge.sender!.use(credential, for: challenge)
    } else {
        challenge.sender!.performDefaultHandling!(for: challenge)
    }
}

But still, I have white screen and nothing prints to log.

Any help is appreciate. It the first time I stuck with such problem, so sorry if my questions seems dummy.

I'm using ios 10.3 UPD: this problem occurs only when launching on device (iphone 6)

Community
  • 1
  • 1
dand1
  • 371
  • 2
  • 8
  • 22

2 Answers2

0

As far as I know, Apple only lets you use HTTPS for security reasons. To access HTTP websites you have to change your Info.plist (inside support files folder). In there, you have to add a new key:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>yourwebsite.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
            <key>otherdomain.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>

Replace "yourwebsite.com" with the domain of the website you want to access. This tells xcode that you want to access that website and that you know it is insecure HTTP. If you are going to access other domains as well, you can add more keys to the dictionary in the .plist like shown above with "otherdomain".

Alexander Luna
  • 5,261
  • 4
  • 30
  • 36
0

See error log there will be

"App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file. "

Add this in your info.plist

enter image description here

See screenshot here enter image description here

Chanchal Warde
  • 983
  • 5
  • 17
  • 1
    I have already changed status, but I have problems with https, not with http – dand1 May 19 '17 at 11:09
  • Its loading in my app. If its not loading in your app then there is no issue with url. You might have done wrong something else. – Chanchal Warde May 19 '17 at 11:09
  • oh, yes, I tried to launch on simulator, and it worked. But the problem remains on my device – dand1 May 19 '17 at 11:17
  • Then check you internet connection, because i added screenshot of iphone 6. And comment its code in delegate also just for testing. – Chanchal Warde May 19 '17 at 11:53