Development environment and release environment switch for easy testing

Requirements: Click somewhere to trigger an event, and you can freely switch between testing, pre-production, and production environments.

Principle: Use NSUserDefault or Singleton to maintain a collection of environment variables.

macro definition configuration

/****************Singleton mode macro**************/

#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 class

 

#import <Foundation/Foundation.h>

 

 

@interface FBChangeEnvironment : NSObject

MACRO_SHARED_INSTANCE_INTERFACE

// switch environment

- (void)changeEnvironment;

// get the current environment

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

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325023533&siteId=291194637