Shob-Z
Shob-Z

Reputation: 891

Implement JS function in IOS

I have certain validation functions in a javascript file. I want to use them in my IOS app.

NSString *path = [[NSBundle mainBundle] pathForResource:@"validate" ofType:@"js"];

NSError* error;

NSString* validate = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

I have a function keyDetails() in validate.js

How do i create an instance of that function and call it.

Upvotes: 1

Views: 107

Answers (1)

woz
woz

Reputation: 10994

This is how you would do it:

NSString *path = [[NSBundle mainBundle] pathForResource:@"validate" ofType:@"js"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"keyDetails()"];

The documentation for stringByEvaluatingJavaScriptFromString is here.

Upvotes: 1

Related Questions