在IOS 开发中请求我们一般都选用的是HTTP协议,很少用到HTTPS协议,但是有时候涉及到安全方面的时候,我们就要用到HTTPS协议了,(iOS 终端请求服务端数据时,为了保证数据安全,我们一般会使用https协议加密)。
我封装了一个类,只要大家导入了这个类便迎刃而解了:
类的.h
#import <UIKit/UIKit.h>
@interface BrowserController : UIViewController
{
UIWebView *webView;
NSURL *_currenURL;
NSURLConnection* reUrlConnection;
NSURLRequest* originRequest;
}
@property(nonatomic,assign,getter =isAuthed)BOOL authed;
@property(nonatomic,strong)NSURL *currenURL;
- (void)loadUrl:(NSString *)url;
@end
类的.m
#import "BrowserController.h"
//屏幕宽度
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
//屏幕高度
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
//导航栏高度
#define NAVIGATION_BAR_HEIGHT 44.0f
//状态栏高度
#define STATUS_BAR_HEIGHT 20.0f
//工具栏高度
#define TOOL_BAR_HEIGHT 30
#define HTTPS @"https"
@interface BrowserController ()<UIWebViewDelegate,NSURLConnectionDelegate>
@end
@implementation BrowserController
-(void)loadView{
[super loadView];
if (webView == nil)
{
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, SCREEN_WIDTH, SCREEN_HEIGHT - NAVIGATION_BAR_HEIGHT - STATUS_BAR_HEIGHT - 30)];
webView.delegate = self;
[self.view addSubview:webView];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
if(_currenURL)
{
NSURLRequest *request = [NSURLRequest requestWithURL:_currenURL];
[MBProgressHUD showMessage:@"加载中..." toView:self.view];
[webView loadRequest:request];
}
}
- (void)loadUrl:(NSString *)url
{
if (webView)
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[webView loadRequest:request];
}
}
- (void)loadURLof:(NSURL *)url
{
self.currenURL = url;
}
- (BOOL)webView:(UIWebView *)awebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString* scheme = [[request URL] scheme];
if ([scheme isEqualToString:HTTPS]) {
if (!self.isAuthed) {
originRequest = request;
NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];
[awebView stopLoading];
return NO;
}
}
NSURL *theUrl = [request URL];
self.currenURL = theUrl;
return YES;
}
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount]== 0) {
_authed = YES;
NSURLCredential* cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:cre forAuthenticationChallenge:challenge];
} else {
[challenge.sender cancelAuthenticationChallenge:challenge];
}
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
return [protectionSpace.authenticationMethod
isEqualToString:NSURLAuthenticationMethodServerTrust];
return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if (([challenge.protectionSpace.authenticationMethod
isEqualToString:NSURLAuthenticationMethodServerTrust])) {
if ([challenge.protectionSpace.host isEqualToString:@"111.200.87.69:443/ccard"]) {
NSURLCredential *credential = [NSURLCredential credentialForTrust:
challenge.protectionSpace.serverTrust];
[challenge.sender useCredential:credential
forAuthenticationChallenge:challenge];
}
}
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.authed = YES;
[webView loadRequest:originRequest];
[connection cancel];
}
剩下的就是我们调用我们封装好的类了:
NSString * str1 = @"https://.........";
NSURL* url = [NSURL URLWithString:str1];
BrowserController* b = [BrowserController new];
b.currenURL = url;
这篇文章也是我开发中遇到的难点,现在分享给大家,希望可以帮到大家。