Configure advanced features of the HTTPDNS SDK for iOS, including logging, IP address ranking, time calibration, region selection, and custom TTL.
Specify whether to allow HTTPDNS to display logs
Enable or disable local debug logging for HTTPDNS to help troubleshoot resolution issues. We recommend that you disable logging before you release your app.
Operation definition
- (void)setLogEnabled:(BOOL)enable;
Category
HttpDnsService
Parameter description
|
Parameter |
Type |
Indicates whether the parameter is required |
Description |
|
enable |
BOOL |
Yes |
Log switch |
Configure a callback class used to receive SDK logs
After you enable logging, call this operation to redirect logs to a custom destination. For example, you can write resolution logs to files for collection and analysis.
Operation definition
- (void)setLogHandler:(id<HttpdnsLoggerProtocol>)logHandler;
Category
HttpDnsService
Parameter description
|
Parameter |
Type |
Indicates whether the parameter is required |
Description |
|
logHandler |
id<HttpdnsLoggerProtocol> |
Yes |
The callback function for log output. |
Sample code
Implement a log receiving class as shown below.
#import "HttpdnsLoggerProtocol.h"
@interface MyLoggerHandler : NSObject <HttpdnsLoggerProtocol>
@end
@implementation MyLoggerHandler
- (void)log:(NSString *)logStr {
NSLog(@"[myLog] - %@", logStr);
}
@end
Configure IP address ranking
Specify the service port number for a domain name. When the domain name is resolved, the SDK performs TCP connection speed tests on the returned IP addresses and dynamically sorts them so that the first IP address has the best availability.
-
Supported in SDK V3.2.0 and later. If the IP address list contains both IPv4 and IPv6 addresses, they are tested and sorted separately.
-
Speed testing takes time. On the first call that triggers HTTPDNS resolution, the returned IP address list may not yet be sorted.
-
If multiple service IP entry points have similar performance or network conditions, speed test results across clients may converge on the same IP address. This can cause load imbalance and affect stability. Evaluate carefully before enabling IP address ranking.
Operation definition
- (void)setIPRankingDatasource:(NSDictionary<NSString *, NSNumber *> *)IPRankingDatasource;
Parameter description
|
Parameter |
Type |
Indicates whether the parameter is required |
Description |
|
IPRankingDatasource |
NSDictionary<NSString *, NSNumber *> |
Yes |
The port number for the domain name. Format: @{host: port} |
Sample code
HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:xxxx secretKey:@"xxxxx"];
NSDictionary *IPRankingDatasource = @{
@"www.aliyun.com" : @443,
@"www.taobao.com" : @443,
@"www.exmaple.com" : @443
};
[httpdns setIPRankingDatasource:IPRankingDatasource];
Calibrate the signature time of an app
Calibrate the device time for each network request to prevent authentication failures caused by time drift. If this operation is not called, the device time is used as-is.
-
Supported in SDK V3.0.0 and later.
-
Applicable when the device clock is inaccurate.
-
Calibration takes effect within a single app lifecycle. After the app restarts, you must call this operation again.
-
A time service is required. The time service can be a self-managed service that supports simple timestamp operations. After your app starts, obtain the accurate time from the time service and pass it to this operation so the SDK can calculate the difference between the accurate time and the device time.
-
In version 2.x.x and earlier, this operation was defined as
- (void)setAuthCurrentTime:(NSTimeInterval)currentTime;
Operation definition
- (void)setInternalAuthTimeBaseBySpecifyingCurrentTime:(NSTimeInterval)currentTime;
Category
HttpDnsService
Parameter description
|
Parameter |
Type |
Indicates whether the parameter is required |
Description |
|
currentTime |
NSTimeInterval |
Yes |
The accurate timestamp used for calibration. |
Specify a region
To improve resolution efficiency for apps running outside the Chinese mainland, configure a startup service node for the SDK. The SDK uses this node for domain name resolution and subsequent scheduling node list updates.
By default, no configuration is needed. Even if your app starts outside the Chinese mainland, the SDK automatically schedules to the nearest overseas node with minimal impact on efficiency.
Supported in SDK V3.1.0 and later.
Operation definition
- (void)setRegion:(NSString *)region;
Category
HttpDnsService
Parameter description
|
Parameter |
Type |
Indicates whether the parameter is required |
Description |
|
region |
NSString * |
Yes |
The region to use. You can specify a region in the Chinese mainland or one of the following regions outside the Chinese mainland: China (Hong Kong), Singapore, Germany (Frankfurt), or US (Virginia). These regions correspond to the following macros:
|
Specify the time to live (TTL) of a resolution result
Override the server-returned TTL of a domain name with a custom value in your local app.
Operation definition
- (int64_t)httpdnsHost:(NSString *)host ipType:(AlicloudHttpDNS_IPType)ipType ttl:(int64_t)ttl;
Category
HttpdnsTTLDelegate
Callback parameter description
|
Parameter |
Type |
Description |
|
host |
NSString * |
The domain name for which to set a custom TTL. |
|
ipType |
AlicloudHttpDNS_IPType |
The query type for which to set a custom TTL. |
|
ttl |
int64_t |
The original TTL returned from the server. |
Sample code
@interface AppDelegate () <HttpdnsTTLDelegate>
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Specify the required account ID.
HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:xxxx secretKey:@"xxxx"];
httpdns.ttlDelegate = self;
return YES;
}
- (int64_t)httpdnsHost:(NSString *)host ipType:(AlicloudHttpDNS_IPType)ipType ttl:(int64_t)ttl {
if ([host isEqualToString:@"www.aliyun.com"]) {
// The resolution record of the www.aliyun.com domain is stable and can be cached locally for a long period of time.
// If the host is www.aliyun.com, set the TTL to 600 seconds.
return 600;
}
// If you do not have special configurations, the original TTL is returned.
return ttl;
}
@end