gpt4 book ai didi

ios - UIWebview didReceiveAuthenticationChallenge

转载 作者:可可西里 更新时间:2023-11-01 05:37:39 36 4
gpt4 key购买 nike

我正在使用 webview 加载网站。我设法运行了这些应用程序。但我的问题是,如果我传递了错误的密码,我将无法加载网站,它只会显示白屏。

如果我传递正确的用户名/密码,它将加载该网站。有没有办法处理我的身份验证用户名/密码正确或错误?

我正在使用这段代码。

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodNTLM])
{
NSURLCredential *credentail = [NSURLCredential
credentialWithUser:@"username"
password:@"Password"
persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge];
}}

正确的用户名和密码包含在上面的代码中。如果我将用户名更改为“user”或密码更改为“pass”,我将无法加载该网站。如何捕获身份验证错误?

谢谢。

最佳答案

我刚找到。只需要再添加 1 个验证。 [[challenge previousFailureCount] == 0. 这是代码。

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
//receive a authenticate and challenge with the user credential
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodNTLM] &&
[challenge previousFailureCount] == 0)
{
NSURLCredential *credentail = [NSURLCredential
credentialWithUser:@"username"
password:@"password"
persistence:NSURLCredentialPersistenceForSession];


[[challenge sender] useCredential:credentail forAuthenticationChallenge:challenge];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error Message" message:@"Invalid credentails" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}

谢谢。 :)

关于ios - UIWebview didReceiveAuthenticationChallenge,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12702240/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com