All Products
Search
Document Center

HTTPDNS:Best practices for using HTTPDNS with the Alibaba Cloud OSS SDK for iOS

Last Updated:Sep 12, 2025

This topic describes how to integrate HTTPDNS with the Alibaba Cloud Object Storage Service (OSS) SDK for iOS.

1. Background

Alibaba Cloud Object Storage Service (OSS) is a general-purpose cloud storage service. It allows developers to easily 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.

You can combine the HTTPDNS SDK with the OSS iOS SDK to resolve OSS domain names. This integration improves file transfer stability and speed. It also provides anti-hijacking protection for domain names and accelerates DNS resolution to optimize the network experience when an application accesses OSS.

2. Integration solution

On iOS, you can integrate HTTPDNS using the EMAS local proxy service. The local proxy provides transparent forwarding for 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. Although the APIs are not identical, the principle for integrating HTTPDNS is the same for both versions. This topic uses the Objective-C version as an example.

#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];
Important
  • 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. This combination improves access stability and security. The core of this implementation is to configure a local proxy service that includes HTTPDNS resolution logic for OSS.

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 HTTPDNS resolution fails or returns no result, return nil to fall back to the system DNS.