iOS webview 配置 HTTPS

关于2017适配HTTPS 很多人问过苹果官方,回复是还没有收到任何信息,说不用召集配置.但是大家已经认识到了HTTPS的重要性
之前看到过API afnetworking 下配置HTTPS,这里就不说啦
今天说一下webveiw下.我第一反应是懵逼的不知道怎么整.
通过
NSURLProtocol 拦截全部的webview的协议然后把下面的添加进去就好啦 root,cer 就是运维给的证书
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    static CFArrayRef certs;
    if (!certs) {
        NSData*certData =[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"root" ofType:@"cer"]];
       // SecCertificateRef rootcert =SecCertificateCreateWithData(kCFAllocatorDefault,CFBridgingRetain(certData));
        NSString * cerstr = [[NSString alloc]initWithData:certData encoding:NSUTF8StringEncoding];
        if ([cerstr hasPrefix:@"-----BEGIN CERTIFICATE-----"]||[cerstr hasSuffix:@"-----END CERTIFICATE-----"]) {
            cerstr = [cerstr stringByReplacingOccurrencesOfString:@"-----BEGIN CERTIFICATE-----" withString:@""];
            cerstr = [cerstr stringByReplacingOccurrencesOfString:@"-----END CERTIFICATE-----" withString:@""];
        }
        NSData * cerdata = [[NSData alloc]initWithBase64EncodedData:[cerstr dataUsingEncoding:NSUTF8StringEncoding] options:NSDataBase64DecodingIgnoreUnknownCharacters];
        SecCertificateRef rootcert =SecCertificateCreateWithData(kCFAllocatorDefault,CFBridgingRetain(cerdata));
        const void *array[1] = { rootcert };
        certs = CFArrayCreate(NULL, array, 1, &kCFTypeArrayCallBacks);
        CFRelease(rootcert);    // for completeness, really does not matter
    }
    
    SecTrustRef trust = [[challenge protectionSpace] serverTrust];
    int err;
    SecTrustResultType trustResult = 0;
    err = SecTrustSetAnchorCertificates(trust, certs);
    if (err == noErr) {
        err = SecTrustEvaluate(trust,&trustResult);
    }
  //  CFRelease(trust);
    BOOL trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed)||(trustResult == kSecTrustResultConfirm) || (trustResult == kSecTrustResultUnspecified));
    
    if (trusted) {
        [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
    }else{
        [challenge.sender cancelAuthenticationChallenge:challenge];
    }}

这是在测试情况下,一般权威的HTTPS 移动端是不许呀做处理的

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,641评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,477评论 19 139
  • 我所认为的朋友是需要一定时间来拉长彼此的世界那种,简单的说就是友谊也是需要时间考验的。我其实对相见恨晚并不是很认同...
    寿司用来玩aquarius阅读 1,248评论 0 1
  • 似乎想单身是件很容易的事,不找对象不得了吗?再说找不到对象的岂不都是单身人?既然我们谈到想单身的话题,也就是只侧重...
    遗弃小屋阅读 1,183评论 0 0