1. 程式人生 > >開發環境和發布環境切換以方便測試

開發環境和發布環境切換以方便測試

smu emf stand .com rst shee key can dac

需求:點擊某個地方觸發事件,可以自由的切換測試、預生產、生產三種環境。

技術分享圖片

原理:用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];

}

開發環境和發布環境切換以方便測試