之所以有这个东西是因为方便调试和查问题,正常来说我们需要的基础功能包括切换环境,看日志,清缓存,限制网速测试等。还有一些的就是根据不同的业务场景来添加如快速访问一个网页,特殊测试需求的入口等。一定要注意这些功能都要判断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"#import "KZWConstants.h"@implementation KZWDebugServicestatic id _debug = nil; + (id)currentDebug { @synchronized(self) { if (!_debug) { NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:KZWDEBUGKEY]; 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:KZWDEBUGKEY]; } else { NSData *data = [NSKeyedArchiver archivedDataWithRootObject:_debug]; [[NSUserDefaults standardUserDefaults] setObject:data forKey:KZWDEBUGKEY]; [[NSUserDefaults standardUserDefaults] synchronize]; } } } + (void)deleteDebug { @synchronized(self) { _debug = nil; [[NSUserDefaults standardUserDefaults] removeObjectForKey:KZWDEBUGKEY]; } }@end
2.保存日志数据:
#import "LPDBRequestObject.h"- (void)handleRespondse:(NSURLSessionDataTask *)response responseObject:(id)responseObject error:(NSError *)error {#if DEBUG [KZWDebugService setCurrentDebug:responseObject]; [KZWDebugService saveDebug];#endif}
3.在基类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 { //摇动取消KZWDebugService NSLog(@"摇动取消"); } - (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event { //摇动结束 if (event.subtype == UIEventSubtypeMotionShake) { // your code NSLog(@"摇动结束"); AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//振动效果 需要#import <AudioToolbox/AudioToolbox.h> NSString *message = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:[KZWDebugService currentDebug] options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding]; //对展示进行格式化处理 [[KZWHUD sharedKZWHUD] 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(0, 0, 0, 0)); }]; 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 alloc] initWithFrame:CGRectMake(0, 0, 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:20, 30, 30)]; [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进行设置。
网速限制.jpeg
清缓存在之前的文章中说过,就直接放代码了:
- (void)clearCache { if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) { WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore]; [dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] completionHandler:^(NSArray<WKWebsiteDataRecord *> * __nonnull records) { for (WKWebsiteDataRecord *record in records) { if ( [record.displayName containsString:@"xxxxx"]) { [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes forDataRecords:@[record] completionHandler:^{ NSLog(@"Cookies for %@ deleted successfully",record.displayName); [KZWTosatView showToastWithMessage:@"清除成功" view:self.view]; }]; } } }]; }else { NSString *librarypath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject; NSString *cookiesFolderPath = [librarypath stringByAppendingString:@"/Cookies"]; [[NSFileManager defaultManager] removeItemAtPath:cookiesFolderPath error:nil]; } NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (NSHTTPCookie *cookie in [cookieJar cookies]) { [cookieJar deleteCookie:cookie]; } }
这只是一些通用的debug设置,更多的是不同公司不同业务需求下的一些debug配置,读者可以在评论里说出来,有意思的可以一起讨论下。文章完。
代码地址:https://github.com/ouyrp/KZWFoundation
作者:moonCoder
链接:https://www.jianshu.com/p/e2620fd35f4a