2013-05-09 57 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://」前綴到該網頁的網址字符串