自从升级到 iOS 7.1 后,我的应用程序出现了一些严重问题。 webview 和身份验证挑战运行良好。由于我已经升级,webview 不再显示内容。身份验证似乎工作正常,因为我通过调试 + NSLog 获得了正确的值,所以无效的凭据不是问题(检查了大约 10 次)。在 webview 使用有效凭据重新加载页面后,仍然没有显示任何内容。这是我的代码:
- (void)viewDidLoad
{
_authed = NO;
[self.webView loadRequest:[NSURLRequest requestWithURL: [NSURL URLWithString"xxx"]]];
}
- (BOOL)webViewUIWebView *)webView shouldStartLoadWithRequestNSURLRequest *)request navigationTypeUIWebViewNavigationType)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)connectionNSURLConnection *)connection willSendRequestForAuthenticationChallengeNSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] > 0) {
}
else
{
NSURLCredential *credential = [NSURLCredential credentialWithUser"xxx"
password"xxx"
persistence:NSURLCredentialPersistencePermanent];
[[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
}
}
- (void)connectionNSURLConnection *)connection didReceiveAuthenticationChallengeNSURLAuthenticationChallenge *)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)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response;
{
NSLog(@"received response via nsurlconnection");
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString: @"xxx"]];
[self.webView loadRequest:urlRequest];
_authed = YES;
}
- (BOOL)connectionShouldUseCredentialStorageNSURLConnection *)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
提前致谢。
Best Answer-推荐答案 strong>
您可以在 url 而不是请求 header 中通过身份验证,请尝试而不是 BASE_URL:
NSString* urlString = [NSString stringWithFormat"%@//%@:%@@%@",@"http:",userName,password,myUrl]
关于ios - UIWebView 身份验证在 iOS 7.1 中不再起作用,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/23130780/
|