之所以有這個(gè)東西是因?yàn)榉奖阏{(diào)試和查問題恤煞,正常來說我們需要的基礎(chǔ)功能包括切換環(huán)境,看日志侄柔,清緩存共啃,限制網(wǎng)速測(cè)試等。還有一些的就是根據(jù)不同的業(yè)務(wù)場(chǎng)景來添加如快速訪問一個(gè)網(wǎng)頁暂题,特殊測(cè)試需求的入口等移剪。一定要注意這些功能都要判斷debug,不要出現(xiàn)在線上P秸摺W菘痢!
切換環(huán)境的原理就是我們?nèi)直4嬉粋€(gè)key來當(dāng)現(xiàn)在的環(huán)境言津,切換的時(shí)候改變value的值然后接口訪問的時(shí)候根據(jù)不同的value值拼接不同的接口鏈接就可以了攻人。
代碼如下:
AppDelegate設(shè)置默認(rèn)
#if DEBUG
[ELMEnvironmentManager setEnvironment:[[NSUserDefaults standardUserDefaults] objectForKey:@"LPDB_ENV"]? ((NSNumber *)[[NSUserDefaults standardUserDefaults] objectForKey:@"LPDB_ENV"]).integerValue: ELMEnvBeta];
#endif
環(huán)境枚舉如下:
typedef NS_ENUM(NSUInteger, ELMEnv) {
ELMEnvTesting = 1,
ELMEnvStaging,
ELMEnvBeta,
ELMEnvAlpha,
ELMEnvProduction,
};
看日志,我是在接口返回的基類里去保存下接口的返回值悬槽,然后在viewcontroller的基類里寫搖一搖進(jìn)行展示返回怀吻。
代碼如下:
1.創(chuàng)建一個(gè)KZWDebugService來管理日志的保存和刪除
#import "KZWDebugService.h"
#import "KZWConstants.h"
@implementation KZWDebugService
static 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.保存日志數(shù)據(jù):
#import "LPDBRequestObject.h"
- (void)handleRespondse:(NSURLSessionDataTask *)response responseObject:(id)responseObject error:(NSError *)error {
#if DEBUG
[KZWDebugService setCurrentDebug:responseObject];
[KZWDebugService saveDebug];
#endif
}
3.在基類KZWViewController中加搖一搖觸發(fā)
#if DEBUG
[[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];
#endif
#pragma mark - ShakeToEdit 搖動(dòng)手機(jī)之后的回調(diào)方法
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//檢測(cè)到搖動(dòng)開始
if (motion == UIEventSubtypeMotionShake)
{
// your code
NSLog(@"檢測(cè)到搖動(dòng)開始");
}
}
- (void) motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//搖動(dòng)取消KZWDebugService
NSLog(@"搖動(dòng)取消");
}
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
//搖動(dòng)結(jié)束
if (event.subtype == UIEventSubtypeMotionShake) {
// your code
NSLog(@"搖動(dòng)結(jié)束");
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//振動(dòng)效果 需要#import <AudioToolbox/AudioToolbox.h>
NSString *message = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:[KZWDebugService currentDebug] options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding]; //對(duì)展示進(jìn)行格式化處理
[[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;
}];
}
限制網(wǎng)速是去設(shè)置中的開發(fā)者里的network link conditioner進(jìn)行設(shè)置。
網(wǎng)速限制.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設(shè)置蓬坡,更多的是不同公司不同業(yè)務(wù)需求下的一些debug配置,讀者可以在評(píng)論里說出來磅叛,有意思的可以一起討論下屑咳。文章完。
代碼地址:https://github.com/ouyrp/KZWFoundation