LoginSignup
17
18

More than 5 years have passed since last update.

Macアプリを開いている間はスリープさせない2行。

Last updated at Posted at 2014-04-01

サイネージ用アプリ、VJアプリなんかを作るときに、
いちいちMacの省エネルギー、スクリーンセーバーを変えるのはイケてないので。
アプリが開いている間はスリープしないようにする設定。

使い方

SampleAppDelegate.m

#import "LittleCaffeine.h"

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{    
    LittleCaffeineEnable();
}


- (void) applicationWillTerminate:(NSNotification *)notification
{
    LittleCaffeineDisable();
}


ソース

LittleCaffeine.h
//
//  LittleCaffeine.h
//
//  This is tiny utility inspired by Caffeine http://lightheadsw.com/caffeine/
//
//  Created by Koki Ibukuro on 2014/04/01.
//  Copyright (c) 2014年 Koki Ibukuro. All rights reserved.
//

#pragma once

#ifndef Little_Caffeine_h
#define Little_Caffeine_h

#import <Foundation/Foundation.h>
#import <IOKit/pwr_mgt/IOPMLib.h>

static IOPMAssertionID assertionID;

static BOOL LittleCaffeineEnable() {

    // https://developer.apple.com/library/mac/qa/qa1340/_index.html

    CFStringRef reasonForActivity= CFSTR("Little Caffeine for Wake Up");
    IOReturn success = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep,
                                                   kIOPMAssertionLevelOn, reasonForActivity, &assertionID);

    if (success == kIOReturnSuccess) {
        return YES;
    }
    else {
        assertionID = -1;
        return NO;
    }
}

static BOOL LittleCaffeineDisable() {
    IOReturn success = NO;
    if(assertionID > 0) {
        success = IOPMAssertionRelease(assertionID);
    }

    if(success == kIOReturnSuccess) {
        return YES;
    }
    else {
        return NO;
    }
}

#endif


17
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
18