This topic describes the advanced configuration operations of HTTPDNS SDK for iOS.
Specify whether to allow HTTPDNS to display logs
You can specify whether to enable the local logging feature for HTTPDNS. Enabling this feature helps you troubleshoot resolution issues. We recommend disabling the local logging feature when you publish an app that integrates with HTTPDNS.
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 the local logging feature, you can call this operation to display logs to a custom location. If you want to use log files to collect resolution logs, you can call this operation to write the logs to the relevant files.
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
Refer to the following code to implement a log receiving class.
#import "HttpdnsLoggerProtocol.h"
@interface MyLoggerHandler : NSObject <HttpdnsLoggerProtocol>
@end
@implementation MyLoggerHandler
- (void)log:(NSString *)logStr {
NSLog(@"[myLog] - %@", logStr);
}
@endConfigure IP address ranking
Specify the port number for the service corresponding to a domain name. After you configure this operation, if the corresponding domain name is resolved, the SDK will perform TCP connection speed tests on the IP address list returned by the resolution. Based on the test results, the SDK will dynamically sort the IP address list to ensure that the first IP address has the best availability.
This feature is supported in SDK V3.2.0 and later. If the IP address list contains both IPv4 and IPv6 addresses, they will be tested and sorted separately.
Network speed testing requires a certain amount of time. When you call the operation for the first time to trigger HTTPDNS to resolve a domain name, the IP address list obtained immediately may not be sorted.
When there are significant performance or network environment differences among multiple service IP entry points, the speed test results of all clients may be similar. This can cause all clients to send business requests to the same service IP address, resulting in load imbalance and affecting stability. Therefore, you need to carefully evaluate whether to enable 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 | Set the port number for the corresponding domain name. @{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
After performing this operation, if there is a time drift, the device time will be calibrated for each network request. This prevents authentication failures due to expired device time in authentication mode. If this operation is not performed, the device time will be used as the standard.
SDK V3.0.0 and later are supported.
Scenario: The time on a mobile phone is inaccurate.
The calibration takes effect within one lifecycle of an app. After the app is restarted, you must call this operation again.
A time service is required. The time service can be a self-managed time service that supports simple timestamp operations. After you start your app, obtain the accurate time using the time service and specify the time in the calibration operation. This way, the SDK can obtain the difference between the accurate time and the time on the device.
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
If you want to use HTTPDNS on your app in a region outside the Chinese mainland, you can configure a startup service node of the SDK to improve the resolution efficiency. After you configure the node, the SDK uses the node for domain name resolution and the subsequent scheduling node list updates.
By default, no configuration is required. Even if your app starts in a region outside the Chinese mainland, the SDK will automatically schedule to the nearest overseas node, and the impact on efficiency is not significant.
SDK V3.1.0 and later are supported.
Operation definition
- (void)setRegion:(NSString *)region;
Category
HttpDnsService
Parameter description
Parameter | Type | Indicates whether the parameter is required | Description |
region | NSString * | Yes | The region that you want to specify. Specify a region in the Chinese mainland or one of the following regions outside the Chinese mainland: the China (Hong Kong) region, the Singapore region, the Germany (Frankfurt) region, and the US (Virginia) region. These regions correspond to the following macros:
|
Specify the time to live (TTL) of a resolution result
If you want to specify the TTL of a domain name in a local app to overwrite the actual domain name configuration, you can use this operation.
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 that you want to specify a custom TTL. |
ipType | AlicloudHttpDNS_IPType | The type of the query in which you need to specify 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