uiwebview not loading the web page

可紊 提交于 2019-12-08 13:52:28

问题


in my program uiwebview is not loading the url address.when i nslogged the request object is not null.however when i nslog the webview.request it returns null.what may be the reason it is not loading

 - (void)viewDidLoad {
        [super viewDidLoad];

        self.web = [[UIWebView alloc] init];
        NSString *urlAddress = @"http://www.google.com";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

        NSLog(@"%@",requestObj);
        [self.web loadRequest:requestObj];
        [web setFrame:CGRectMake(0, 0, 320, 370)];
        NSLog(@"%@   %@",self.web,self.web.request);


    }

the nslog results are

<NSURLRequest http://www.google.com>
 <UIWebView: 0xbb17ff0; frame = (0 0; 320 370); layer = <CALayer: 0xbb12f20>>   (null)

as per suggestions from this site i changed it from IB and made it to code .i made the class confirm to uiwebview delegate..both the webviewdidstart and webview did finish are being called these are the nslog outputs from these methods

webviewdidstart

webView-----><NSMutableURLRequest >
webView-----><NSMutableURLRequest http://www.google.com>

webview did finish

webView-finished----><NSMutableURLRequest http://www.google.com>
webView-finished----><NSMutableURLRequest http://www.google.com>

still nothing is being called and i think both these methods are called twice


回答1:


This is a working code, test this out in your application and let us know the outcome

in .h file, add UIWebViewDelegate

in .m file's viewDidLoad, add this:

UIWebView *webview=[[UIWebView alloc] initWithFrame:CGRectMake(0.0,0.0,320.0,460.0)];
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
webview.delegate = self;
[webview loadRequest:requestObj];
[self.view addSubview:webview];

Now check in the delegates if your page is loading or not:

- (void)webViewDidStartLoad:(UIWebView *)webView {     
   NSLog(@"page is loading");       
 }

-(void)webViewDidFinishLoad:(UIWebView *)webView {
       NSLog(@"finished loading");
 }



回答2:


try like this

 CGRect webFrame = CGRectMake(0.0, 0.0, 320.0, 460.0);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];

NSString *urlAddress = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
 [webView loadRequest:requestObj]; 
[self addSubview:webView]; 
[webView release];


来源:https://stackoverflow.com/questions/6544495/uiwebview-not-loading-the-web-page

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