tencent cloud

Integration
Last updated:2025-10-13 15:31:03
Integration
Last updated: 2025-10-13 15:31:03

Overview

HTTPDNS helps avoid the failure to access the optimal access point during the traditional local DNS of ISPs. It works by replacing the traditional DNS protocol with the HTTP encryption protocol, without using domains throughout the process, which greatly reduces the possibility of DNS hijacking.

Preparations

1. You have activated HTTPDNS as instructed in Activating HTTPDNS.
2. You have added domains to be resolved in the HTTPDNS console as instructed in Adding a Domain.

Installation package structure

Download the latest version of the SDK package here.
Get the open-source repository of the SDK here.
Name
Applicable Scope
MSDKDns.xcframework
Applicable to projects with "Build Setting->C Language Dialect" configured as "GNU++98" and "Build Setting->C++ Standard Library" as "libstdc (GNU C standard library)" .
MSDKDns_intl.xcframework
MSDKDns.xcframework version for Tencent Cloud International.
MSDKDns_C11.xcframework
Applicable to projects with "Build Setting->C Language Dialect" and "Build Setting->C Standard Library" configured as "GNU++11" and "libc++(LLVM C standard library with C11 support)", respectively.
MSDKDns_C11_intl.xcframework
MSDKDns_C11.xcframework version for Tencent Cloud International.
Note:
If you want to use the version for Tencent Cloud International, obtain the initialization configuration from the HTTPDNS international console.

SDK integration

Note:
For more information on quick integration, see SDK Quick Access.
HTTPDNS provides two integration methods for iOS:
Integration using CocoaPods
Manual integration

Integration using CocoaPods

1. Install CocoaPods
Enter the following command in a terminal window (you need to install Ruby on your Mac first):
sudo gem install cocoapods
2. Create a Podfile file
Go to the project directory, and enter the following command. A Podfile file will then be created in the project directory.
pod init
3. Edit the Podfile file
Add the following code inside the Podfile of your project:
# Applies to projects with "Build Setting->C++ Language Dialect" configured as **"GNU++98"** and "Build Setting->C++ Standard Library" as **"libstdc++(GNU C++ standard library)"**.
pod 'MSDKDns_intl'
# Applies to projects with these two configurations set as **"GNU++11"** and **"libc++(LLVM C++ standard library with C++11 support)"**.
# pod 'MSDKDns_C11_intl'
4. Update and install the SDK
Enter the following command in the terminal window to update local repository files:
pod install
Or run this command to update the local repository:
pod update
After the pod command is executed, an .xcworkspace project file with integrated SDK is generated. Double-click to open it.
Note:
For more information on CocoaPods, see CocoaPods.

Manual integration

For the directions of manual integration, see the following demos:
Download the Objective-C demo here.
Download the Swift demo here.
Perform the following steps:
1. Import dependency libraries, drag the framework from the HTTPDNSLibs directory into the corresponding Target, and select Copy items if needed in the pop-up box:
MSDKDns_C11_intl.framework (or MSDKDns_framework, depending on the project configuration)

2. Import system libraries:
libz.tbd
libsqlite3.tbd
libc++ .tbd
Foundation.framework
CoreTelephony.framework
SystemConfiguration.framework
CoreGraphics.framework
Security.framework
3. ObjC configuration:
For iOS SDK integration, configure -ObjC in MSDKDnsDemo -> Build Settings -> Linking -> Other Linker Flags. Add the -ObjC attribute, as shown below:


SDK initialization

1. Introduce the SDK header file
Objective-C
Swift
#import <MSDKDns_C11_intl/MSDKDns.h>
// Support for importing in the bridging header file
#import "MSDKDns_C11_intl/MSDKDns.h"
2. API call example:
Objective-C
Swift
// Use in .m files in the following ways:
DnsConfig config = {
.dnsId = dns authorization id, // Obtain in the mobile domain name resolution Tencent Cloud console. Refer to the "preliminary preparation" screenshot above. The "authorization id" is located at the top-left corner.
.dnsKey = @"secretkey",
.encryptType = HttpDnsEncryptTypeDES,
.debug = YES,
.timeout = 2000,
};
[[MSDKDns sharedInstance] initConfig: &config];
// Use in .mm files in the following ways:
DnsConfig *config = new DnsConfig();
config->dnsId = dns authorization id; // Obtain in the console of Tencent Cloud Mobile Domain Name Resolution, refer to the above text "preliminary preparation" screenshot, "authorization id" is at the top-left corner.
config->dnsKey = @"secretkey";
config->encryptType = HttpDnsEncryptTypeDES;
config->debug = YES;
config->timeout = 2000;
[[MSDKDns sharedInstance] initConfig: config];
let msdkDns = MSDKDns.sharedInstance() as? MSDKDns;
msdkDns?.initConfig(with: [
"dnsId": "dns authorization id", // Obtain in the console of Tencent Cloud Mobile Domain Name Resolution, see the screenshot in the "preliminary preparation" section, the "authorization id" is at the top-left corner
"dnsKey": "secretkey",
"encryptType": 0, // 0 -> des,1 -> aes
]);
Note:
iOS 9 introduced a new feature App Transport Security (ATS). This feature requires apps to use HTTPS protocol for network access.
SDK initialization encryption method settings DES and AES (parameter: encryptType) use an HTTP-based signing mechanism to ensure access security. It does not use HTTPS, so special configuration is required. Details are given below:
Add keywords in the Info.plist file: Add a Dictionary of type NSAppTransportSecurity, then add a Boolean of type NSAllowsArbitraryLoads under NSAppTransportSecurity and set its value to YES.

