Hello Readers,
In order to call javascript functions within webview,following code snippet may be used:
Specify path of the html file which contains the javascript function:
NSString *path;
NSBundle *bundle = [NSBundle mainBundle];
path = [bundle pathForResource:@"FileName" ofType:@"html"];
//Form the url with the path and load it in webview
NSURL *fileUrl = [NSURL fileURLWithPath:path];
[webViewVariable loadRequest:[NSURLRequest requestWithURL:fileUrl]];
//Call the javascript function here
NSString * funcCall = [NSString stringWithFormat:@"functionTest()"];// Name of the function that needs to be called goes here
[webViewOutlet stringByEvaluatingJavaScriptFromString:funcCall];
In swift the following code can be used:
var path : NSString;
var bundle = NSBundle.mainBundle()
path = bundle.pathForResource("FileName", ofType: "html")!
var fileUrl = NSURL.fileURLWithPath(path as String)
var request = NSURLRequest(URL: fileUrl)
webViewOutlet.loadRequest(request)
var funcCall = NSString(format:"functionTest()")
webViewOutlet.stringByEvaluatingJavaScriptFromString(funcCall as String)
0 Comment(s)