3

在我的mac 应用程序中,我有一个网络视图,当用户访问任何带有“ https ”的网站时,我想显示一个小按钮(如在 safari 中)。单击此按钮时,我想显示证书信息。

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

1) 已实现 - (void)webView:(WebView *)sender resource:(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];
    }
}

}

现在当我运行应用程序时,它涉及到这个委托,但是证书的数量是 0

3)此特定链接提到使用 CFNetwork API如何在嵌入式 WebView 中显示 HTTPS 请求的证书

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

4

1 回答 1

3

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

 NSLog(@"%@", trustCertificates)

打印以下内容:

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

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

[[SFCertificatePanel sharedCertificatePanel] runModalForCertificates:trustCertificates showGroup:YES];
于 2014-04-24T23:02:09.433 回答