【发布时间】:2016-04-20 10:19:14
【问题描述】:
我有以下问题:
我有一个UIWebView,它正在正确加载网站,但服务器也需要来自客户端 (UIWebView) 的身份验证。我添加了ssl certificate 以及我从另一个站点获得的以下代码:
应该StartLoadWithRequest:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType (UIWebViewNavigationType)navigationType;
{
if(![self authenticated])
{
[self setAuthenticated:NO];
[self setUrlConnection:[[NSURLConnection alloc] initWithRequest:[self requestObj] delegate:self]];
[[self urlConnection] start];
return NO;
}
return YES;
}
didReceiveAuthenticationChallenge:
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0)
{
[self setAuthenticated:YES];
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
}
else [[challenge sender] cancelAuthenticationChallenge:challenge];
}
didReceiveResponse:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
[self setAuthenticated:YES];
[[self webView] loadRequest:[self requestObj]];
[[self urlConnection] cancel];
}
canAuthenticateAgainstProtectionSpace:
-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
现在服务器需要来自客户端的身份验证(证书),具有特定的DN 名称。我找到了iOS Client Certificates and Mobile Device Management,但代码没有帮助我,也没有解决我的问题。
是否可以将 PKCS12 文件附加到我的 UIWebView,所以如果服务器想要来自客户端的身份验证,UIWebView 会向他显示此文件?
我总是得到错误
2016-04-20 12:20:50.880 App [469:126255] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)
2016-04-20 12:20:51.454 App [469:126252] CFNetwork SSLHandshake failed (-9824 -> -9829)
2016-04-20 12:20:51.456 App [469:126252] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9829)
【问题讨论】:
-
你在模拟器中测试了吗?
-
@BHASKAR 不,我已经在我的测试设备上进行了测试。我应该在模拟器上测试它吗?
标签: ios objective-c iphone uiwebview client-certificates