1

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.

4
  • Why would you want to run a JavaScript function? What are you trying to accomplish? Commented Nov 11, 2013 at 13:56
  • Im doing an app of a website which has a huge number of validations. Im trying to use the same javascript for validation here. Commented Nov 11, 2013 at 14:00
  • I would think it would be easier to do an HTML5 app wrapped in a UIWebView then. Commented Nov 11, 2013 at 14:05
  • yeah.. Tats a better idea.. But i need to learn about this.. Commented Nov 11, 2013 at 14:08

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

how do i call a function in that jsvascript file??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.