Integrate HTTPDNS with the Alibaba Cloud Object Storage Service (OSS) SDK for iOS to prevent DNS hijacking and accelerate file transfers.
1. Background
Alibaba Cloud Object Storage Service (OSS) is a general-purpose cloud storage service that allows developers to store and access files such as profile pictures, images, audio, and video from various applications. To use OSS on iOS, you must integrate the OSS iOS SDK.
Combining the HTTPDNS SDK with the OSS iOS SDK enables HTTPDNS-based domain name resolution for OSS requests, which improves file transfer stability and speed, prevents DNS hijacking, and optimizes the network experience.
2. Integration solution
On iOS, integrate HTTPDNS by using the EMAS local proxy service, which transparently forwards OSS requests and optimizes DNS resolution.
2.1 Install the HTTPDNS proxy
Add the EMASLocalProxy dependency to the Podfile:
source 'https://github.com/aliyun/aliyun-specs.git'
target 'yourAppTarget' do
use_framework!
pod 'AlicloudHTTPDNS', 'x.x.x'
pod 'AliyunOSSiOS', 'x.x.x'
pod 'EMASLocalProxy', 'x.x.x'
end
Version requirements:
-
AlicloudHTTPDNS: >= 3.0.0
-
AliyunOSSiOS: >= 2.11.2
-
EMASLocalProxy: >= 1.4.0
For more information about integrating HTTPDNS using a local proxy, see Local proxy solution.
2.1 HTTPDNS SDK initialization
// AppDelegate.m
#import <AlicloudHttpDNS/AlicloudHttpDNS.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize HTTPDNS.
HttpDnsService *httpdns = [[HttpDnsService alloc] initWithAccountID:@"your-account-id"
secretKey:@"your-secret-key"];
[httpdns setLogEnabled:YES];
[httpdns setHTTPSRequestEnabled:YES];
[httpdns setPersistentCacheIPEnabled:YES];
[httpdns setNetworkingTimeoutInterval:5];
NSLog(@"HTTPDNS SDK initialization complete!");
return YES;
}
2.2 Configure the OSS client
The OSS iOS SDK is available in Objective-C and Swift (V2) versions. The HTTPDNS integration principle is the same for both versions. The following example uses the Objective-C version.
#import <AliyunOSSiOS/AliyunOSSiOS.h>
#import <AlicloudHttpDNS/AlicloudHttpDNS.h>
#import "EMASLocalHttpProxy.h"
// 1. Configure the DNS resolver.
[EMASLocalHttpProxy setDNSResolverBlock:^NSArray<NSString *> * _Nullable(NSString * _Nonnull hostname) {
HttpDnsService *httpdns = [HttpDnsService sharedInstance];
HttpdnsResult *result = [httpdns resolveHostSync:hostname byIpType:HttpdnsQueryIPTypeBoth];
if (result && (result.hasIpv4Address || result.hasIpv6Address)) {
NSMutableArray<NSString *> *allIPs = [NSMutableArray array];
if (result.hasIpv4Address) [allIPs addObjectsFromArray:result.ips];
if (result.hasIpv6Address) [allIPs addObjectsFromArray:result.ipv6s];
return allIPs;
}
return nil; // If HTTPDNS resolution fails, return nil to fall back to the system DNS.
}];
// 2. Start the proxy service and get the port.
UInt16 proxyPort = 0;
if ([EMASLocalHttpProxy isProxyReady]) {
proxyPort = [EMASLocalHttpProxy proxyPort];
}
// 3. Create the OSS client configuration.
id<OSSCredentialProvider> credentialProvider = [[OSSPlainTextAKSKPairCredentialProvider alloc]
initWithPlainTextAccessKey:@"your-access-key-id" // Replace with your actual AccessKey ID.
secretKey:@"your-access-key-secret"]; // Replace with your actual AccessKey secret.
OSSClientConfiguration *config = [OSSClientConfiguration new];
NSString *endpoint = @"https://oss-cn-hangzhou.aliyuncs.com";
// 4. Configure the HTTPDNS proxy.
if (proxyPort > 0) {
config.proxyHost = @"127.0.0.1";
config.proxyPort = @(proxyPort);
}
// 5. Create the OSS client.
OSSClient *client = [[OSSClient alloc] initWithEndpoint:endpoint
credentialProvider:credentialProvider
clientConfiguration:config];
-
You must initialize the HTTPDNS SDK and the proxy before you initialize the OSS SDK.
3. Summary
Combining HTTPDNS with the OSS iOS SDK prevents DNS hijacking and accelerates file transfers, improving access stability and security. The core of this integration is a local proxy service that handles HTTPDNS resolution for OSS requests.
Key points:
-
Initialization order: The HTTPDNS SDK and the local proxy must be initialized before the OSS client.
-
Fallback handling: The setDNSResolverBlock is the core of the HTTPDNS integration. If resolution fails or returns no result, return nil to fall back to system DNS.