菜鸟教程小白 发表于 2022-12-12 21:44:48

ios - UIWebView 身份验证在 iOS 7.1 中不再起作用


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

<pre><code>- (void)viewDidLoad
{
    _authed = NO;
    ]];
}

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

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

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

    if ( &gt; 0) {

    }
    else
    {

      NSURLCredential *credential = [NSURLCredential credentialWithUser:@&#34;xxx&#34;
                                                               password:@&#34;xxx&#34;
                                                            persistence:NSURLCredentialPersistencePermanent];
      [ useCredential:credential forAuthenticationChallenge:challenge];
    }

}

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

    if ( == 0) {
      _authed = YES;
      [ useCredential: forAuthenticationChallenge:challenge];
    }
    else
      [ cancelAuthenticationChallenge:challenge];
}

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

    NSURLRequest *urlRequest = ];

    ;
    _authed = YES;
}


- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection;
{
    return NO;
}
</code></pre>

<p><strong>NSLog 输出:</strong> </p>

<pre><code>2014-04-17 12:23:20.418 Test Did start loading: xxx auth:0

2014-04-17 12:23:20.704 Test received response via nsurlconnection

2014-04-17 12:23:20.710 Test Did start loading: xxx auth:1
</code></pre>

<p>提前致谢。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以在 url 而不是请求 header 中通过身份验证,请尝试而不是 BASE_URL:
NSString* urlString = </p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - UIWebView 身份验证在 iOS 7.1 中不再起作用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/23130780/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/23130780/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - UIWebView 身份验证在 iOS 7.1 中不再起作用