源码

KZWFoudation基础配置之Debug模式

之所以有这个东西是因为方便调试和查问题,正常来说我们需要的基础功能包括切换环境,看日志,清缓存,限制网速测试等。还有一些的就是根据不同的业务场景来添加如快速访问一个网页,特殊测试需求的入口等。一定要注意这些功能都要判断debug,不要出现在线上!!!
切换环境的原理就是我们全局保存一个key来当现在的环境,切换的时候改变value的值然后接口访问的时候根据不同的value值拼接不同的接口链接就可以来。
代码如下:

AppDelegate设置默认
#if DEBUG
    [ELMEnvironmentManager setEnvironment:[[NSUserDefaults standardUserDefaults] objectForKey:@"LPDB_ENV"]? ((NSNumber *)[[NSUserDefaults standardUserDefaults] objectForKey:@"LPDB_ENV"]).integerValue: ELMEnvBeta];
#endif
环境枚举如下:
typedef NS_ENUM(NSUInteger, ELMEnv) {
    ELMEnvTesting = 1,
    ELMEnvStaging,
    ELMEnvBeta,
    ELMEnvAlpha,
    ELMEnvProduction,
};

看日志,我是在接口返回的基类里去保存下接口的返回值,然后在viewcontroller的基类里写摇一摇进行展示返回。
代码如下:
1.创建一个KZWDebugService来管理日志的保存和删除

#import "KZWDebugService.h"

@implementation KZWDebugService

static id _debug = nil;

+ (id)currentDebug {
    @synchronized(self) {
        if (!_debug) {
            NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kLPDBDebugKey];
            if (data) {
                _debug = [NSKeyedUnarchiver unarchiveObjectWithData:data];
            }
        }
    }
    return _debug;
}

+ (void)setCurrentDebug:(id)debug {
    @synchronized(self) {
        _debug = debug;
    }
}

+ (void)saveDebug {
    @synchronized(self) {
        if (_debug == nil) {
            [[NSUserDefaults standardUserDefaults] removeObjectForKey:kLPDBDebugKey];
        } else {
            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_debug];
            [[NSUserDefaults standardUserDefaults] setObject:data forKey:kLPDBDebugKey];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    }
}

+ (void)deleteDebug {
    @synchronized(self) {
        _debug = nil;
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:kLPDBDebugKey];
    }
}

@end

2.在基类KZWViewController中加摇一摇触发

#if DEBUG
    [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
#endif
#pragma mark - ShakeToEdit 摇动手机之后的回调方法

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    //检测到摇动开始
    if (motion == UIEventSubtypeMotionShake)
    {
        // your code
        NSLog(@"检测到摇动开始");
    }
}

- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    //摇动取消
    NSLog(@"摇动取消");
}

- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    //摇动结束
    if (event.subtype == UIEventSubtypeMotionShake) {
        // your code
        NSLog(@"摇动结束");
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//振动效果 需要#import 

        NSString *message = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:[KZWDebugService currentDebug] options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];  //对展示进行格式化处理
        [[CQHUD sharedCQHUD] showDebug:message];

    }
}
- (void)showDebug:(NSString *)message {
    [[[[UIApplication sharedApplication] delegate] window] addSubview:self.bgView];
    self.bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0000));
    }];



    UIScrollView *scrollerview = [[UIScrollView alloc] initWithFrame:CGRectMake(10, KZW_StatusBarAndNavigationBarHeight, SCREEN_WIDTH - 20, SCREEN_HEIGHT - KZW_StatusBarAndNavigationBarHeight - KZW_TabbarHeight)];
    scrollerview.backgroundColor = [UIColor whiteColor];


    UILabel *label = [UILabel initWithFrame:CGRectMake(00, SCREEN_WIDTH - 20, SCREEN_HEIGHT - KZW_StatusBarAndNavigationBarHeight - KZW_TabbarHeight) textColor:[UIColor colorWithHexString:FontColor333333] font:FontSize26];
    label.text = message;
    [label sizeToFit];
    [scrollerview addSubview:label];
    scrollerview.contentSize = CGSizeMake(SCREEN_WIDTH - 20, label.frame.size.height);
    [self.bgView addSubview:scrollerview];

    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 30 - 20, KZW_iPhoneX?44:203030)];
    [backButton setImage:[UIImage imageNamed:@"bg_cancel"] forState:UIControlStateNormal];
    [self.bgView addSubview:backButton];

    @WeakObj(self)
    [backButton touchUpInside:^{
        @StrongObj(self)
        [self.bgView removeFromSuperview];
        self.bgView = nil;
    }];
}

限制网速是去设置中的开发者里的network link conditioner进行设置。

(2)

本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/kzwfoudationjichupeizhizhidebugmoshi.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:7 月 12, 2018 at 07:07 下午

热评文章

发表回复

[必填]

我是人?

提交后请等待三秒以免造成未提交成功和重复