React Native开发技术干货React Native实践

react-native ios 集成httpdns

2017-09-07  本文已影响264人  45b645c5912e

react-native 项目 iOS端集成httpdns

集成步骤

原生入口基本配置

/** httpdns */
  // 初始化HTTPDNS
  // 设置AccoutID
  HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:xxxxx];
  //鉴权方式初始化
  //HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:0000 secretKey:@"XXXX"];
  
  // 为HTTPDNS服务设置降级机制
//  [httpdns setDelegateForDegradationFilter:self];
  // 允许返回过期的IP
  [httpdns setExpiredIPEnabled:YES];
  // 打开HTTPDNS Log,线上建议关闭
  [httpdns setLogEnabled:YES];
  /*
   *  设置HTTPDNS域名解析请求类型(HTTP/HTTPS),若不调用该接口,默认为HTTP请求;
   *  SDK内部HTTP请求基于CFNetwork实现,不受ATS限制。
   */
  //[httpdns setHTTPSRequestEnabled:YES];
  // edited
  NSArray *preResolveHosts = @[ @"www.xxx.cn", @"www.xxx.cn"];
  // NSArray* preResolveHosts = @[@"pic1cdn.igetget.com"];
  // 设置预解析域名列表
  [httpdns setPreResolveHosts:preResolveHosts];
/*
 * 降级过滤器,您可以自己定义HTTPDNS降级机制
 */
- (BOOL)shouldDegradeHTTPDNS:(NSString *)hostName {
    NSLog(@"Enters Degradation filter.");
    // 根据HTTPDNS使用说明,存在网络代理情况下需降级为Local DNS
    if ([NetworkManager configureProxies]) {
        NSLog(@"Proxy was set. Degrade!");
        return YES;
    }
    
    // 假设您禁止"www.taobao.com"域名通过HTTPDNS进行解析
    if ([hostName isEqualToString:@"www.taobao.com"]) {
        NSLog(@"The host is in blacklist. Degrade!");
        return YES;
    }
    
    return NO;
}

修改react-native 网络请求库

WechatIMG52.jpeg
#pragma mark - NSURLSessionTaskDelegate
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *_Nullable))completionHandler {
  if (!challenge) {
    return;
  }
  NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  NSURLCredential *credential = nil;
  /*
   * 获取原始域名信息。
   */
  NSString *host = [[self.request allHTTPHeaderFields] objectForKey:@"host"];
  if (!host) {
    host = self.request.URL.host;
  }
  if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
    if ([self evaluateServerTrust:challenge.protectionSpace.serverTrust forDomain:host]) {
      disposition = NSURLSessionAuthChallengeUseCredential;
      credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
    } else {
      disposition = NSURLSessionAuthChallengePerformDefaultHandling;
    }
  } else {
    disposition = NSURLSessionAuthChallengePerformDefaultHandling;
  }
  // 对于其他的challenges直接使用默认的验证方案
  completionHandler(disposition, credential);
}
- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
                  forDomain:(NSString *)domain {
  /*
   * 创建证书校验策略
   */
  NSMutableArray *policies = [NSMutableArray array];
  if (domain) {
    [policies addObject:(__bridge_transfer id) SecPolicyCreateSSL(true, (__bridge CFStringRef) domain)];
  } else {
    [policies addObject:(__bridge_transfer id) SecPolicyCreateBasicX509()];
  }
  /*
   * 绑定校验策略到服务端的证书上
   */
  SecTrustSetPolicies(serverTrust, (__bridge CFArrayRef) policies);
  /*
   * 评估当前serverTrust是否可信任,
   * 官方建议在result = kSecTrustResultUnspecified 或 kSecTrustResultProceed
   * 的情况下serverTrust可以被验证通过,https://developer.apple.com/library/ios/technotes/tn2232/_index.html
   * 关于SecTrustResultType的详细信息请参考SecTrust.h
   */
  SecTrustResultType result;
  SecTrustEvaluate(serverTrust, &result);
  return (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed);
}
上一篇 下一篇

猜你喜欢

热点阅读