2013-10-04 70 views
0

http://m-qa-www.comixology.net/這是當我加載它在Safari它的偉大工程的URL。 但是當我加載它的UIWebView它不這樣做,因爲認證挑戰的事情,所以我增加了對認證支持它工作在某些網址,但它並不適用於這個上面url.following工作是我使用它的代碼。的UIWebView和認證的挑戰無法驗證

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; 
{ 
    [self showNetworkIndicator]; 
    if (!authed && !conectionAlready) { 
     authed = NO; 
     conectionAlready=YES; 
     urlCon=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 
     return YES; 
    } 
    return YES; 
} 

- (void)webViewDidStartLoad:(UIWebView *)webView 
{ 
    NSLog(@"webViewDidStartLoad"); 
    [webView stringByEvaluatingJavaScriptFromString:@"window.alert=null;"]; 
    [self showNetworkIndicator]; 
    authed=NO; 
} 

-(void) webViewDidFinishLoad:(UIWebView *)webView 
{ 
    //conectionAlready=NO; 
    //[iWebView stringByEvaluatingJavaScriptFromString:@"window.alert=null;"]; 
    NSString *currentURL = webView.request.URL.absoluteString; 
    NSLog(@"current url %@",currentURL); 
    URLBar.text=currentURL; 
    if([iWebView canGoBack]) 
    { 
     [backButton setEnabled:true]; 
    } 
    else 
    { 
     [backButton setEnabled:false]; 
    } 
    if([iWebView canGoForward]) 
    { 
     [fwdButton setEnabled:true]; 
    } 
    else 
    { 
     [fwdButton setEnabled:false]; 
    } 
    [self hideNetworkIndicator]; 
} 

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
{ 
    NSLog(@"url error ->%@",error.localizedDescription); 
    [self hideNetworkIndicator]; 
    NSString *errorStr=error.localizedDescription; 
    if ([errorStr rangeOfString:@"NSURLErrorDomain"].location==NSNotFound) { 
     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"A server with the specified hostname could not be found." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
     [alert show]; 
    } 
} 

-(void) showNetworkIndicator 
{ 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 
} 

-(void) hideNetworkIndicator 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge; 
{ 
    authed = YES; 
    authChallenge=challenge; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Aithentication Required" 
                message:scRequest.URL.absoluteString 
                delegate:self cancelButtonTitle:@"Cancel" 
              otherButtonTitles:@"Log In", nil]; 
    [alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; 
    [alert show]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 
{ 
    [connection cancel]; 
    conectionAlready=NO; 
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
    if ([response respondsToSelector: @ selector (allHeaderFields)]) { 
     NSDictionary * dictionary = [httpResponse allHeaderFields]; 
     NSLog (@ "dictionary =%@", dictionary); 
    } 
    if (authed) 
    { 
     NSLog(@"remote url returned error %d %@",[httpResponse statusCode],[NSHTTPURLResponse localizedStringForStatusCode:[httpResponse statusCode]]); 
     NSLog(@"The response is =%@",response); 
     authed=YES; 
     NSString *newUrl = [NSString stringWithFormat:@"%@", response.URL]; 
     NSLog(@"newURL%@",newUrl); 
     scURL =[NSURL URLWithString:newUrl]; 
     scRequest=[NSMutableURLRequest requestWithURL:scURL]; 
     conectionAlready=NO; 
     [iWebView loadRequest:scRequest]; 
    } 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [self hideNetworkIndicator]; 
    authed=NO; 
    conectionAlready=NO; 
    NSLog(@"oops localizedDescription:%@",error.localizedDescription); 
    NSLog(@"oops localizedFailureReason:%@",error.localizedFailureReason); 
} 

- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    //NSLog(@"textFieldAtIndex0 %@",[alert textFieldAtIndex:0].text); 
    //NSLog(@"textFieldAtIndex1 %@",[alert textFieldAtIndex:1].text); 
    if (buttonIndex==0) { 
     [[authChallenge sender] cancelAuthenticationChallenge:authChallenge]; 
    } 
    else 
    { 
     [[authChallenge sender] useCredential:[NSURLCredential credentialWithUser:[[alert textFieldAtIndex:0] text] password:[[alert textFieldAtIndex:1] text] persistence:NSURLCredentialPersistenceNone] forAuthenticationChallenge:authChallenge]; 
    } 
} 

- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection; 
{ 
    return YES; 
} 
+0

的UIWebView和認證是PITA。 – Codo

+0

我面臨同樣的問題。你找到的任何解決方案 – hariszaman

+0

對不起,我沒有得到任何解決方案。 –

回答

0

記住webView不像原生瀏覽器那麼勝任。本地瀏覽器在內部處理許多webView根本無法勝任的異常。我會建議如果你有鏈接的代碼,你試圖遍歷代碼,並找到錯誤的webView不加載。我遇到了同樣的問題,其中一些JS文件沒有響應。沒有其他方式可以在webView中查找錯誤,因爲它提供了有限的一組委託來實際遍歷。 :)

+0

如果我能在Safari應用程式中開啓這個的話,我敢肯定有一切就OK了與JS –

+0

我知道你在想什麼。但是,webView並不像Safari那麼出色。嘗試使用從App Store下載的本地Chrome應用打開相同的鏈接。 – Saify

+0

它在鉻中工作。可能是什麼問題 –

相關問題