Calling Javascript using UIWebView Calling Javascript using UIWebView javascript javascript

Calling Javascript using UIWebView


Simple: You try to execute the JS function from Objective-C before the page even has been loaded.

Implement the UIWebView's delegate method webViewDidFinishLoad: in your UIViewController and in there you call [graphView stringByEvaluatingJavaScriptFromString:@"methodName()"]; to make sure the function gets called after the page has been loaded.


To clarify a little bit more.

.h - implement the UIWebViewDelegate

@interface YourViewController : UIViewController <UIWebViewDelegate>@property (weak, nonatomic) IBOutlet UIWebView *webView;@end

.m

- (void)viewDidLoad{    [super viewDidLoad];    NSString *path = @"http://www.google.com";    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]];    _webView.delegate = self; //Set the webviews delegate to this}- (void) webViewDidFinishLoad:(UIWebView *)webView{    //Execute javascript method or pure javascript if needed    [_webView stringByEvaluatingJavaScriptFromString:@"methodName();"];}

You could also assign the delegate from storyboard instead of doing it in the code.