标签:shared 地方 messages ids nim 环境 dispatch ctf round
需求:点击某个地方触发事件,可以自由的切换测试、预生产、生产三种环境。
原理:用NSUserDefault或者Singleton去维护环境变量集合。
宏定义配置
/***************单例模式宏**************/ #define MACRO_SHARED_INSTANCE_INTERFACE +(instancetype)sharedInstance;
#define MACRO_SHARED_INSTANCE_IMPLEMENTATION(CLASS) \ +(instancetype)sharedInstance \ { \ static CLASS * sharedInstance = nil; \ static dispatch_once_t onceToken; \ dispatch_once(&onceToken, ^{ \ sharedInstance = [[CLASS alloc] init]; \ }); \ return sharedInstance; \ }
#define APPDelegate ((AppDelegate*)[UIApplication sharedApplication].delegate) |
FBChangeEnvironment.h类
#import <Foundation/Foundation.h>
@interface FBChangeEnvironment : NSObject MACRO_SHARED_INSTANCE_INTERFACE //切换环境 - (void)changeEnvironment; //获得当前环境 - (NSString *)currentEnvironment; @end |
FBChangeEnvironment.m
备注:下面devConfig、prodConfig、prodConfig1改为自己服务器3种环境的地址
#import "FBChangeEnvironment.h" //UAT static NSString *const devConfig = @"0"; //预生产 static NSString *const prodConfig = @"1"; //生产 static NSString *const prodConfig1 = @"2";
@implementation FBChangeEnvironment
MACRO_SHARED_INSTANCE_IMPLEMENTATION(FBChangeEnvironment) //切换环境 - (void)changeEnvironment{ NSLog(@"change environment start");
NSString *title=@"切换环境"; NSString *subTitle=@"重启后生效, 非测试人员请点击cancel"; UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:subTitle preferredStyle:UIAlertControllerStyleActionSheet];
//修改title NSMutableAttributedString *alertControllerStr = [[NSMutableAttributedString alloc] initWithString:title]; // [alertControllerStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, alertControllerStr.length)]; [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:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, alertControllerMessageStr.length)]; [alertControllerMessageStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:NSMakeRange(0, alertControllerMessageStr.length)]; [alert setValue:alertControllerMessageStr forKey:@"attributedMessage"];
NSString *currentEnvironment=@""; if ([[self currentEnvironment] isEqualToString:devConfig]) { currentEnvironment=@"当前环境为 UAT";
[alert addAction:[UIAlertAction actionWithTitle:@"UAT" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { //测试环境 [[NSUserDefaults standardUserDefaults] setObject:devConfig forKey:@"serverFB"]; }]]; [alert addAction:[UIAlertAction actionWithTitle:@"预生产" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //预生产环境 [[NSUserDefaults standardUserDefaults] setObject:prodConfig forKey:@"serverFB"]; }]]; [alert addAction:[UIAlertAction actionWithTitle:@"生产" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //生产环境 [[NSUserDefaults standardUserDefaults] setObject:prodConfig1 forKey:@"serverFB"]; }]];
} else if ([[self currentEnvironment] isEqualToString:prodConfig]) { currentEnvironment=@"当前环境为 预生产";
[alert addAction:[UIAlertAction actionWithTitle:@"UAT" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //测试环境 [[NSUserDefaults standardUserDefaults] setObject:devConfig forKey:@"serverFB"]; }]]; [alert addAction:[UIAlertAction actionWithTitle:@"预生产" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { //预生产环境 [[NSUserDefaults standardUserDefaults] setObject:prodConfig forKey:@"serverFB"]; }]]; [alert addAction:[UIAlertAction actionWithTitle:@"生产" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //生产环境 [[NSUserDefaults standardUserDefaults] setObject:prodConfig1 forKey:@"serverFB"]; }]]; } else if ([[self currentEnvironment] isEqualToString:prodConfig1]) { currentEnvironment=@"当前环境为 生产";
[alert addAction:[UIAlertAction actionWithTitle:@"UAT" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //测试环境 [[NSUserDefaults standardUserDefaults] setObject:devConfig forKey:@"serverFB"]; }]]; [alert addAction:[UIAlertAction actionWithTitle:@"预生产" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { //预生产环境 [[NSUserDefaults standardUserDefaults] setObject:prodConfig forKey:@"serverFB"]; }]]; [alert addAction:[UIAlertAction actionWithTitle:@"生产" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { //生产环境 [[NSUserDefaults standardUserDefaults] setObject:prodConfig1 forKey:@"serverFB"]; }]]; }
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { // }]]; //当前选择的视图控制器需要自己赋值,比如tabbar的didSelectViewController里 [APPDelegate.currentSelectedVC.navigationController presentViewController:alert animated:YES completion:^{ // }]; } //获得当前环境 - (NSString *)currentEnvironment{ //默认生产环境 NSString *currentEnvironment=prodConfig1; if ([[NSUserDefaults standardUserDefaults] objectForKey:@"serverFB"]) { currentEnvironment=[[NSUserDefaults standardUserDefaults] objectForKey:@"serverFB"]; } return currentEnvironment; }
@end |
某个地方需要调用事件时,调用以下方法即可
- (void)changeEnvironment { [[FBChangeEnvironment sharedInstance]changeEnvironment]; } |
标签:shared 地方 messages ids nim 环境 dispatch ctf round
原文地址:https://www.cnblogs.com/huangzs/p/8962369.html