如上图所示手动切换环境:
1、先实现一个切换环境的类
#import
@interface ChangeEnvironment : NSObject
+ (instancetype)sharedInstance;
//切换环境
- (void)changeEnvironment;
//获得当前环境
- (NSString *)currentEnvironment;
@end
#import "ChangeEnvironment.h"
#import
#define kUrlKey @"urlKey"
//内网环境
static NSString *const devConfig = @"0";
//外网环境
static NSString *const prodConfig = @"1";
@implementation ChangeEnvironment
+ (instancetype)sharedInstance {
staticChangeEnvironment * sharedInstance =nil;
staticdispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[ChangeEnvironment alloc] init];
});
returnsharedInstance;
}
//切换环境
- (void)changeEnvironment {
NSString *title=@"切换环境";
NSString *subTitle=@"重启后生效, 放弃切换环境请点击取消";
UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:subTitle preferredStyle:UIAlertControllerStyleActionSheet];
//修改title
NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title];
[alertControllerStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:NSMakeRange(0, alertControllerStr.length)];
[alert setValue:alertControllerStr forKey:@"attributedTitle"];
//修改message
NSMutableAttributedString *alertControllerMessageStr = [[NSMutableAttributedString alloc] initWithString:subTitle];
[alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, alertControllerMessageStr.length)];
[alert setValue:alertControllerMessageStr forKey:@"attributedMessage"];
NSString *currentEnvironment =@"";
[alert addAction:[UIAlertAction actionWithTitle:@"内网环境"style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnullaction) {
//内网环境
[[NSUserDefaults standardUserDefaults] setObject:devConfig forKey:kUrlKey];
//重置请求的基url
[selfresetRequestBaseUrl];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"外网环境"style:UIAlertActionStyleDestructive handler:^(UIAlertAction *_Nonnullaction) {
//外网环境
[[NSUserDefaults standardUserDefaults] setObject:prodConfig forKey:kUrlKey];
//重置请求的基url
[selfresetRequestBaseUrl];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:^(UIAlertAction *_Nonnullaction) {
}]];
NSLog(@"currentEnvironment = %@", currentEnvironment);
//当前选择的视图控制器需要自己赋值,比如tabbar的didSelectViewController里
[[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:YEScompletion:nil];
}
//获得当前环境
- (NSString *)currentEnvironment {
//默认测试环境
NSString *currentEnvironment = devConfig;
if([[NSUserDefaults standardUserDefaults] objectForKey:kUrlKey]) {
currentEnvironment=[[NSUserDefaults standardUserDefaults] objectForKey:kUrlKey];
}
returncurrentEnvironment;
}
- (void)resetRequestBaseUrl {
#pragma mark- 切换内网环境和外网环境
if([[ChangeEnvironment sharedInstance].currentEnvironment isEqualToString:@"0"]) {
NSLog(@"内网环境。。。。。。。");
//测试环境
[MGRequest setDefaultUrl:@"内网环境地址"];
[[NSUserDefaults standardUserDefaults] setObject:devConfig forKey:kUrlKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}elseif([[ChangeEnvironment sharedInstance].currentEnvironment isEqualToString:@"1"]) {
NSLog(@"外网环境。。。。。。。");
//外网环境
[MGRequest setDefaultUrl:@"外网环境地址"];
[[NSUserDefaults standardUserDefaults] setObject:prodConfig forKey:kUrlKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
@end
2、调用切换环境的类即可: