UIWebView显示URL而不是网页

问题描述

| 我尝试将URL(字符串格式)加载到UIWebView,但没有显示网页,而是显示了URL。这是我的代码
URL = @\"www.Google.com\" 
Nsstring *path = [[NSBundle mainBundle] bundlePath]; 
baseURL = [NSURL fileURLWithPath:path];
[webView clearsContextBeforeDrawing];   
[webView loadHTMLString:URL baseURL:baseURL];
[webView setNeedsLayout];
    

解决方法

        
loadHTMLString:baseURL:
将html页面内容作为输入,而不仅仅是字符串形式的URL。它将“ 2”视为html字符串,并显示出来。你必须要做的
NSString * htmlString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@\"http://www.google.com\"] encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
    ,        您已使用字符串\'www.Google.com \'加载了UIWebView的内容,而不是该地址中的内容。 loadHTMLString将从字符串中加载HTML内容。 您需要使用loadRequest。 参见http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIWebView_Class/Reference/Reference.html     ,        使用ѭ4代替www.Google.com
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@\"www.google.com\"]];
[webView loadRequest:request];