gpt4 book ai didi

iphone - iOS https认证

转载 作者:太空狗 更新时间:2023-10-30 03:50:10 26 4
gpt4 key购买 nike

我希望我的应用程序针对设备进行身份验证,即通过 https 传递用户名和密码。

我看了几个例子,基本上构建了以下内容:

-(void)authentication
{
//setting the string of the url taking from appliance IP.
NSString *urlString =[NSString
stringWithFormat:
@"http://%@",appliance_IP.text];
//define the url with the string
NSURL *url = [NSURL URLWithString:urlString];

//creating request
NSURLRequest *request = [NSURLRequest requestWithURL:url];

//setting credential taking from the username and password field
NSURLCredential *credential = [NSURLCredential credentialWithUser:username.text password:password.text persistence:NSURLCredentialPersistenceForSession];


NSURLProtectionSpace *protectionSpace=
[[NSURLProtectionSpace alloc]initWithHost:urlString port:443 protocol:@"https" realm:nil authenticationMethod:nil ];


[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential forProtectionSpace:protectionSpace];

[ NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];

}

因为我使用的示例我需要更多理解,上面示例中的 NSURLConnection 不包含用户名和密码,我该如何添加?因此请求还将包含用户名和密码,或者传递该信息可能更好。目前请求只包含url字符串。

谢谢错误

最佳答案

当发出需要身份验证的请求时,您将收到 didReceiveAuthenticationChallenge 的回调,您的处理如下:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
if ([challenge.protectionSpace.host isEqualToString:MY_PROTECTION_SPACE_HOST])
{
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}

[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
else if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic])
{
if ([challenge previousFailureCount] == 0)
{
NSURLCredential *newCredential;

newCredential = [NSURLCredential credentialWithUser:MY_USERNAME
password:MY_PASSWORD
persistence:NSURLCredentialPersistenceForSession];

[[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge];
}
else
{
[[challenge sender] cancelAuthenticationChallenge:challenge];

// inform the user that the user name and password
// in the preferences are incorrect

NSLog (@"failed authentication");

// ...error will be handled by connection didFailWithError
}
}
}

关于iphone - iOS https认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10063224/

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