2013-05-09 67 views
0

我无法加载任何网页。 任何想法,为什么这可能是? 为web视图的帧(从调试器印刷)uiwebview未加载任何内容

<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 

回答

4

使用这一个it'l工作。

problem:在您的网址http://被错过

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

@Avner Barr:它现在在工作吗? – Balu 2013-05-09 12:29:44

+0

我真的很粗心:)现在正在工作 – 2013-05-09 12:33:28

1

你没有适当的URL。 因为+ URLWithString:如果您有www.google.com,那么它就无法构建网址,因此需要使用协议(例如http://,https://), 。

NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; 
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]; 
0

只是用这个你搞定压法

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

始终确保使用的是协议输入“http://”前缀到该网页的网址字符串