内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

SDWebImage加载Https自签名证书时的图片问题

2018-11-07 18:25 出处:清屏网 人气: 评论(0

你是否遇到了这种情况,好不容易把自签名HTTPS证书配置好了,访问https接口也成功了,但是图片加载不出来?

传了SDWebImageAllowInvalidSSLCertificates 还是没效果没效果(这种情况只适用于CA我觉得),

并且一直 HTTP load failed (error code: -999 [1:89]),

经过不懈努力,终于找到了在不修改SDWebimageDownloader.m源码的情况下的解决方案;

通过创建SDWebimageDownloader的分类来实现,如下:

.h 文件

1 #import "SDWebImageDownloader.h"
2 
3 @interface SDWebImageDownloader (AFNHttps)
4 
5 @end
 1 #import <SDWebImageDownloader.h>
 2 #import "SDWebImageDownloader+AFNHttps.h"
 3 
 4 @implementation SDWebImageDownloader (AFNHttps)
 5 
 6 + (void)load {
     //设置SDWebImageDownloader的证书
 7     [SDWebImageDownloader sharedDownloader].urlCredential = [self myUrlCredential];
 8 }
 9 
10 + (NSURLCredential *)myUrlCredential {
11     
12     NSString *thePath = [[NSBundle mainBundle] pathForResource:@"p12证书名" ofType:@"p12"];
13     //倒入证书       NSLog(@"thePath===========%@",thePath);
14     NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
15     CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;
16     
17     SecIdentityRef identity = NULL;
18     // extract the ideneity from the certificate
19     [self mosM_extractIdentity:inPKCS12Data toIdentity:&identity];
20     
21     SecCertificateRef certificate = NULL;
22     SecIdentityCopyCertificate (identity, &certificate);
27     
28     return [NSURLCredential credentialWithIdentity:identity certificates:nil persistence:NSURLCredentialPersistencePermanent];;
29 }
30 
31 
32 + (OSStatus)extractIdentity:(CFDataRef)inP12Data toIdentity:(SecIdentityRef*)identity {
33     OSStatus securityError = errSecSuccess;
34     CFStringRef password = CFSTR("p12证书密码");
35     const void *keys[] = { kSecImportExportPassphrase };
36     const void *values[] = { password };
37     CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);
38     CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
39     securityError = SecPKCS12Import(inP12Data, options, &items);
40     if (securityError == 0)
41     {
42         CFDictionaryRef ident = CFArrayGetValueAtIndex(items,0);
43         const void *tempIdentity = NULL;
44         tempIdentity = CFDictionaryGetValue(ident, kSecImportItemIdentity);
45         *identity = (SecIdentityRef)tempIdentity;
46     }
47     if (options) {
48         CFRelease(options);
49     }
50     return securityError;
51 }
分享给小伙伴们:
本文标签: SDWebImageHttps

相关文章

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

CopyRight © 2015-2016 QingPingShan.com , All Rights Reserved.

清屏网 版权所有 豫ICP备15026204号