uiwebview没有加载任何内容

时间:2013-05-09 12:24:00

标签: uiwebview

我无法加载任何网页。 知道为什么会这样吗? webview的框架(从调试器打印)

<UIWebView: 0x8a49840; frame = (0 0; 320 241); autoresize = TM+BM; layer = <CALayer: 0x8a498f0>>

这是我的代码:

#import "ViewController.h"

@interface ViewController () <UIWebViewDelegate>

@end

@implementation ViewController
@synthesize webview;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.webview.delegate = self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated
{

}
- (IBAction)buttonPress:(id)sender {
    NSLog(@"pressed");
    NSURL *theUrl = [NSURL URLWithString:@"www.google.com"];
    NSError *e ;
    // just for test - ALSO returning nil!!!
    NSString *str =  [NSString stringWithContentsOfURL:theUrl encoding:NSUTF8StringEncoding error:&e];
    NSLog(@"%@",str);
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:theUrl];
    [self.webview loadRequest:theRequest];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"page is loading");
}
// this method is never called
-(void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"finished loading");
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

@end

5 个答案:

答案 0 :(得分:4)

使用这个它可以工作。

您的网址problem:中的

http://错过了

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

答案 1 :(得分:1)

您没有正确的网址。 因为+ URLWithString:需要协议(例如http://,https://), 如果你有 www.google.com ,那么它无法构建网址。所以请尝试使用这样的..

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

答案 2 :(得分:1)

您还可以检查您的链接是否包含“http://”或“https://”并添加。

NSString *website = @"www.google.com";
NSRange textRangeHTTP = [[website lowercaseString] rangeOfString:@"http://"];
NSRange textRangeHTTPS = [[website lowercaseString] rangeOfString:@"https://"];
if((textRangeHTTP.location == NSNotFound) && (textRangeHTTPS.location == NSNotFound))
       website = [NSString stringWithFormat:@"http://%@",website];

NSURL *theUrl = [NSURL URLWithString:website];

答案 3 :(得分:0)

只需在按下的按下方法中使用它

 NSURL *theUrl = [NSURL URLWithString:@"http://www.google.com"];
 NSURLRequest *theRequest = [NSURLRequest requestWithURL:theUrl];
 [self.webview loadRequest:theRequest];

始终确保使用“http://”的协议类型作为web url字符串的前缀

答案 4 :(得分:0)

(Xcode 5 iOS 7)需要更新适用于iOS 7和Xcode 5的通用应用程序。它是一个开源项目/示例,位于此处:Link to SimpleWebView (Project Zip and Source Code Example)