First, you need to be sure that the webView
has loaded properly, so do any code after it has notified you that it's done with the UIWebViewDelegate
method. Then, you have to make sure that you are at the right URL for that JS method to work (you could be redirected to a mobile site (look for .m in the URL)). If you are, there's probably no helping it, so you will have to test any JS you'll use on the iPhone on the mobile website, by opening the same URL in your computer and looking around. Run this test:
#import "MyWebViewController.h"
@interface MyWebViewController() <UIWebViewDelegate>
// If you have a storyboard
@property (nonatomic, weak) IBOutlet UIWebView *webView;
// If you don't have a storyboard
// @property (nonatomic, strong) UIWebView *webView;
@end
@implementation MyWebViewController
- (void)viewDidLoad
{
// If you don't have a storyboard
// self.webView = [[UIWebView alloc] initWithFrame:self.view.frame];
// [self.view addSubview:self.webView];
self.webView.delegate = self;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"The URL of the site we are on is: %@", webView.request.URL.absoluteString);
NSLog(@"Now, I'll evaluate the JS: %@", [webView stringByEvaluatingJavaScriptFromString:@"someMethod()"]);
}
@end