1. 程式人生 > >SDWebImage 加載Https自簽名證書時的圖片問題

SDWebImage 加載Https自簽名證書時的圖片問題

str values ext 找到 from ict ati implement creat

你是否遇到了這種情況,好不容易把自簽名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 }

SDWebImage 加載Https自簽名證書時的圖片問題