Alan Tingey
Alan Tingey

Reputation: 961

Pass Value from iOS to uiWebView

I can successfully obtain a variable from javascript and display it as an alert message in an iOS app as follows:

    let loggedIn = self.webView.stringByEvaluatingJavaScript(from: "loggedIn");

    let alertController = UIAlertController(title: "iOScreator", message:
        loggedIn, preferredStyle: UIAlertControllerStyle.alert)
    alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default,handler: nil))

    self.present(alertController, animated: true, completion: nil)

The above works perfectly. Is there a similar option to pass a variable from iOS to a javascript variable and run a function that does a pop alert with that variable?

Any help, as always, is appreciated. Many thanks, Alan.

Upvotes: 0

Views: 2072

Answers (1)

Surya Subenthiran
Surya Subenthiran

Reputation: 2217

Swift code to call javascript method

self.webView.stringByEvaluatingJavaScript(from: "myFunction('param')")

javascript method

function myFunction(param)
{
    alert(param);
}

Reference passing objective c variable to javascript in ios

Hope this helps.

Upvotes: 3

Related Questions