Calling Javascript using UIWebView

前端 未结 2 852
野趣味
野趣味 2020-11-27 18:08

I am trying to call a javascript in a html page using the function -

View did load function
{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDo         


        
相关标签:
2条回答
  • 2020-11-27 18:59

    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.

    0 讨论(0)
  • 2020-11-27 19:05

    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.

    0 讨论(0)
提交回复
热议问题