0

http://m-qa-www.comixology.net/这是我在 safari 上加载它时的 url,效果很好。但是当我在 uiwebview 上加载它时,由于身份验证挑战,它没有做任何事情,所以我添加了对身份验证的支持,它适用于某些 url,但它不适用于上面的 url。以下是我正在使用的代码。

- (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;
}
4

1 回答 1

0

请记住,webView 不如原生浏览器那么胜任。本机浏览器在内部处理许多 webView 根本无法胜任的异常。我建议您是否有您正在点击的链接的代码,而不是尝试遍历代码并查找 webView 未加载的错误。我遇到了同样的问题,我的一些 JS 文件没有响应。没有其他方法可以在 webView 中找到错误,因为它提供了一组有限的委托来实际遍历。:)

于 2013-10-04T13:15:57.797 回答