I tried to call a Javascript function from swift using below code but can't access the Javascript function
This is how I create the Javascript context object:
lazy var context: JSContext? = {
let context = JSContext()
guard let
JSPath = Bundle.main.path(forResource: "IAV", ofType: "html")
else {
print("Unable to read resource files.")
return nil
}
do {
let iav = try String(contentsOfFile: JSPath, encoding: String.Encoding.utf8)
_ = context?.evaluateScript(iav)
} catch (let error) {
print("Error while processing script file: \(error)")
}
return context
}()
//Set value to js
func setToken(Token:String)
{
let jsmethod = context?.objectForKeyedSubscript("setIAVToken")
let passParameter = jsmethod?.call(withArguments: [Token])
}
The content of html file is
sample IAV form
</head> <body > <header> </header> <main> <h1>Registration Form</h1> <form id="myform" method="post"> <div id="mainContainer"> <input type="button" id="start" value="Add Bank"> </div>
var value="dhjhsd";
var setIAVToken = function(token) { value= token; } $('#start').click(function() { var iavToken = value; alert(iavToken) dwolla.configure('uat'); dwolla.iav.start('iavContainer', iavToken, function(err, res) { console.log('Error: ' + JSON.stringify(err) + ' -- Response: ' + JSON.stringify(res)); }); }); </script> </html>
In case of objective c do the following:
#import <JavaScriptCore/JavaScriptCore.h>
in header file.Next use this code inside viewcontroller.
NSString *jsPath = [[NSBundle mainBundle]
pathForResource:@"JSFileName" ofType:@"js"];