文章标题
2016-06-20 16:17
363 查看
ios9 中关于 ssl 访问配置
首先,在工程的plist文件中加入如下标签
生成一个NSURLRequest的Category
在实现的m文件中,使用UrlRequest访问
然后实现NSURLConnectionDelegate
首先,在工程的plist文件中加入如下标签
<key>NSAppTransportSecurity</key> <dict> <key>NSExceptionDomains</key> <dict> <key>需要访问的https的主域名(比如:www.baidu.com)</key> <dict> <key>NSIncludesSubdomains</key> <true/> <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key> <true/> <key>NSTemporaryExceptionMinimumTLSVersion</key> <string>1.0</string> <key>NSTemporaryExceptionRequiresForwardSecrecy</key> <false/> </dict> </dict> </dict>
生成一个NSURLRequest的Category
#import "NSURLRequest+NSURLRequestWithIgnoreSSL.h" @implementation NSURLRequest (NSURLRequestWithIgnoreSSL) + (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host { return YES; }
在实现的m文件中,使用UrlRequest访问
_urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
然后实现NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {} - (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection { return YES; } - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { if ([challenge previousFailureCount] == 0) { _authenticated = YES; // NSURLCredential 这个类是表示身份验证凭据不可变对象。凭证的实际类型声明的类的构造函数来确定。 NSURLCredential *cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; [challenge.sender useCredential:cre forAuthenticationChallenge:challenge]; } else { [challenge.sender cancelAuthenticationChallenge:challenge]; } } // Deprecated authentication delegates. - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; return YES; } - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; } - (void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {} #pragma mark ================= NSURLConnectionDataDelegate <NSURLConnectionDelegate> - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response { return request; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { _authenticated = YES; // webview 重新加载请求。 [self.forgetWebView loadRequest:_urlRequest]; [connection cancel]; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {}
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 访问Nginx发生SSL connection error的一种情况
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- MySQL复制解决方案(Replication Solutions)
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- Apache SSL服务器配置SSL详解
- 配置apache默认使用ssl的方法
- World Wide Web Publishing 服务尝试删除 IIS 所有的 SSL 配置数据失败的几种方法
- Apache、SSL、MySQL和PHP平滑无缝地安装
- mysql通过ssl的方式生成秘钥具体生成步骤
- 讲解iOS开发中基本的定位功能实现
- iOS中定位当前位置坐标及转换为火星坐标的方法
- js判断客户端是iOS还是Android等移动终端的方法
- iOS应用开发中AFNetworking库的常用HTTP操作方法小结
- iOS应用中UISearchDisplayController搜索效果的用法