小编Mar*_*ark的帖子

iOS:使用UIWebView进行HTTP Basic/Digest Auth

概观

我正在为一个iOS应用程序进行SAML登录(单点登录,类似于openID)解决方案,该解决方案涉及显示带有a的视图控制器,UIWebView并且在处理HTTP基本/摘要时我遇到了计时和/或超时问题认真的UIWebView.

具体来说,当客户端获得HTTP身份验证质询时,我会弹出UIAlertView提示用户输入用户ID和密码的信息.如果用户能够快速输入信息(<10秒),则可以正常工作.但是,如果条目超过10秒,则连接似乎已终止且没有任何反应.

问题

  1. 调用超时是否connection:didReceiveAuthenticationChallenge:会阻止我提示用户输入用户ID和密码(并且必须等待用户输入)?有没有人有解决方法(例如某种方式来延长连接超时)?
  2. 有没有更好的方法来处理来自UIWebView一个子类的HTTP基本/摘要auth NSURLProtocol

细节和代码

对于我们需要处理的大多数SAML系统,登录将显示为常规网页UIWebView.但是,我们需要处理的一些系统可以回退到使用移动浏览器的HTTP基本或HTTP摘要身份验证,因此我们也需要能够处理它.

最大的挑战始于UIWebView不会暴露下面的网络调用.为了得到我需要的东西,我已经创建了一个子类NSURLProtocol并在必要时注册了它:

[NSURLProtocol registerClass:[SMURLProtocol class]];
Run Code Online (Sandbox Code Playgroud)

这样,SMURLProtocol当发出HTTP基本/ auth质询时,会调用此方法,因此我返回YES,我们可以处理HTTP基本和摘要式身份验证:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return ([protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest]
        || [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]);
}
Run Code Online (Sandbox Code Playgroud)

现在我告诉网络堆栈SMURLProtocol可以处理auth挑战,所以它调用

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];
NSString *authenticationMethod = [protectionSpace authenticationMethod];

if ([authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]
    || [authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest]) {
    // Stash the challenge in an IVAR so we can use …
Run Code Online (Sandbox Code Playgroud)

objective-c uiwebview nsurlprotocol ios nsurlconnectiondelegate

6
推荐指数
0
解决办法
2492
查看次数