react-native 項(xiàng)目 iOS端集成httpdns
- 官方文檔 https://help.aliyun.com/document_detail/30143.html
- Demo地址 https://github.com/aliyun/alicloud-ios-demo/tree/master/httpdns_ios_demo
集成步驟
- 手動(dòng)集成
- 下載SDK,將組建包拖入項(xiàng)目中
AlicloudHttpDNS.framework
AlicloudUtils.framework
UTDID.framework
- 添加系統(tǒng)庫(kù)
libresolv.tbd
CoreTelephony.framework
SystemConfiguration.framework
- 下載SDK,將組建包拖入項(xiàng)目中
- Pod集成
- 指定Master倉(cāng)庫(kù)和阿里云倉(cāng)庫(kù):
source 'https://github.com/CocoaPods/Specs.git'
source 'https://github.com/aliyun/aliyun-specs.git'
- 添加依賴:
pod 'AlicloudHTTPDNS', '~> 1.5.2'
- 指定Master倉(cāng)庫(kù)和阿里云倉(cāng)庫(kù):
- 修改編譯選項(xiàng)
- 在
Target->Build Setting->Linking->Other Linker Flags
中添加 -ObjC 選項(xiàng)蛾娶。
- 在
原生入口基本配置
-
AppDelegate
入口處添加如下配置代碼
/** httpdns */
// 初始化HTTPDNS
// 設(shè)置AccoutID
HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:xxxxx];
//鑒權(quán)方式初始化
//HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:0000 secretKey:@"XXXX"];
// 為HTTPDNS服務(wù)設(shè)置降級(jí)機(jī)制
// [httpdns setDelegateForDegradationFilter:self];
// 允許返回過期的IP
[httpdns setExpiredIPEnabled:YES];
// 打開HTTPDNS Log灯谣,線上建議關(guān)閉
[httpdns setLogEnabled:YES];
/*
* 設(shè)置HTTPDNS域名解析請(qǐng)求類型(HTTP/HTTPS),若不調(diào)用該接口蛔琅,默認(rèn)為HTTP請(qǐng)求胎许;
* SDK內(nèi)部HTTP請(qǐng)求基于CFNetwork實(shí)現(xiàn),不受ATS限制。
*/
//[httpdns setHTTPSRequestEnabled:YES];
// edited
NSArray *preResolveHosts = @[ @"www.xxx.cn", @"www.xxx.cn"];
// NSArray* preResolveHosts = @[@"pic1cdn.igetget.com"];
// 設(shè)置預(yù)解析域名列表
[httpdns setPreResolveHosts:preResolveHosts];
- 如果需要降級(jí)機(jī)制實(shí)現(xiàn)需要遵循
HttpDNSDegradationDelegate
協(xié)議實(shí)現(xiàn)下面的代理方法
/*
* 降級(jí)過濾器辜窑,您可以自己定義HTTPDNS降級(jí)機(jī)制
*/
- (BOOL)shouldDegradeHTTPDNS:(NSString *)hostName {
NSLog(@"Enters Degradation filter.");
// 根據(jù)HTTPDNS使用說明钩述,存在網(wǎng)絡(luò)代理情況下需降級(jí)為L(zhǎng)ocal DNS
if ([NetworkManager configureProxies]) {
NSLog(@"Proxy was set. Degrade!");
return YES;
}
// 假設(shè)您禁止"www.taobao.com"域名通過HTTPDNS進(jìn)行解析
if ([hostName isEqualToString:@"www.taobao.com"]) {
NSLog(@"The host is in blacklist. Degrade!");
return YES;
}
return NO;
}
修改react-native 網(wǎng)絡(luò)請(qǐng)求庫(kù)
WechatIMG52.jpeg
- 首先根據(jù)上圖步驟進(jìn)行到第四步,添加一個(gè)路徑
$(SRCROOT)/../../../../ios
(否則無法再Libraries
下的依賴庫(kù)下訪問主項(xiàng)目中剛剛添加的SDK) - 進(jìn)入標(biāo)記5中
RCTHTTPRequestHandler.mm
文件- 首先引入
#import <AlicloudHttpDNS/AlicloudHttpDNS.h>
上述步驟路徑如果配置錯(cuò)誤穆碎,這里將會(huì)報(bào)文件找不到的錯(cuò)誤 - 實(shí)現(xiàn)一個(gè)
NSMutableURLRequest
屬性用來保存發(fā)送的request
@property (nonatomic, strong) NSMutableURLRequest *request;
- 在數(shù)據(jù)發(fā)起request請(qǐng)求方法中(
- (NSURLSessionDataTask *)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate>)delegate
)獲取IP并替換 - 由于ios目前都是https請(qǐng)求牙勘,所以進(jìn)行一步攔截證書的操作,遵循
NSURLSessionTaskDelegate
協(xié)議并實(shí)現(xiàn)下面的代理方法
- 首先引入
#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;
}
// 對(duì)于其他的challenges直接使用默認(rèn)的驗(yàn)證方案
completionHandler(disposition, credential);
}
- (BOOL)evaluateServerTrust:(SecTrustRef)serverTrust
forDomain:(NSString *)domain {
/*
* 創(chuàng)建證書校驗(yàn)策略
*/
NSMutableArray *policies = [NSMutableArray array];
if (domain) {
[policies addObject:(__bridge_transfer id) SecPolicyCreateSSL(true, (__bridge CFStringRef) domain)];
} else {
[policies addObject:(__bridge_transfer id) SecPolicyCreateBasicX509()];
}
/*
* 綁定校驗(yàn)策略到服務(wù)端的證書上
*/
SecTrustSetPolicies(serverTrust, (__bridge CFArrayRef) policies);
/*
* 評(píng)估當(dāng)前serverTrust是否可信任方面,
* 官方建議在result = kSecTrustResultUnspecified 或 kSecTrustResultProceed
* 的情況下serverTrust可以被驗(yàn)證通過,https://developer.apple.com/library/ios/technotes/tn2232/_index.html
* 關(guān)于SecTrustResultType的詳細(xì)信息請(qǐng)參考SecTrust.h
*/
SecTrustResultType result;
SecTrustEvaluate(serverTrust, &result);
return (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed);
}
大功告成北秽,附上這段代碼的截圖
- WechatIMG53.jpeg
- WechatIMG54.jpeg
- WechatIMG55.jpeg
- WechatIMG56.jpeg