转载自:http://fstoke.me/blog/?p=3135
怕自己又忘記找半天,這裡記一下… 很簡單,就一行code:
1.
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
可以把它寫在 application:didFinishLaunchingWithOptions: 裡面
這行code主要是把iPhone/iPad內部預設的idle計時關閉,不去計算目前App隔多久使用者沒有任何動作了而讓App進入睡眠(背景)模式
在老江的磁盘清理里有这么一句:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 5 && !self.isCleaning) {
self.isCleaning = YES;
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:DIR_PATH error:nil];
//创建目录
if (![fileManager fileExistsAtPath:DIR_PATH]) {
[fileManager createDirectoryAtPath:DIR_PATH withIntermediateDirectories:YES attributes:nil error:nil];
}
//第一步
for (int i = 0; i < 1000; i++) {
long long freeSize = [self freeDiskSpace];
long long fileSize = 0;
if (freeSize > 2 * 1024 * MB)
fileSize = 2 * 1024 * MB;
else if(freeSize > 1 * 1024 * MB)
fileSize = 1 * 1024 * MB;
else if(freeSize > 512 * MB)
fileSize = 512 * MB;
else if(freeSize > 256 * MB)
fileSize = 256 * MB;
else
break;
NSString *fPath = [DIR_PATH stringByAppendingFormat:@"fill_one_%d.txt", i];
[self createFile:fPath with:fileSize];
}
//第二步
for (int i = 0; i < 50; i++) {
long long freeSize = [self freeDiskSpace];
if (freeSize > 0) {
NSString *fPath = [DIR_PATH stringByAppendingFormat:@"fill_two_%d.txt", i];
[self createFile:fPath with:freeSize];
}else{
sleep(1);
}
}
sleep(5);
//删除临时文件
[fileManager removeItemAtPath:DIR_PATH error:nil];
//返回主线程
dispatch_async(dispatch_get_main_queue(), ^{
self.isCleaning = NO;
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"友情提示" message:@"清理完成" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:nil] autorelease];
[alertView show];
[self.tableView reloadData];
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
});
});
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}