How do I get my app to login to a Swift 4 webview website?

Asked

Viewed 138 times

1

import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {

    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string:"http://177.220.153.190:8080/solerp/")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }}
  • Hello Wenerson. Please describe the problem in more detail. What is not working? Does the code compile? Is there an error in the Xcode logs?

  • This code above is compiling Rafael yes. However I need to enter some method or function so that the field of the website login screen receives the user and password to make an automatic login. And that’s what I need, because I don’t know where to start. Would you help me.?

1 answer

0

one way to do this would be to use Javascript execution through WKWebView, have a function in the Javasscript of the site that receives the login and password and execute the login.

Let’s assume that on the site there is a function autoLogin(usuario,senha) which fills the fields and executes the click on the login button (or something of the type, depends on the necessary logic). What would need to be done is this, in the app:

let usuario = "usuario" //usuario obtido de algum lugar
let senha = "senha" //senha obtida de algum lugar

webview.evaluateJavaScript("autoLogin('\(usuario)','\(senha)')", completionHandler: nil)

I hope I’ve helped

Browser other questions tagged

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