调用系统底层Dns解析库,可以使用 114 等第三方 dns 解析,可以使用 Doh 协议的 Dns 解析方案,也可以集成 dnspod 等 httpdns。另外也有丰富的 hosts 域名配置。
通过 CocoaPods
pod "HappyDNS"
通过 Swift Package Manager (Xcode 11+)
App 对接:
File -> Swift Packages -> Add Package Dependency,输入 HappyDNS 库链接,选择相应版本即可
库链接: https://github.com/qiniu/happy-dns-objc
库对接:
let package = Package(
dependencies: [
.package(url: "https://github.com/qiniu/happy-dns-objc", from: "1.0.4")
],
// ...
)
* 返回 IP 列表
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[QNResolver systemResolver]];
[array addObject:[[QNResolver alloc] initWithAddress:@"119.29.29.29"]];
[array addObject:[QNDohResolver resolverWithServer:@"https://dns.alidns.com/dns-query"]];
QNDnsManager *dns = [[QNDnsManager alloc] init:array networkInfo:[QNNetworkInfo normal]];
NSArray <QNRecord *> *records = [dns queryRecords:@"www.qiniu.com"];
* url 请求,返回一个IP 替换URL 里的domain
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[QNResolver systemResolver]];
[array addObject:[[QNResolver alloc] initWithAddress:@"119.29.29.29"]];
QNDnsManager *dns = [[QNDnsManager alloc] init:array networkInfo:[QNNetworkInfo normal]];
NSURL *u = [[NSURL alloc] initWithString:@"rtmp://www.qiniu.com/abc?q=1"];
NSURL *u2 = [dns queryAndReplaceWithIP:u];
static QNDnsManager *dns = nil;
dns = [[QNDnsManager alloc] init:@[ [QNResolver systemResolver] ] networkInfo:nil];
[QNDnsManager setGetAddrInfoBlock:^NSArray *(NSString *host) {
return [dns query:host];
}];
struct addrinfo hints = {0};
struct addrinfo *ai = NULL;
int x = qn_getaddrinfo(host, "http", &hints, &ai);
qn_freeaddrinfo(ai); // 也可以用系统的freeaddrinfo, 代码一致,不过最好用这个
$ xctool -workspace HappyDNS.xcworkspace -scheme "HappyDNS_Mac" -sdk macosx -configuration Release test -test-sdk macosx
可以在单元测试上修改,熟悉使用
QNDnsManager *dns;
if([QNDnsManager needHttpDns]){
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[[QNResolver alloc] initWithAddress:@"119.29.29.29"]];
[array addObject:[QNResolver systemResolver]];
dns = [[QNDnsManager alloc] init:array networkInfo:[QNNetworkInfo normal]];
}else{
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[QNResolver systemResolver]];
[array addObject:[[QNResolver alloc] initWithAddress:@"114.114.114.114"]];
dns = [[QNDnsManager alloc] init:array networkInfo:[QNNetworkInfo normal]];
}
详情参考代码提交指南。
The MIT License (MIT).详情见 License文件.