iOS网络切换工具

在开发阶段,在app中切换网络环境避免了每次需要打包安装的烦恼,最近抽时间整理了下,在此做个记录。

先看效果图:

iOS网络切换工具

不废话,开始进入正题,展示代码:

1.配置环境

    YJDomainModel *local1 = [YJDomainModel itemWithType:YJDomainTypeLocal domainName:@"本地环境1" baseUrl:@"https:www.localtest1.com"];
    YJDomainModel *local2 = [YJDomainModel itemWithType:YJDomainTypeLocal domainName:@"本地环境2" baseUrl:@"https:www.localtest2.com"];
    
    YJDomainModel *test1 = [YJDomainModel itemWithType:YJDomainTypeTest domainName:@"测试环境1" baseUrl:@"https:www.betatest1.com"];
    YJDomainModel *test2 = [YJDomainModel itemWithType:YJDomainTypeTest domainName:@"测试环境2" baseUrl:@"https:www.betatest2.com"];
     
    YJDomainModel *produce1 = [YJDomainModel itemWithType:YJDomainTypeProduce domainName:@"发布环境1" baseUrl:@"https:www.production1.com"];
    YJDomainModel *produce2 = [YJDomainModel itemWithType:YJDomainTypeProduce domainName:@"发布环境2" baseUrl:@"https:www.production2.com"];
    
    
    YJDomainManager *manager = [YJDomainManager manager];
    //显示自定义地址
    manager.showManualInput = YES;
    /*
     ?? 适用于同一个类型下需访问不同服务器的情况。 比如“发布”环境下需要访问不同的中英文服务器,可开启 allowRefresh 实时刷新baseUrl信息
     ?? 若无此需求,建议不要开启 allowRefresh
     */
    manager.allowRefresh = YES;
    //配置服务器地址
    __weak typeof(self) weakSelf = self;
    manager.configureDomainBlock = ^NSArray<YJDomainModel *> * _Nonnull{
        if (weakSelf.environmentButton.selected) {
            return @[local2,test2,produce2];
        }else {
            return @[local1,test1,produce1];
        }
    };

2.弹出选择框,切换网络

//弹出URL
    [[YJDomainManager manager] chooseDomianCompletion:^(NSString * _Nonnull baseUrl, NSString * _Nonnull domainName) {

        NSLog(@"baseUrl = %@\ndomainName = %@",baseUrl,domainName);
        [sender setTitle:[NSString stringWithFormat:@"%@ - %@",domainName,baseUrl] forState:UIControlStateNormal];
    }];

 3.demo下载地址:

github: 

https://github.com/JeanOrey/YJDomainManager

CocoaPod:

pod YJDomainManager

有何不足之处,希望大佬指正!

觉得好用的朋友,请不要吝啬您的Star????,跪谢!???♀????♀????♀?

 

iOS网络切换工具

上一篇:c# Visual Studio|There is no editor available for ***,make sure the application for the file type(.vb) is installed问题解决方法


下一篇:vue中Cannot read property '_wrapper' of undefined 报错