关于IOS 中HTTPS协议(过滤信任检测)

   在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;

这篇文章也是我开发中遇到的难点,现在分享给大家,希望可以帮到大家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值