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"#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);//振動效果 需要#importNSString *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* __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配置,讀者可以在評論裡說出來,有意思的可以一起討論下。文章完。
程式碼地址:
作者:moonCoder
連結:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/2730/viewspace-2810630/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 點雲配準基礎
- Oracle網路配置之共享模式和專有模式Oracle模式
- IDEA的debug除錯--基礎小白篇Idea除錯
- Debug---Eclipse斷點除錯基礎Eclipse斷點除錯
- 急速 debug 實戰一(瀏覽器-基礎篇)瀏覽器
- 組合語言實驗1—Debug基礎操作組合語言
- 移動端適配 - 基礎知識篇
- JS 基礎篇(代理模式)JS模式
- Java基礎-建造者模式Java模式
- java 設計模式基礎Java設計模式
- Java基礎之代理模式Java模式
- Java基礎-單例模式Java單例模式
- 配運基礎資料快取瘦身實踐快取
- Debug模式應用程式輸出Debug除錯資訊模式除錯
- Clion Debug模式使用實踐模式
- Java基礎篇--設計模式Java設計模式
- Java基礎-抽象工廠模式Java抽象模式
- Java基礎-工廠方法模式Java模式
- 設計模式-適配者模式設計模式
- [JAVAFX基礎]掃雷經典入門低配版Java
- 牆裂科普:移動端適配的基礎概念
- web基礎(四)嚴格模式與混雜模式Web模式
- 【設計模式基礎】行為模式 - 5 - 策略(Strategy)設計模式
- Flutter深色模式適配Flutter模式
- 設計模式-UML關係基礎設計模式
- (譯)React 元件設計模式基礎React元件設計模式
- Nginx配置之基於域名的虛擬主機Nginx
- 【設計模式基礎】行為模式 - 7 - 迭代器(Iterator)設計模式
- 【設計模式基礎】行為模式 - 8 -狀態(State)設計模式
- 【大話設計模式】——淺談設計模式基礎設計模式
- JS 基礎篇(五):JS嚴格模式JS模式
- 設定模式基礎 之 3閉包模式
- Flutter適配深色模式(DarkMode)Flutter模式
- 【設計模式基礎】行為模式 - 6 - 模板方法(Template Method)設計模式
- pycharm開啟debug模式報錯彙總PyCharm模式
- 如何設定tomcat的非debug模式?Tomcat模式
- SAP SD基礎知識之銷售模式模式
- 【SAP業務模式】之ICS(二):基礎資料模式