Setting basic business information
Type definition
Note:
[Disused from v1.7.0] The SDK log reporting capability is configured in the console.
typedef enum {
HttpDnsEncryptTypeDES = 0,
HttpDnsEncryptTypeAES = 1,
HttpDnsEncryptTypeHTTPS = 2
} HttpDnsEncryptType;
typedef enum {
HttpDnsAddressTypeAuto = 0,
HttpDnsAddressTypeIPv4 = 1,
HttpDnsAddressTypeIPv6 = 2,
HttpDnsAddressTypeDual = 3,
} HttpDnsAddressType;
typedef struct DnsConfigStruct {
NSString* appId;
int dnsId;
NSString* dnsKey;
NSString* token;
NSString* dnsIp;
BOOL debug;
int timeout;
HttpDnsEncryptType encryptType;
HttpDnsAddressType addressType;
NSString* routeIp;
BOOL httpOnly;
NSUInteger retryTimesBeforeSwitchServer;
NSUInteger minutesBeforeSwitchToMain;
BOOL enableReport;
} DnsConfig;
API declaration
- (BOOL) initConfig:(DnsConfig *)config;
- (BOOL) initConfigWithDictionary:(NSDictionary *)config;
- (void) WGSetPreResolvedDomains:(NSArray *)domains;
- (void) WGSetKeepAliveDomains:(NSArray *)domains;
- (void) WGSetIPRankData:(NSDictionary *)IPRankData;
- (void) WGSetExpiredIPEnabled:(BOOL)enable;
- (void) WGSetPersistCacheIPEnabled:(BOOL)enable;
Note:
The HTTPDNS SDK provides multiple DNS optimization options, which can be combined for optimal DNS performance.
You can configure (void) WGSetExpiredIPEnabled:(true)enable;
and (void) WGSetPersistCacheIPEnabled:(true)enable;
to achieve optimistic DNS cache.
The persistent cache feature is used to improve cache hit rate and shorten the time to the first meaningful paint. This feature stores the last DNS result locally and preferentially reads the local cache when an app is launched.
If the cached DNS result has expired (TTL expired), since optimistic DNS cache allows the return of IP addresses in expired DNS results, the SDK returns the IP addresses in the expired DNS result (assuming that the IP addresses are available in most cases) and initiates an async request to update the cache.
When optimistic DNS cache is enabled, cache cannot be hit (there is no cache) for the first DNS request of a domain, so 0;0
is returned, and an async DNS request is initiated to update the cache. Therefore, after enabling optimistic DNS cache, you need to ensure that local DNS will work if no cache is hit. For important domains, we recommend you enable DNS prefetch via (void) WGSetPreResolvedDomains:(NSArray *)domains;
.
If the server IP addresses are changed frequently, be sure to enable automatic cache update via (void) WGSetKeepAliveDomains:(NSArray *)domains;
and DNS prefetch via (void) WGSetPreResolvedDomains:(NSArray *)domains;
to ensure the accuracy of DNS results.
Sample code
Sample API call:
In an Objective-C project:
DnsConfig *config = new DnsConfig();
config->dnsIp = @"HTTPDNS server IP";
config->dnsId = DNS authorization ID;
config->dnsKey = @"Encryption key";
config->encryptType = HttpDnsEncryptTypeDES;
config->debug = YES;
config->timeout = 2000;
[[MSDKDns sharedInstance] initConfig: config];
In a Swift project:
let msdkDns = MSDKDns.sharedInstance() as? MSDKDns;
msdkDns?.initConfig(with: [
"dnsIp": "HTTPDNS server IP",
"dnsId": "DNS authorization ID",
"dnsKey": "Encryption key",
"encryptType": 0,
]);
DNS APIs
The following APIs are used to get IPs, and you only need to import the header file to call the corresponding API. For batch query, a maximum of 8 domain names are supported at each time.
Sync APIs
Single query API: WGGetHostByName:
Batch query API (with one IP returned): WGGetHostsByNames:
Batch query API (with all IPs returned): WGGetAllHostsByNames:
Note:
For a sync API, execution will block. Therefore, we recommended you call a sync API in a child thread or use an async API.
Async APIs
Single query API: WGGetHostByNameAsync:returnIps:
Batch query API (with one IP returned): WGGetHostsByNamesAsync:returnIps:
Batch query API (with all IPs returned): WGGetAllHostsByNamesAsync:returnIps:
The returned address is in the following format:
Single query: The single query API will return NSArray
with a fixed length of 2, where the first value is the IPv4 address, and the second value is the IPv6 address. The response format is as described below:
Under IPv4, only the IPv4 address will be returned; that is, the response format will be [ipv4, 0]
.
Under IPv6, only the IPv6 address will be returned; that is, the response format will be [0, ipv6]
.
Under a dual stack network, the resolved IPv4 and IPv6 addresses (if they exist) will be returned; that is, the response format will be [ipv4, ipv6]
.
If DNS query fails, [0, 0]
will be returned; in this case, you only need to call the WGGetHostByName
API again.
Batch query (with one IP returned) : The batch query API will return an NSDictionary
, where key
is the queried domain, and value
is NSArray
with a fixed length of 2. The first value is the IPv4 address, and the second value is the IPv6 address. The response format is as described below:
Under IPv4, only the IPv4 address will be returned; that is, the response format will be {"queryDomain" : [ipv4, 0]}
.
Under IPv6, only the IPv6 address will be returned; that is, the response format will be {"queryDomain" : [0, ipv6]}
.
Under a dual stack network, the resolved IPv4 and IPv6 addresses (if they exist) will be returned; that is, the response format will be {"queryDomain" : [ipv4, ipv6]}
.
If DNS query fails, {"queryDomain" : [0, 0]}
will be returned; in this case, you only need to call the WGGetHostByNames
API again.
Batch query (with all IPs returned) : The batch query API will return an NSDictionary
, where key
is the queried domain, and value
is an NSDictionary
containing two keys (ipv4, ipv6) with corresponding NSArray
objects as values indicating all queried IPv4/IPv6 result IPs. The response format is as follows:
{"queryDomain" : { "ipv4": [], "ipv6": [ ]}}
How to improve IPv6 usage:
When you make a URL request by using an IPv6 address, you need to enclose the IPv6 address in square brackets, such as http://[64:ff9b::b6fe:7475]/
.
If the IPv6 address is 0, you can directly use the IPv4 address for connection.
If the IPv4 address is 0, you can directly use the IPv6 address for connection.
If neither the IPv4 address nor the IPv6 address is 0, the client can determine which address to use first for connection; if the client fails to connect to the preferred address, it should switch to the other one.
When you integrate HTTPDNS through the SDK, if HTTPDNS does not find a DNS query result, the domain will be resolved by local DNS, and the result provided by local DNS will be returned.
Sync DNS APIs
API name
WGGetHostByName and WGGetHostsByNames
API declaration
- (NSArray *) WGGetHostByName:(NSString *) domain;
- (NSDictionary *) WGGetHostsByNames:(NSArray *) domains;
Sample code
Sample API call:
NSArray *ipsArray = [[MSDKDns sharedInstance] WGGetHostByName: @"qq.com"];
if (ipsArray && ipsArray.count > 1) {
NSString *ipv4 = ipsArray[0];
NSString *ipv6 = ipsArray[1];
if (![ipv6 isEqualToString:@"0"]) {
} else if (![ipv4 isEqualToString:@"0"]){
} else {
}
}
NSDictionary *ipsDict = [[MSDKDns sharedInstance] WGGetHostsByNames: @[@"qq.com", @"dnspod.cn"]];
NSArray *ips = [ipsDict objectForKey: @"qq.com"];
if (ips && ips.count > 1) {
NSString *ipv4 = ips[0];
NSString *ipv6 = ips[1];
if (![ipv6 isEqualToString:@"0"]) {
} else if (![ipv4 isEqualToString:@"0"]){
} else {
}
}
Async DNS APIs
API name
WGGetHostByNameAsync and WGGetHostsByNamesAsync
API declaration
- (void) WGGetHostByNameAsync:(NSString *) domain returnIps:(void (^)(NSArray *ipsArray))handler;
- (void) WGGetHostsByNamesAsync:(NSArray *) domains returnIps:(void (^)(NSDictionary * ipsDictionary))handler;
Sample code
Note:
You can select any call method based on your business needs.
Wait for the entire DNS query process to complete, get the result, and make a connection.
Strengths: It is guaranteed that each request can get the returned result for subsequent connection operations.
Shortcomings: Processing through the async API is slightly more complex than that through the sync API.
[[MSDKDns sharedInstance] WGGetHostByNameAsync:@"qq.com" returnIps:^(NSArray *ipsArray) {
if (ipsArray && ipsArray.count > 1) {
NSString *ipv4 = ipsArray[0];
NSString *ipv6 = ipsArray[1];
if (![ipv6 isEqualToString:@"0"]) {
} else if (![ipv4 isEqualToString:@"0"]){
} else {
}
}
}];
[[MSDKDns sharedInstance] WGGetHostsByNamesAsync:@[@"qq.com", @"dnspod.cn"] returnIps:^(NSDictionary *ipsDict) {
NSArray *ips = [ipsDict objectForKey: @"qq.com"];
if (ips && ips.count > 1) {
NSString *ipv4 = ips[0];
NSString *ipv6 = ips[1];
if (![ipv6 isEqualToString:@"0"]) {
} else if (![ipv4 isEqualToString:@"0"]){
} else {
}
}
}];
You can directly get the cached result with no need to wait. If there is no cache, result
will be nil
Strengths: Businesses that have demanding requirements for the DNS time don't need to wait and can directly get the cached result to perform subsequent connection operations, unlike the sync API where the DNS query may take more than 100 milliseconds.
Shortcomings: The result of the first request will be nil
, and you need to add the processing logic.
__block NSArray* result;
[[MSDKDns sharedInstance] WGGetHostByNameAsync:domain returnIps:^(NSArray *ipsArray) {
result = ipsArray;
}];
// You can directly get the cached result with no need to wait. If there is no cache, `result` will be `nil`
if (result) {
// Get the cached result and make a connection
} else {
// There is no cache for this request, and you can follow the original logic
}
Was this page helpful?