1、NSURLRequest方式
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation NSURLRequest(ViewController)
//该方法对原生App接口访问及WebView接口访问均可
#pragma mark--允许进行https通信
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host {
return YES;
}
+ (void)setAllowsAnyHTTPSCertificate:(BOOL)allow forHost:(NSString*)host {
}
@end
2、AFNetworking
方法一:
在AFNetworking的头文件<AFNetworking.h>添加如下宏定义
#define AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES
然后在做网络请求时添加如下代码即可:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy defaultPolicy];
securityPolicy.allowInvalidCertificates = YES;
manager.securityPolicy = securityPolicy;
方法二(未测):
- AFJSONRequestOperation * op = [AFJSONRequestOperation JSONRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:jsonURL]] success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
- DLog(@"%@", JSON);
- } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
- DLog(@"%@", error);
- }];
- op.allowsInvalidSSLCertificate = YES;
- [op start];