Integration verification

Note:
Using SDK-based HTTPDNS integration, if HTTPDNS fails to query the parsing result, it will return the LocalDNS parsing result. It is necessary to use log verification to verify successful HTTPDNS access.

Log verification

Enable the SDK DEBUG logging (set debug in DnsConfig to YES), find the printed api name:HDNSGetHostByName, data: { ... } log, and check the log info of the local DNS ( ldns_ip in the log) and HTTPDNS ( hdns_ip in the log) to determine whether the connection is successful.
The hdns_ip key indicates the DNS query results of A records provided by HTTPDNS.
The hdns_4a_ips key indicates the DNS query results of AAAA records provided by HTTPDNS.
If hdns_ip or hdns_4a_ips is not empty, the connection is successful.
The ldns_ip key indicates the DNS query results provided by the local DNS.

Notes

If the service on the client is bound to the host, such as the HTTP service or the CDN service of the host, you need to specify the host field of the HTTP header after replacing the domain in the URL with the IP returned by HTTPDNS. Example:
NSURLConnection
NSURLSession
curl
Unity WWW API
AFNetworking
NSURL *httpDnsURL = [NSURL URLWithString:@"URL concatenated with parsing result ip"];
float timeOut = timeout period;
NSMutableURLRequest *mutableReq = [NSMutableURLRequest requestWithURL:httpDnsURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: timeOut];
[mutableReq setValue:@"original domain name" forHTTPHeaderField:@"host"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:mutableReq delegate:self];
[connection start];
NSURL *httpDnsURL = [NSURL URLWithString:@"URL concatenated with parsing result ip"];
float timeOut = timeout period;
NSMutableURLRequest *mutableReq = [NSMutableURLRequest requestWithURL:httpDnsURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: timeOut];
[mutableReq setValue:@"original domain name" forHTTPHeaderField:@"host"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue currentQueue]];
NSURLSessionTask *task = [session dataTaskWithRequest:mutableReq];
[task resume];
If you want to access www.qq.com, the IP resolved by HTTPDNS resolution is 192.168.0.111, so call in the following ways:
curl -H "host:www.qq.com" http://192.168.0.111/aaa.txt.
string httpDnsURL = "URL concatenated with parsing result ip";
Dictionary<string, string> headers = new Dictionary<string, string> ();
headers["host"] = "original domain name";
WWW conn = new WWW (url, null, headers);
yield return conn;
if (conn.error != null) {
print("error is happened:"+ conn.error);
} else {
print("request ok" + conn.text);
}
NSURL *httpDnsURL = [NSURL URLWithString:@"URL concatenated with parsing result ip"];
float timeOut = timeout period;
NSMutableURLRequest *mutableReq = [NSMutableURLRequest requestWithURL:httpDnsURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval: timeOut];
[mutableReq setValue:@"original domain name" forHTTPHeaderField:@"host"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
AFHTTPSessionManager* sessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:config];
sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionDataTask *task = [sessionManager dataTaskWithRequest:mutableReq
uploadProgress:nil
downloadProgress:nil
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (!error) {
NSString *responseString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"request complete ==== response: %@ ===== error: nil", responseString);
} else {
NSLog(@"request complete ==== response: nil ===== error: %@", error);
}
}];
[task resume];
Check whether an HTTP proxy is used locally. If an HTTP proxy is used, we recommend you not use HTTPDNS to resolve domains.
- (BOOL)isUseHTTPProxy {
CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings();
const CFStringRef proxyCFstr = (const CFStringRef)CFDictionaryGetValue(dicRef, (const void*)kCFNetworkProxiesHTTPProxy);
NSString *proxy = (__bridge NSString *)proxyCFstr;
if (proxy) {
return YES;
} else {
return NO;
}
}

SDK API and Practice

API

For SDK API interface, see the following document:

SDK Implementation Tutorial

For SDK implementation tutorial, see the following case:
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback