如何讓App保持在運作狀態

本文介绍了一种方法来禁用iOS应用中的自动休眠功能,通过简单的一行代码即可实现。此外,还展示了如何在特定操作期间保持应用活跃状态,并提供了一个实际案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载自: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];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值