SDWebImage 載入Https自簽名證書時的圖片問題
你是否遇到了這種情況,好不容易把自簽名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 12NSString *thePath = [[NSBundle mainBundle] pathForResource:@"p12證書名" ofType:@"p12"]; 13//倒入證書NSLog(@"thePath===========%@",thePath); 14NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath]; 15CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data; 16 17SecIdentityRef identity = NULL; 18// extract the ideneity from the certificate 19[self mosM_extractIdentity:inPKCS12Data toIdentity:&identity]; 20 21SecCertificateRef certificate = NULL; 22SecIdentityCopyCertificate (identity, &certificate); 27 28return [NSURLCredential credentialWithIdentity:identity certificates:nil persistence:NSURLCredentialPersistencePermanent];; 29 } 30 31 32 + (OSStatus)extractIdentity:(CFDataRef)inP12Data toIdentity:(SecIdentityRef*)identity { 33OSStatus securityError = errSecSuccess; 34CFStringRef password = CFSTR("p12證書密碼"); 35const void *keys[] = { kSecImportExportPassphrase }; 36const void *values[] = { password }; 37CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL); 38CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL); 39securityError = SecPKCS12Import(inP12Data, options, &items); 40if (securityError == 0) 41{ 42CFDictionaryRef ident = CFArrayGetValueAtIndex(items,0); 43const void *tempIdentity = NULL; 44tempIdentity = CFDictionaryGetValue(ident, kSecImportItemIdentity); 45*identity = (SecIdentityRef)tempIdentity; 46} 47if (options) { 48CFRelease(options); 49} 50return securityError; 51 }