ios - UIWebView 身份验证在 iOS 7.1 中不再有效

标签 ios uiwebview basic-authentication

自升级到 iOS 7.1 后,我的应用程序出现了一些严重问题。 WebView 和身份验证质询工作正常。由于我已升级,webview 不再显示内容。身份验证似乎工作正常,因为我通过调试 + NSLog 获得了正确的值,因此无效的凭据不是问题(检查了大约 10 次)。在 webview 使用有效凭据重新加载页面后,仍然没有显示任何内容。这是我的代码:

- (void)viewDidLoad
{
    _authed = NO;
    [self.webView loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString:@"xxx"]]];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
    NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authed);

    if (!_authed) {
        _authed = NO;
        [[NSURLConnection alloc] initWithRequest:request delegate:self];
        return NO;
    }
    return YES;
}

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{

    if ([challenge previousFailureCount] > 0) {

    }
    else
    {

        NSURLCredential *credential = [NSURLCredential credentialWithUser:@"xxx"
                                                                 password:@"xxx"
                                                              persistence:NSURLCredentialPersistencePermanent];
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    }

}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
{
    NSLog(@"got auth challange");

    if ([challenge previousFailureCount] == 0) {
        _authed = YES;
        [[challenge sender] useCredential:[NSURLCredential credentialWithUser:@"xxx" password:@"xxx"  persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];
    }
    else
        [[challenge sender] cancelAuthenticationChallenge:challenge];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
    NSLog(@"received response via nsurlconnection");

    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: @"xxx"]];

    [self.webView loadRequest:urlRequest];
    _authed = YES;
}


- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
{
    return NO;
}

NSLog输出:

2014-04-17 12:23:20.418 Test[510:60b] Did start loading: xxx auth:0

2014-04-17 12:23:20.704 Test[510:60b] received response via nsurlconnection

2014-04-17 12:23:20.710 Test[510:60b] Did start loading: xxx auth:1

提前致谢。

最佳答案

您可以在 url 中而不是请求 header 中传递身份验证请尝试代替 BASE_URL: NSString* urlString = [NSString stringWithFormat:@"%@//%@:%@@%@",@"http:",userName,password,myUrl]

关于ios - UIWebView 身份验证在 iOS 7.1 中不再有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23130780/

相关文章:

基于 NodeJS 的 HTTP 客户端 : How to authenticate a request?

ios - 自定义 View - 在 "required init?(coder aDecoder: NSCoder)"方法中崩溃

javascript - 如何使用注入(inject)的 JavaScript 将 UIWebView 滚动到一个点

ios - react native 套接字io不会从客户端发出任何事件

iphone - setDefaultCredential 不适用于 iOS 7 中的 UIWebView,但在早期的 iO​​S 版本中工作正常

ios - 播放音频时设置自定义元数据(控制中心、锁屏...)

ios - 如何处理 AVURLAsset 中的 HTTP 基本身份验证?

powershell - 使用 Invoke-WebRequest 以及用户名和密码在 GitHub API 上进行基本身份验证

c# - iOS 从本地通知打开页面

iOS : How to create view which is top of all screen