How to call JavaScript Function in objective C
NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:@"first" ofType:@"html"];
NSURL *instructionsURL = [NSURL fileURLWithPath:path];
[webView loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
NSString * jsCallBack = [NSString stringWithFormat:@"myFunction()"];
[webView stringByEvaluatingJavaScriptFromString:jsCallBack];
[webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
If all this code called from single place, than it won't work, because page load is asynchronous and page with JavaScript code is not ready at that moment.
Try to call this methods [webView stringByEvaluatingJavaScriptFromString:@"myFunction()"];
in UIWebViewDelegate callback method –webViewDidFinishLoad:
How to call javascript code from Objective - C code?
You can load your js using
[_webView loadHTMLString:@"<script src=\"your.js\"></script>" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
and then in webViewDidFinishLoad
you can call function and pass parameters as follows
NSString *javascriptString = [NSString stringWithFormat:@"yourFunction('%@','%@')",firstVar,secondVar];
[_webView stringByEvaluatingJavaScriptFromString:javascriptString];
Hope it Helps :)
Call Javascript function with parameters from iOS ObjC
You need to build a string containing the passed argument and use that as parameter for evaluateJavascript
.
Like this:
-(void) callJavascriptFunc: (NSString*)val{
NSString* script = [NSString stringWithFormat:@"exampleFuncName('%@', '\(someConstant)')", val];
[self.webview evaluateJavascript:script completionHandler: nil];
}
Depending on where the value of the parameter comes from, however, you should make sure that no code injection is possible!
how to call a javascript function from my custom CDVPlugin class (Objective C Language)
I found the answer if we want to call any javascript method call it with evalJs which is declared in CDVCommandDelegate
Class and can write the name of the function as string with semicolon
In Cordova :
we can use evaluateJavaScript method like this
[self.commandDelegate evalJs:@"callCustomMethodJs();"];
Related Topics
Xmppframework - Retrieve Archived Messages from Openfire Server
Play Video in Background Using Avplayer
Extra Padding Above Table View Headers in iOS 15
Find List of Local Notification the App Has Already Set
Getting Username and Profile Picture from Facebook iOS 7
Scroll to Bottom of Uitextview Erratic in iOS 7
iOS Different Font Sizes Within Single Size Class for Different Devices
Why Safari Shows "No Inspectable Applications" During Remote Debugging with iOS 6 Device
Is There a Swiftui Equivalent for Viewwilldisappear(_:) or Detect When a View Is About to Be Removed
How to Add Uipickerview into Uialertcontroller (Alert or Actionsheet) in Swift
How to Use Navigation Controller Inside of Uitabbarcontroller with Storyboard on Swift
How Can an iOS 7 App Make Itself Transparent to See a User's Home Screen Image
How to Delete All Objects from My Persistent Store in Core Data
What Is Lldb_Expr in Swift Playground
Get Current iPhone Device Timezone Date and Time from Utc-5 Timezone Date and Time iPhone App