Load web page in Uiwebview Swift 3

Asked

Viewed 235 times

0

Good morning, everyone

I can easily upload a web page to a Uiwebview with the following Swift code:

import UIKit

class ConteudoOnlineViewController: UIViewController {
@IBOutlet weak var pagina: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    pagina.delegate = self as? UIWebViewDelegate
    if let url = URL(string: "http://www.meusite.com.br/login.aspx") {
        let request = URLRequest(url: url)
        pagina.loadRequest(request)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }
}

However my site has login page and I created a validation to access directly by passing a token, it happens that now I can not load this page in Uiwebview, just nothing appears. Could someone give me an idea of what I should do??

import UIKit

class ConteudoOnlineViewController: UIViewController {
@IBOutlet weak var pagina: UIWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    pagina.delegate = self as? UIWebViewDelegate
    if let url = URL(string: "http://www.meusite.com.br/login.aspx?token=6D5C6A4BF468D43ABD521A3C9D3469C3​") {
        let request = URLRequest(url: url)
        pagina.loadRequest(request)
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
    }
}

1 answer

0

iOS has a security layer called App Transport Security (ATS) which by default blocks insecure connections (HTTP). The ideal is to implement the secure connection on the server side, as unsafe connections can be blocked completely in some future version of iOS. If this is not possible, you can disable these restrictions through the project’s info.plist file. I suggest reading the documentation on portal Apple. In your case, probably the necessary configuration is something like:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>meusite.com.br</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>
  • Rafael Leão thanks for your comment, but I’ve done it to be able to access links in http but I do not get success in this case, it opens any link but when I try this way passing a token to login directly in the system nothing appears in the webview

  • Then I suggest implementing Uiwebviewdelegate methods and see which ones are called

Browser other questions tagged

You are not signed in. Login or sign up in order to post.