javascript – WkWebView Message handler does not return the worth of textbox first time, nevertheless it does the second time I press the button


So, I’ve an HTML kind with textboxes. I load the shape inside WKWebView and I take advantage of a message handler inside my Swift app to have the ability to dump the content material of the textboxes and use it afterward inside Swift Variables.

The issue is, once I press the Register button which triggers the javascript operate that grabs the worth of the textboses, first result’s clean. Not null, only a area.

If I press the button once more, I get the precise content material of the textboxes. I’ve tried utilizing the evaluateJavascript operate to get the ingredient by ID, similar factor, first time I get nothing, second time I get the anticipated values.

My ViewController’s code:

personal var webkitview: WKWebView!
    var lsusername = "";
    var lspassword = "";
    
    @IBOutlet weak var baseview: UIView!
    @IBOutlet weak var registerAccountButton: UIButton!
    
    override func viewDidLoad() {
        tremendous.viewDidLoad()
    
        self.webkitview = WKWebView(body: self.baseview.bounds, configuration: self.getWKWebViewConfiguration())
        baseview.addSubview(self.appvalleyRegistration)
        baseview.translatesAutoresizingMaskIntoConstraints = false;
        
        let url = URL(string: "https://redacted.com/registrationform.php")!
        webkitview.load(URLRequest(url: url))

    }
    
    override func viewWillAppear(_ animated: Bool) {
        tremendous.viewWillAppear(animated)
        setNeedsStatusBarAppearanceUpdate()
    }
    override var preferredStatusBarStyle: UIStatusBarStyle {
        .darkContent
    }
    
    
    personal func getWKWebViewConfiguration() -> WKWebViewConfiguration {
            let userController = WKUserContentController()
            userController.add(self, identify: "observer")
            let configuration = WKWebViewConfiguration()
            configuration.userContentController = userController
            return configuration
    }
        
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if let knowledge = message.physique as? [String : String], let passw = knowledge["passw"], let e-mail = knowledge["email"] {
            lsusername = e-mail;
            lspassword = passw;
        } else {
            print("No knowledge.");
        }
    }
    
    @IBAction func registerUser(_ sender: Any) {
        webkitview.evaluateJavaScript("submitForm()");
        showUser(e-mail: lsusername, identify: lspassword);
    }
    
    personal func showUser(e-mail: String, identify: String) {
            let userDescription = "(e-mail) (identify)"
            let alertController = UIAlertController(title: "Consumer", message: userDescription, preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "OK", fashion: .default))
            current(alertController, animated: true)
    }

The alert I set off utilizing showUser() is clean the primary time, and if I press the button once more it exhibits the right username and password grabbed from the web page inside Webkit.

My kind:

<physique>
<script kind="textual content/javascript">
          operate submitForm() {
              var message = {
                passw: doc.getElementById("passw").worth,
                e-mail: doc.getElementById("e-mail").worth
              };
              window.webkit.messageHandlers.observer.postMessage(message);
          }
</script>
  <div class="container">
    <h1>Welcome to my website</h1>
    <p>Let's arrange your account</p>
    <hr>

    <label for="e-mail"><b>E mail</b></label>
    <enter kind="textual content" placeholder="Enter E mail" identify="e-mail" id="e-mail" required>

    <label for="psw"><b>Password</b></label>
    <enter kind="password" placeholder="Enter Password" identify="passw" id="passw" required>

    <label for="psw-repeat"><b>Repeat Password</b></label>
    <enter kind="password" placeholder="Repeat Password" identify="psw-repeat" id="psw-repeat" required>
  </div>
</physique>

I do not perceive why it really works the second time however not the primary.

I attempted including sleep() to perhaps wait JS to totally full evaluating however nonetheless nothing.

Similar Posts

Leave a Reply

Your email address will not be published.