AF3.0
//添加證書
- (AFSecurityPolicy *)customSecurityPolicy {
// /先導入證書
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"aichewang.cer" ofType:nil]; //證書的路徑
NSData *certData = [NSData dataWithContentsOfFile:cerPath];
// AFSSLPinningModeCertificate 使用證書驗證模式
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
// allowInvalidCertificates 是否允許無效證書(也就是自建的證書)捆探,默認為NO
// 如果是需要驗證自建證書,需要設置為YES
securityPolicy.allowInvalidCertificates = YES;
// validatesDomainName 是否需要驗證域名站粟,默認為YES黍图;
//假如證書的域名與你請求的域名不一致,需把該項設置為NO奴烙;如設成NO的話助被,即服務器使用其他可信任機構頒發(fā)的證書,也可以建立連接切诀,這個非常危險揩环,建議打開。
//置為NO幅虑,主要用于這種情況:客戶端請求的是子域名丰滑,而證書上的是另外一個域名。因為SSL證書上的域名是獨立的倒庵,假如證書上注冊的域名是www.google.com褒墨,那么mail.google.com是無法驗證通過的;當然哄芜,有錢可以注冊通配符的域名*.google.com貌亭,但這個還是比較貴的。
//如置為NO认臊,建議自己添加對應域名的校驗邏輯圃庭。
securityPolicy.validatesDomainName = NO;
securityPolicy.pinnedCertificates = [[NSSet alloc] initWithObjects:certData, nil];
return securityPolicy;
}
eg:
_sessionManager = [AFHTTPSessionManager manager];
// [_sessionManager setSecurityPolicy:[self customSecurityPolicy]];
WKWebView
設置代理navigationDelegate
// https 支持
- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler{
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSURLCredential *card = [[NSURLCredential alloc]initWithTrust:challenge.protectionSpace.serverTrust];
completionHandler(NSURLSessionAuthChallengeUseCredential,card);
}
}
SDWebImage
options : SDWebImageAllowInvalidSSLCertificates
通過runtime 方法交換
import "UIImageView+JFHttps.h"
#import <UIImageView+WebCache.h>
#import <objc/runtime.h>
@implementation UIImageView (JFHttps)
+ (void)load {
Class myClass = [self class];
// 獲取SEL
SEL originSetImageSel = @selector(sd_setImageWithURL:placeholderImage:options:progress:completed:);
SEL newSetImageSel = @selector(sd_setHttpsImageWithURL:placeholderImage:options:progress:completed:);
// 生成Method
Method originMethod = class_getInstanceMethod(myClass, originSetImageSel);
Method newMethod = class_getInstanceMethod(myClass, newSetImageSel);
// 交換方法實現(xiàn)
method_exchangeImplementations(originMethod, newMethod);
}
- (void)sd_setHttpsImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
NSLog(@"這里實現(xiàn)了");
[self sd_setHttpsImageWithURL:url placeholderImage:placeholder options:SDWebImageAllowInvalidSSLCertificates progress:progressBlock completed:completedBlock];
}
@end