Use these operations to pre-resolve, synchronously resolve, or asynchronously resolve domain names through the HTTPDNS iOS SDK.
Perform pre-resolution
After the app starts and the SDK is initialized, call this operation to specify frequently accessed domain names. The SDK resolves these domain names in advance, reducing latency for subsequent resolution requests.
If called during runtime, the SDK resolves the domain names in the specified list and refreshes the cached results. This forces an update and is typically used after specific business actions to refresh domain name resolution and force rescheduling.
Operation definition
- (void)setPreResolveHosts:(NSArray *)hosts;
Class
HttpDnsService
Parameter description
|
Parameter |
Type |
Required or not |
Description |
|
hosts |
NSArray * |
Required |
The domain names that you want to pre-resolve. Important
|
Synchronous non-blocking domain name resolution operation
The synchronous non-blocking operation resolves a domain name without blocking the current thread, but may return an empty result.
This operation queries only the cache and returns the cached resolution result. If no result exists in the cache or the cached result has expired (TTL), domain name resolution runs in a worker thread. After successful resolution, the cache is updated for subsequent use.
Operation definition
- (nullable HttpdnsResult *)resolveHostSyncNonBlocking:(NSString *)host byIpType:(HttpdnsQueryIPType)queryIpType;
- (nullable HttpdnsResult *)resolveHostSyncNonBlocking:(NSString *)host byIpType:(HttpdnsQueryIPType)queryIpType withSdnsParams:(NSDictionary<NSString *, NSString *> *)sdnsParams sdnsCacheKey:(NSString *)cacheKey;
- (nullable HttpdnsResult *)resolveHostSyncNonBlocking:(HttpdnsRequest *)request;
-
This operation is added in HTTPDNS SDK V3.0.0.
Class
HttpDnsService
Parameter definition
|
Parameter |
Type |
Required or not |
Remarks |
|
host |
NSString * |
Required |
The domain name that you want to resolve |
|
queryIpType |
HttpdnsQueryIPType |
Required |
The type of the IP addresses to which you want to resolve the domain name. Use Valid values:
|
|
sdnsParams |
NSDictionary<NSString *, NSString *> * |
Not required |
The software-defined resolution parameters. |
|
cacheKey |
NSString * |
Not required |
The software-defined cache key. |
|
request |
HttpdnsRequest * |
Required |
For more information, see Resolution-related data structures. |
Return description
|
Type |
Description |
|
HttpdnsResult * |
The resolution result. For more information, see the section Resolution-related data structures. |
Synchronous domain name resolution operation
The synchronous resolution operation blocks the current thread until a valid resolution result is obtained and returned.
This operation first queries the cache and immediately returns the cached result if valid. If no valid result is found, it blocks the calling thread and performs resolution in a worker thread. The resolution result is returned upon completion, or a null value if the timeout is reached.
-
This operation is added in HTTPDNS SDK V3.0.0.
-
To prevent app stuttering caused by misuse of this operation in the main thread, this operation performs detection. If the operation detects that the calling thread is the main thread, it automatically downgrades to the implementation logic of the
resolveHostSyncNonBlockingoperation. -
The default timeout period for this operation is 2 seconds. If no valid result is obtained within the timeout period, nil is returned. You can use a request object to specify a custom timeout period.
Operation definition
- (nullable HttpdnsResult *)resolveHostSync:(NSString *)host byIpType:(HttpdnsQueryIPType)queryIpType;
- (nullable HttpdnsResult *)resolveHostSync:(NSString *)host byIpType:(HttpdnsQueryIPType)queryIpType withSdnsParams:(NSDictionary<NSString *, NSString *> *)sdnsParams sdnsCacheKey:(NSString *)cacheKey;
- (nullable HttpdnsResult *)resolveHostSync:(HttpdnsRequest *)request;
Class
HttpDnsService
Parameter definition
|
Parameter |
Type |
Required or not |
Remarks |
|
host |
NSString * |
Yes |
The domain name that you want to resolve |
|
queryIpType |
HttpdnsQueryIPType |
Yes |
The type of the IP addresses to which you want to resolve the domain name. Use Valid values:
|
|
sdnsParams |
NSDictionary<NSString *, NSString *> * |
No |
The software-defined resolution parameters. |
|
cacheKey |
NSString * |
No |
The software-defined cache key. |
|
request |
HttpdnsRequest * |
Yes |
For more information, see Resolution-related data structures. |
Return description
|
Type |
Description |
|
HttpdnsResult * |
The resolution result. For more information, see Resolution-related data structures. |
Asynchronous domain name resolution operation
The asynchronous resolution operation resolves a domain name without blocking the current thread and returns the result through a callback.
This operation first queries the cache and immediately returns the cached result through the callback if valid. If no valid result is found, it performs resolution in a worker thread and returns the result through the callback upon completion or timeout.
Operation definition
- (void)resolveHostAsync:(NSString *)host byIpType:(HttpdnsQueryIPType)queryIpType completionHandler:(void (^)(HttpdnsResult * nullable))handler;
- (void)resolveHostAsync:(NSString *)host byIpType:(HttpdnsQueryIPType)queryIpType withSdnsParams:(NSDictionary<NSString *, NSString *> *)sdnsParams sdnsCacheKey:(NSString *)cacheKey completionHandler:(void (^)(HttpdnsResult * nullable))handler;
- (void)resolveHostAsync:(HttpdnsRequest *)request completionHandler:(void (^)(HttpdnsResult * nullable))handler;
-
This operation is added in HTTPDNS SDK V3.0.0.
-
The default timeout period for this operation is 2 seconds. If no valid result is retrieved within the timeout period, nil is returned. You can use a request object to specify a custom timeout period.
Class
HttpDnsService
Parameter definition
|
Parameter |
Type |
Required or not |
Description |
|
host |
NSString * |
Required |
The domain name that you want to resolve |
|
queryIpType |
HttpdnsQueryIPType |
Required |
The type of the IP addresses to which you want to resolve the domain name. Use Valid values:
|
|
handler |
(void (^)(HttpdnsResult * nullable)) |
Required |
The callback. |
|
sdnsParams |
NSDictionary<NSString *, NSString *> * |
Not required |
The software-defined resolution parameters. |
|
cacheKey |
NSString * |
Not required |
The software-defined cache key. |
|
request |
HttpdnsRequest * |
Required |
For more information, see Resolution-related data structures. |
Callback result description
|
Type |
Description |
|
HttpdnsResult * |
The resolution result. For more information, see Resolution-related data structures. |
Operation for domain name cache clearance
Call this operation to clear cached DNS records of domain names, enabling resolution results to update within seconds. For example, if the domain name xxx.com resolves to ip1 and attacks occur on ip1, you can migrate traffic to ip2 as follows:
-
First, modify the IP address of the domain name to ip2 in the DNS console.
-
Then, go to the HTTPDNS console and manually submit a refresh action for this domain name to trigger a refresh on the server side.
-
Manually call this operation to clear the local cache of the xxx.com domain name.
-
If a request is initiated after the cache is cleared from the app, the server re-sends a request to the authoritative server to obtain ip2 in the latest DNS record. This way, ip2 is used as the actual IP address for resolution.
You can clear the cache of specific domain names or all domain names.
-
This operation is supported only in HTTPDNS SDK V2.0.4 and later. The newly added
cleanAllHostCacheoperation is supported in V3.1.0 and later. -
The operation clears both the memory cache and the local persistent sandbox cache.
Operation definition
- (void)cleanHostCache:(NSArray <NSString *>*)hostArray;
- (void)cleanAllHostCache;
Parameter description
|
Parameter |
Type |
Required or not |
Description |
|
hostArray |
NSArray <NSString *> * |
Required |
An array consisting of the domain name whose DNS record you want to clear from the local cache. If you want to clear all data, set this parameter to nil or an empty array |
Resolution-related data structures
The HTTPDNS iOS SDK resolution operations use two data structures: HttpdnsRequest and HttpdnsResult.
HttpdnsRequest
Wraps the request parameters for domain name resolution.
|
Parameter |
Type |
Required or not |
Description |
|
host |
NSString * |
Required |
The domain name that you want to resolve. |
|
queryIpType |
HttpdnsQueryIPType |
Required |
The type of the IP addresses to which you want to resolve the domain name. Use Valid values:
|
|
resolveTimeoutInSecond |
double |
Not required |
The timeout period for resolution. For synchronous operations, this parameter specifies the maximum wait time. For asynchronous operations, this parameter specifies the maximum wait time for callback. Default value: 2. Valid values: 0.5 to 5. Unit: seconds |
|
sdnsParams |
NSDictionary<NSString *, NSString *> * |
Not required |
The software-defined resolution parameters. |
|
cacheKey |
NSString * |
Not required |
The software-defined cache key. |
HttpdnsResult
Wraps the result returned after domain name resolution. An empty value is returned in the following scenarios:
-
The domain name cannot be resolved to an IP address. For example, no IP addresses are returned for recursive resolution.
-
The domain name is not added in the HTTPDNS console, and the server cannot resolve the domain name.
-
The resolution fails due to reasons such as a network exception.
-
When you use the synchronous non-blocking operation, the system first returns an empty value if no record is immediately found in the local cache.
|
Parameter |
Type |
Description |
|
host |
NSString * |
The domain name that is resolved |
|
ips |
NSArray<NSString *> * |
The IPv4 addresses that are returned. |
|
ipv6s |
NSArray<NSString *> * |
The IPv6 addresses that are returned. |
|
lastUpdatedTimeInterval |
NSTimeInterval |
The time when the IPv4 addresses were last updated. Unix timestamp. Unit: seconds |
|
ttl |
NSTimeInterval |
The TTL of the IPv4 addresses. Unit: seconds |