Dynamic Domain Name Implementation in Mobile Apps
The article explains how to implement dynamic domain name switching in mobile apps across iOS, Android, Flutter, and H5 by using a configuration center and native network interception techniques such as NSURLProtocol, OkHttp instrumentation, texture bridging, and frontend configuration to resolve regional DNS issues and enable extended features like request statistics and custom responses.
This article describes how to implement dynamic domain name switching in mobile applications to handle DNS resolution issues in certain regions. The solution combines a configuration center platform with native network interception techniques.
iOS Implementation: Uses NSURLProtocol, an abstract class provided by Apple for handling protocol-specific URL data. By creating a custom subclass and registering it with registerClass: , developers can intercept all HTTP/HTTPS requests. Key methods include canInitWithRequest: to determine which requests to intercept, canonicalRequestForRequest: for request customization, and startLoading / stopLoading for managing the request lifecycle. For third-party libraries like AFNetworking and Alamofire, Method Swizzle is used to replace protocolClasses in NSURLSessionConfiguration.
Android Implementation: Uses OkHttp and HttpUrlConnection/HttpsUrlConnection interception through bytecode instrumentation. The approach involves intercepting OkHttpClient configuration and URLConnection parameters to dynamically replace domain names.
Flutter Implementation: Uses external textures (TextureLayer) to bridge Flutter with native image loading. By leveraging PixelBuffer as the data carrier between Native and Flutter, image loading is delegated to native libraries like SDWebImage, enabling dynamic domain name support for images while regular network requests use Flutter plugins that call native libraries.
H5 Implementation: Uses frontend configuration to dynamically switch domains when domain access anomalies are detected, combined with the configuration center for unified network request management.
The solution can also be extended to: statistics network interface failure rates, redirect network requests, add common headers to requests, and return custom responses.
Youzan Coder
Official Youzan tech channel, delivering technical insights and occasional daily updates from the Youzan tech team.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.