UIWebView Delegate Methods not Working [closed]

99封情书 提交于 2019-12-02 17:53:28

问题


**When I Use self.myWeview.delegate = self , UIWebView can not load URL in UIWebView... But If I set it to self.myWeview.delegate = nil, then methods(delegate) can't load but URL is load this is the code:---

{
   self.myWeview.delegate = nil;
   NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   [myWebView loadRequest:request];
}

This is working fine but couldn't called delegate methods.

On the other hand

{
   self.myWeview.delegate = self;
   NSURL *url = [NSURL URLWithString:@"http:http://www.google.com"];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];
   [myWebView loadRequest:request];
}

It Couldn't load URL but calls the delegate methods


回答1:


Finally I got my answer by lot of experiment,if we not using this method

    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

except that all methods are working fine in UIwebViewDelegate

1. -(void)webViewDidFinishLoad:(UIWebView *)webView
2. -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
3. -(void)webViewDidStartLoad:(UIWebView *)webView



回答2:


remove that extra http

    {
       self.myWeview.delegate = self;
       NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
       NSURLRequest *request = [NSURLRequest requestWithURL:url];
       [myWebView loadRequest:request];
    }



回答3:


You have made the mistake in calling the URL ...

Try this :

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];



回答4:


Use this code in your viewcontroller.h file

@interface ViewController : UIViewController<UIWebViewDelegate>



回答5:


set delegate to self then you need to implement the delegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
   return YES;
}

and also don't forget to confirm the protocol in .h file like this

@interface YourViewController : UIViewController<UIWebViewDelegate>


来源:https://stackoverflow.com/questions/17289990/uiwebview-delegate-methods-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!