JavaScriptInterface icon indicating copy to clipboard operation
JavaScriptInterface copied to clipboard

JSInterface() class in webView??

Open ghost opened this issue 8 years ago • 6 comments

I'm a korea understand English is poor. :)

JSInterface class in..

func sayGreeting(_ message: String, _ name: String)
    {
        webView?.stringByEvaluatingJavaScript(from: "javascriptFunction(msg);")
    }

ViewController

@IBOutlet weak var webView: UIWebView!

How do I use the webView declared as a property in the sayGreeting method?

ghost avatar Jun 12 '17 06:06 ghost

@kantsoft: what did you mean "use the webView declared as property in the sayGreeting method" ? sayGreeting method is a method of Swift, you can use swift to declare some local variable to use. In my example: sayGreeting is used to call from javascript. Let try to run my demo to understand it.

suale-dev avatar Jun 17 '17 02:06 suale-dev

I'll show you the original source you're working on.

import Foundation import JavaScriptCore import UIKit

@objc protocol MyExport : JSExport { func updateUserInfo(_ userid : String) func callbackGetSharedPreferences(userid:String) }

class ViewController: UIViewController,MyExport {

@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
    super.viewDidLoad()
    let url = NSURL (string: "url :)")
    webView.delegate = self
    let requestObj = URLRequest(url: url! as URL)
    webView.loadRequest(requestObj)
    webView.addJavascriptInterface(ViewController(), forKey: "ios");
    
    print("viewDidLoad Function Finished")
}


func updateUserInfo(_ userid: String) {
    print("JS Interface works!")
    self.callbackGetSharedPreferences(userid: userid)
}


func callbackGetSharedPreferences(userid: String)
{
    print(userid)
    print(self.webView)
    self.webView?.stringByEvaluatingJavaScript(from: "callbackGetSharedPreferences('test1')")

}

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

}

Below is javascript.

jQuery(document).ready(function($) { ios.getSharedPreferences(); });

function callbackGetSharedPreferences(msg){ alert(msg); }

ios.getSharedPreferences works fine. However, the callbackGetSharedPreferences does not trigger an alert. The webView Property seems to have a problem and I do not know how to do this.

thank you :)

ghost avatar Jun 17 '17 09:06 ghost

@kantsoft you are wrong: webView.addJavascriptInterface(ViewController(), forKey: "ios"); By this way, you created a new instance of ViewController without webView object.

--> correct your code: webView.addJavascriptInterface(self, forKey: "ios");

suale-dev avatar Jun 17 '17 11:06 suale-dev

Thank You :) Happy Coding

ghost avatar Jun 17 '17 15:06 ghost

@kantsoft I just update project. Try it :)

suale-dev avatar Jun 17 '17 16:06 suale-dev

Hi I have a problem using the ur code in addJavascriptInterface() where UIViewController is subview. Let me explain more detail about it. I have and application where the UIView (with webview) is a addsubview of parent view.

So in my subview's webView added javascript interface. So when the UI launched the HTML button seem not fire to the JSInterface's function. So i suspect the add interface is not set to current webview object?

Test I had create a button where call showAlert, it work well and even at webview Delegate webViewDidFinishLoad

So can u suggest any idea about this problem?

desmondhew2006 avatar Oct 25 '17 06:10 desmondhew2006