如何在 iOS 中使用 SSL 验证

发布于 2025-01-01 18:28:02 字数 865 浏览 2 评论 0原文

我正在尝试使用可信证书,并想看看以下代码是否有效。我不拥有此代码的任何所有权,但只是想了解如何调用此委托。我已将连接设置为自我委托,但它从未调用此方法。

- (BOOL)connection:(NSURLConnection *)connection
canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *) space {
if([[space authenticationMethod] 
isEqualToString:NSURLAuthenticationMethodServerTrust]) {
// Note: this is presently only called once per server (or URL?) until
//       you restart the app
if(shouldAllowSelfSignedCert) {
return YES; // Self-signed cert will be accepted
} else {
return NO;  // Self-signed cert will be rejected
}
// Note: it doesn't seem to matter what you return for a proper SSL cert
//       only self-signed certs
}
// If no other authentication is required, return NO for everything else
// Otherwise maybe YES for NSURLAuthenticationMethodDefault and etc.
return NO;
}

如果有人能指出我正确的方向,我将非常感激。

谢谢。

I am trying to utilize a trusted Cert, and wanted to see if the following code does work. I don't take any ownership for this code, but just wanted to see how to call this delegate. I have set my connect as self delegate, but it never call's this method.

- (BOOL)connection:(NSURLConnection *)connection
canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *) space {
if([[space authenticationMethod] 
isEqualToString:NSURLAuthenticationMethodServerTrust]) {
// Note: this is presently only called once per server (or URL?) until
//       you restart the app
if(shouldAllowSelfSignedCert) {
return YES; // Self-signed cert will be accepted
} else {
return NO;  // Self-signed cert will be rejected
}
// Note: it doesn't seem to matter what you return for a proper SSL cert
//       only self-signed certs
}
// If no other authentication is required, return NO for everything else
// Otherwise maybe YES for NSURLAuthenticationMethodDefault and etc.
return NO;
}

If anyone can point me out to the right direction I will really appreciate that.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文