0

//this is my website on server

let url=url(string://"https://xyz.aspx")

let urlRequest:URLRequest=URLRequest(url:url!)

WebView.loadRequest(urlRequest)

//I want to call LoginByMobileApp() javascript function from swift

 WebView.stringByEvaluatingJavaScript(from:"LoginByMobileApp()")
1

2 Answers 2

1

you can call java script function when webview didfinishload, before that your javascript function is not called..

import UIKit

class yourViewController: UIViewController, UIWebViewDelegate {

    @IBOutlet var webView : UIWebView

    var url = URL(string: "https://xyz.aspx")

    override func viewDidLoad() {
        super.viewDidLoad()

        //load initial URL
        var req = URLRequest(URL : url)
        WebView.delegate = self
        webView.loadRequest(req)
    }
    func webViewDidFinishLoad(webView : UIWebView) {
         WebView.stringByEvaluatingJavaScript(from:"LoginByMobileApp()")
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

@ Er. Khatri check the image in top of the question.
you have done it using WKWebView.., you can also use UIWebView to call javascripts in iOS
@jakirhussain try the above answer once.., I am sure your LoginByMobileApp() function will be successfully called
0

You could use evaluateJavaScript function for WKWebView.

let javaScriptString = "YOUR_JAVASCRIPT_FUNCTION"

 webView.evaluateJavaScript(javaScriptString, completionHandler: nil)

5 Comments

I'm using @IBOutlet weak var WebView: UIWebView!
You cannot do it using UIWebView. Migrate to WKWebView.
after using this I am getting value=nil and error = nil
Could you expalin more? Which value and error you are talking about
how to call JavaScript using WKWebView on DidFinishLoad @ Fares Ben Hamouda

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.