Mac开发之防止系统进入休眠模式

在开发过程中遇到电脑休眠网络断掉的问题,记录下来以便以后查阅。Mac系统如果设置了休眠,一段时间没有操作电脑,系统就会进入休眠模式,这是为了节能,延长电池的使用时间。但是如果我们开发的软件需要一直依赖网络,则不能让系统进入休眠模式。具体代码:

#pragma mark - 防止系统休眠
- (void)preventSystemSleep {
    // kIOPMAssertionTypeNoDisplaySleep prevents display sleep,
    // kIOPMAssertionTypeNoIdleSleep prevents idle sleep
    
    // reasonForActivity is a descriptive string used by the system whenever it needs
    // to tell the user why the system is not sleeping. For example,
    // "Mail Compacting Mailboxes" would be a useful string.
    
    //  NOTE: IOPMAssertionCreateWithName limits the string to 128 characters.
    CFStringRef reasonForActivity= CFSTR("Describe Activity Type");
    
    IOPMAssertionID assertionID;
    IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
                                                   kIOPMAssertionLevelOn, reasonForActivity, &assertionID);
    if (success == kIOReturnSuccess)
    {
        
        // Add the work you need to do without
        // the system sleeping here.
        
//        success = IOPMAssertionRelease(assertionID);
        // The system will be able to sleep again.
    }

}
只要调用preventSystemSleep这个方法,就可以使你的App在运行期间系统不会进入休眠模式,从而保障你的App顺利运行,不会出现断网等各种各样的问题。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值