【问题标题】:UIWebView and authentication challenge unable to authenticateUIWebView 和身份验证质询无法进行身份验证
【发布时间】:2013-10-04 13:01:32
【问题描述】:

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

【问题讨论】:

  • UIWebView 和身份验证是 PITA。
  • 我也面临同样的问题。你找到的任何解决方案
  • 对不起,我没有得到任何解决方案。

标签: ios security uiwebview


【解决方案1】:

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

【讨论】:

  • 如果我可以在 safari 应用程序中打开它,那么我确信 js 一切正常
  • 我知道你的想法。但是 webView 的能力不如 Safari。尝试使用从 App Store 下载的原生 Chrome 应用打开相同的链接。
  • 您是否检查过是否调用了 webViewDilFailedLoading 委托或 webViewDidFinishLoading ?也许你可以在那里得到错误的提示。
  • 是的,在验证 didReceived 后被调用,然后我正在加载请求它转到 webViewDilFailedLoading 方法它不调用 webViewDidFinishLoading
  • 实际上我尝试使用 safari safari 向我显示m-qa-www.comixology.net url 的弹出窗口,当我输入凭据时,它再次显示带有其他 url qa-www.images.comixology.net 的弹出窗口,当我输入它会在 safari 上加载页面。
猜你喜欢
  • 2013-05-30
  • 2015-09-23
  • 2016-04-26
  • 2016-01-11
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
  • 1970-01-01
相关资源
最近更新 更多