一如既往,上来先夸波简书。我在HUAWEI的WIKI上写过文章,真心有点烂。排版不说,标题定下来后,还不能更改。要改还得另外起草一篇,实在不能再吐槽了……
我们leader提出一个思考问题,如果咋们的APP短时间内出现异常奔溃的情况,我们改如何快速处理了?
so,此篇博文就记录下自己的思维,并附上代码。
参数 isCrash crashCount crashTime
解题思路:程序启动把isCrash置为YES,程序退出时isCrash置为NO。
原理:每次启动检查一次isCrash,如果是YES,则数值crashCount+1,也就是第四次启动,则会清空内存。代码还加了一层保险装置,就是会存储每次进入APP的时间,当时间在一分钟以内并且出现isCrash,crashCount才会+1。
贴代码
#import "HCAppSafeModeConfig.h"
#import
static HCAppSafeModeConfig *instance;
@implementationHCAppSafeModeConfig
/**
参数 isCrash crashCount crashTime
原理:每次启动检查一次isCrash,如果是YES,则数值crashCount+1,也就是第四次启动,则会清空内存
整体思路:程序启动把isCrash置为YES,程序退出时isCrash置为NO。
**/
+(instancetype)shareInstance
{
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
instance = [[HCAppSafeModeConfig alloc]init];
});
return instance;
}
// app进入后台
- (void)appEnterBackground
{
[[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:@"Dean_isCrash"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
// app回到前台
- (void)appEnterForeground
{
[[NSUserDefaults standardUserDefaults] setObject:@"YES" forKey:@"Dean_isCrash"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
//存储当前时间戳
-(double)storageTimeInDefaults
{
NSDate* dat = [NSDate dateWithTimeIntervalSinceNow:0];
NSTimeInterval a=[dat timeIntervalSince1970];
NSString*timeString = [NSStringstringWithFormat:@"%0.f", a];//转为字符型
doubletimeFloat = [timeStringdoubleValue];
NSString*timeSting = [NSStringstringWithFormat:@"%0.f",timeFloat];
[[NSUserDefaults standardUserDefaults] setObject:timeSting forKey:@"Dean_time"];
returntimeFloat;
}
//记录奔溃次数
-(void)recordCrashCount
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
//每次进来先判断上次APP是否奔溃
BOOL isCrash = [[[NSUserDefaults standardUserDefaults]valueForKey:@"Dean_isCrash"] boolValue];
//取完isCrash后设置为YES
[[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:@"Dean_isCrash"];
//取上次进入App的时间
NSString *lastTime = [[NSUserDefaults standardUserDefaults]objectForKey:@"Dean_time"];
doubledLastTime = lastTime==nil?0.0:[lastTimedoubleValue];
//记录当前时间戳并返回当前时间
double nowTime = [self storageTimeInDefaults];
NSIntegercrashtime = nowTime - dLastTime;
if(isCrash){
if(crashtime>60) {
//如果奔溃时间间隔大于60秒 则清空CrashCount 并不再执行下去
[[NSUserDefaults standardUserDefaults]setObject:@0 forKey:@"Dean_CrashCount"];
return;
}
//如果奔溃,并且在预定时间内奔溃,则存下奔溃次数
NSNumber *crashNumber = [[NSUserDefaults standardUserDefaults]valueForKey:@"Dean_CrashCount"];
NSIntegercount =0;
if(crashNumber !=nil){
count = [crashNumberintegerValue] +1;
}
//如果达到三次,此处执行方法 method
if(count >=3){
//本地次数清零
[[NSUserDefaults standardUserDefaults]setObject:@0 forKey:@"Dean_CrashCount"];
//清除缓存
[self clearCacheAndFile];
}
//没达到3次存储到本地
crashNumber = [NSNumbernumberWithInteger:count];
[[NSUserDefaults standardUserDefaults]setObject:crashNumber forKey:@"Dean_CrashCount"];
[[NSUserDefaults standardUserDefaults]synchronize];
}
}
-(void)clearCacheAndFile
{
//循环路径下的每个子文件,并清空数据
NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSArray *subPathArry = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:filePath error:nil];
NSString*anotherPath =nil;
NSError*error =nil;
for(NSString*subpathinsubPathArry)
{
//清空数据
anotherPath = [filePathstringByAppendingPathComponent:subpath];
[[NSFileManager defaultManager] removeItemAtPath:anotherPath error:&error];
}
}
@end
博客地址为:http://3ms.huawei.com/km/groups/3198679/blogs/details/5636583
或者下载代码 :https://github.com/DeanZengboy/iOS-APP-Crash-