gpt4 book ai didi

cocoa - 如何在 Cocoa WebView 中显示(https 请求的)服务器证书信息?

转载 作者:行者123 更新时间:2023-12-03 16:19:37 24 4
gpt4 key购买 nike

在我的 ma​​c 应用中,我有一个 WebView ,并且当用户访问任何带有“>https”。单击此按钮后,我想显示证书信息。

这是我到目前为止所尝试过的...

1) 已实现 - (void)webView:(WebView *)发送者资源:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)dataSource

但是,当我访问任何带有“https”的网站时,不会调用该代表

2)由于上述失败,我编写了另一个代码(在示例应用程序中)

// Create the request.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.onlinesbi.com"]];

// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

并实现

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

{

if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
// create trust from protection space
SecTrustRef trustRef;
int trustCertificateCount = (int)SecTrustGetCertificateCount(challenge.protectionSpace.serverTrust);

NSMutableArray* trustCertificates = [[NSMutableArray alloc] initWithCapacity:trustCertificateCount];
for (int i = 0; i < trustCertificateCount; i++) {
SecCertificateRef trustCertificate = SecTrustGetCertificateAtIndex(challenge.protectionSpace.serverTrust, i);
[trustCertificates addObject:(__bridge id) trustCertificate];
}
}

}

现在,当我运行应用程序时,它会转到该委托(delegate),但证书计数为 0

3) 这个特定链接提到使用 CFNetwork API How to display certificate of HTTPS request in embedded WebView

现在我很困惑我是否真的需要进入 CFNetwork 级别并实现代码,或者我在上面 1) 和 2) 中做错了什么,因为我没有得到信息......

最佳答案

您在(2)中的代码对我来说效果很好。

 NSLog(@"%@", trustCertificates)

打印以下内容:

(
"<SecCertificate 0x100107c80 [0x7fff7bb4af00]>",
"<SecCertificate 0x100107ec0 [0x7fff7bb4af00]>",
"<SecCertificate 0x100526fe0 [0x7fff7bb4af00]>"
)

此外,导入<SecurityInterface/SFCertificatePanel.h>并在 for 之后添加以下行循环会弹出一个显示证书链的弹出窗口:

[[SFCertificatePanel sharedCertificatePanel] runModalForCertificates:trustCertificates showGroup:YES];

关于cocoa - 如何在 Cocoa WebView 中显示(https 请求的)服务器证书信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19051127/

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