2012-06-28 34 views
1

我有這套代碼,大約一個小時左右前工作正常。然後,當我嘗試再次啓動它時,UIWebView只顯示一個白色屏幕。但是如果現在我阻止了 - (bool)方法,viewDidLoad將會發生。 (用NSLog測試過)請問代碼發生了什麼?它正在工作,突然停止運作。UIWebView模擬器突然不工作

.M

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { 

    NSString *titleString = @"Error Loading Page"; 
    NSString *messageString = [error localizedDescription]; 
    NSString *moreString = [error localizedFailureReason] ? 
    [error localizedFailureReason] : 
    NSLocalizedString(@"Try typing the URL again.", nil); 
    messageString = [NSString stringWithFormat:@"%@. %@", messageString, moreString]; 

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:titleString 
                 message:messageString delegate:self 
               cancelButtonTitle:@"Cancel" otherButtonTitles:nil]; 
    [alertView show]; 

} 

,如果我實現上述方法,當開放的UIWebView會不斷彈出錯誤說法域誤差-999不停止的應用程序。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSString *currentURL = [[request URL] absoluteString]; 
    NSRange range1= [currentURL rangeOfString:@"news"]; 
    NSRange range2= [currentURL rangeOfString:@"pdf"]; 
    if (range1.location == NSNotFound) 
    { 
     NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"]; 
     [webView loadRequest:[NSURLRequest requestWithURL:url]]; 

     return YES; 
    } 
    if(range2.location==NSNotFound) 
    { 
     NSURL * url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"]; 
     [webView loadRequest:[NSURLRequest requestWithURL:url]]; 
     return YES; 
    } 

    return NO; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    webView.scalesPageToFit=YES; 
    webView.delegate = self; 

    NSString *urlAddress = @"http://www.imc.jhmi.edu/news.html"; 
    NSURL *url =[NSURL URLWithString:urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:requestObj]; 
    NSLog(@"sadfasdf"); 
} 

.H @interface的ViewController:的UIViewController { IBOutlet中的UIWebView * web視圖; }

@property(nonatomic,strong) UIWebView*webView; 

@end 
+0

錯誤域錯誤-999 –

回答

1

當你載入的請求,委託方法

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 

將被調用。

在該方法中,您嘗試無限次地重新加載相同的頁面。

錯誤域= NSURLErrorDomain代碼= -999 「該操作不能完成。(NSURLErrorDomain誤差-999。)」 的UserInfo = 0xe203e80 {NSErrorFailingURLKey = http://www.imc.jhmi.edu/news.html,NSErrorFailingURLStringKey = http://www.imc.jhmi.edu/news.html}

我試過你的代碼,並得到這個錯誤。如果在WebView的先前請求完成之前發生另一個請求,則可能會發生此錯誤。所以請儘量避免這些委託方法中的條件重複加載請求。

if(range2.location==NSNotFound) 

這種情況將永遠是真實的。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSString *currentURL = [[request URL] absoluteString]; 
    NSRange range1= [currentURL rangeOfString:@"news"]; 
    if (range1.location == NSNotFound) 
    { 
     NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"]; 
     [webView loadRequest:[NSURLRequest requestWithURL:url]]; 
     return YES; 
    } 

    return YES; 
} 
+0

是的,我注意到了。但是,如果我刪除了range2部分,webview仍然沒有加載。只有當我刪除了所有內容並且只將其作爲'return yes;'然後webview將會加載 –

+0

請轉換該委託方法就像我的答案中的代碼一樣,它對我很有用。 :) – Neo

+0

它似乎停止工作,如果我把另一個如果我的range2聲明。但如果僅將其作爲範圍1,則工作得很好 –

0

的UIWebView的委託方法:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request  navigationType:(UIWebViewNavigationType)navigationType{ 
} 

返回YES如果web視圖應該否則開始加載內容返回NO。該方法被調用,在viewDidLoad方法,當你撥打:

[webView loadRequest:requestObj]; 

而且在委託方法你再次調用同一個方法:

[webView loadRequest:[NSURLRequest requestWithURL:url]]; 

這變成調用委託方法再次打來電話,再次形成一個無限循環。這發生在loadRequest每次加載之前,再次調用相同的loadRequest,形成循環。

在這個方法中不需要loadRequest,只需返回YES或NO就可以了。