setDefaultCredential不适用于iOS 7中的UIWebView,但在早期的iOS版本中运行良好

jbr*_*837 6 iphone objective-c uiwebview ios

我目前正在使用以下代码为UIWebView设置默认凭据.这在iOS 6.1及更早版本中运行良好.但是,在iOS 7 Beta 6中,它根本不起作用.

我尝试加载的网页使用Windows身份验证.我可以在iOS 7中的Safari中打开它们.但是,当我运行下面的代码然后在UIWebView中打开URL时,我得到一个空的白色矩形,什么都没有加载!就像我说的,这在iOS 6.1及更早版本中完美运行.

我还尝试了第二种方法,涉及使用NSURLConnectionDelegate传递凭证.第二种方法在iOS 6.1及更早版本中也可以正常工作,但在iOS 7中已经破解.

有谁知道为什么会这样?类似的经历?思考?

// Authenticate
NSURLCredential *credential = [NSURLCredential credentialWithUser:@"myusername"
                                                         password:@"mypassword"
                                                      persistence:NSURLCredentialPersistenceForSession];

NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
                                         initWithHost:@"mysite.com"
                                         port:80
                                         protocol:@"http"
                                         realm:nil
                                         authenticationMethod:NSURLAuthenticationMethodDefault];

[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];
Run Code Online (Sandbox Code Playgroud)

小智 4

我遇到了同样的问题 - 为 Windows 身份验证配置的到 Sharepoint 站点的 NSURLConnection 在 iOS 6.1 中工作正常。在 iOS 7 中,无论我是针对 6 还是 7 构建应用程序,所有身份验证似乎都会成功(接收正确的 cookie),但仍然会返回 401 响应;所有使用 cookie 发送的后续请求也将收到 401。

我通过转储 didReceiveAuthenticationChallenge 委托协议以支持 willSendRequestForAuthenticationChallenge 解决了该问题。实现第二个委托协议意味着第一个委托协议永远不会被调用。

在您的委托中,实现此委托协议:

- (void)connection:(NSURLConnection *)sender willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
   if ([challenge previousFailureCount] > 0]) {
       [[challenge sender] cancelAuthenticationChallenge:challenge];
   }else{
      NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLPersistenceForSession];
   [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
   }
}
Run Code Online (Sandbox Code Playgroud)

在我一时兴起实现了这个之后,我的 iOS 7 NTLM 身份验证问题就消失了。