iOS 实现动态切换网络请求Host 域名

467 阅读1分钟

背景

今天在群里发现有个同学在问一个问题,如图: 在这里插入图片描述 这位同学是想知道如何快速又方便的可以随时切换Release 与 Test服务器域名,方便APP的测试。正好,我们也有相同的场景。那么我们是怎么做的呢?

实现

流程图

1、创建一个网络请求Host管理的工具类,我们暂且叫它CNChangeHostManager。并且定义Host属性与初始化方法。

@interface CNChangeHostManager : NSObject
@property (nonatomic, copy) NSString *kCNPassPortHost;
+(instancetype)sharedChangeHostObject;
@end

2、定义枚举配置,来区分Host环境

//PassPort
typedef NS_ENUM(NSUInteger, CNPassPortHostType) {
    CNPassPortHostTypeRelease,//正式
    CNPassPortHostTypeTest,//测试
    CNPassPortHostTypeBeta,//预发布
};

3、实现本地缓存配置与读取配置的方法

//保存配置到沙盒
+ (void)savePassPortHostType:(CNPassPortHostType)passPortHostType
{
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setInteger:passPortHostType forKey:@"passPortHostType"];
    [userdefault synchronize];
}

//从沙盒中读取配置
+ (CNPassPortHostType)passPortHostType
{
    return [[NSUserDefaults standardUserDefaults] integerForKey:@"passPortHostType"];
}

4、设置单例初始化方法,并设置Host默认值

+ (instancetype)sharedChangeHostObject{
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        changeURLObject = [[super allocWithZone:NULL] init];
        
        [changeURLObject defaultHost];
        
    });
    
    return changeURLObject;
    
}

-(void)defaultHost
{
    [self passPortHostWithPassPortHostType:[CNChangeHostManager passPortHostType]];
}

5、注册通知监听,用于接收改变Host的通知

//注册通知监听,用于接收改变Host
- (instancetype)init{
    if (self = [super init]) {
        //注册通知监听,用于接收改变Host
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changePassPortHost:) name:ChangePassPortHostNotification object:nil];
    }
    return self;
}

6、实现监听通知方法,读取本地缓存的配置。

- (void)changePassPortHost:(NSNotification *)notification
{
    [self passPortHostWithPassPortHostType:[CNChangeHostManager passPortHostType]];
}

-(void)passPortHostWithPassPortHostType:(CNPassPortHostType)passPortHostType
{
    switch (passPortHostType) {
        case CNPassPortHostTypeRelease:
            self.kCNPassPortHost = @"https://passport.com";
            break;
            
        case CNPassPortHostTypeTest:
            self.kCNPassPortHost = @"https://test-passport.com";
            break;
            
        case CNPassPortHostTypeBeta:
            self.kCNPassPortHost = @"https://pre-passport.com";
            break;
            
        default:
            self.kCNPassPortHost = @"https://passport.com";
            break;
    }
}

7、设置中修改Host 方法

switch (index) {
    case 0:
        [CNChangeHostManager savePassPortHostType:(CNPassPortHostTypeRelease)];
        break;
    case 1:
        [CNChangeHostManager savePassPortHostType:(CNPassPortHostTypeTest)];
        break;
    case 2:
        [CNChangeHostManager savePassPortHostType:(CNPassPortHostTypeBeta)];
        break;
        
    default:
        break;
}
[[NSNotificationCenter defaultCenter] postNotificationName:ChangePassPortHostNotification object:nil];

8、Host 读取方式

[NSString stringWithFormat:@"/login",[CNChangeHostManager sharedChangeHostObject].kCNPassPortHost]

这样就可以随时切换Host环境配置了。