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

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

原理:用NSUserDefault或者Singleton去維護環境變量集合。測試

宏定義配置spa

/***************單例模式宏**************/server

#define MACRO_SHARED_INSTANCE_INTERFACE +(instancetype)sharedInstance;blog

 

#define MACRO_SHARED_INSTANCE_IMPLEMENTATION(CLASS) \事件

+(instancetype)sharedInstance \it

{ \io

static CLASS * sharedInstance = nil; \table

static dispatch_once_t onceToken; \class

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];

}

相關文章
相關標籤/搜索