Objective-C (iOS): スリープしないようにする

NoSleepViewController

Objective-C: NoSleepViewController.h

#import <UIKit/UIKit.h>

@interface NoSleepViewController : UIViewController

@end

Objective-C: NoSleepViewController.m

#import "NoSleepViewController.h"

@interface NoSleepViewController ()

@end

@implementation NoSleepViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(50, 50, 220, 50);
    [button setTitle:@"スリープしないようにする" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonWasTapped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}

- (void)buttonWasTapped:(UIButton *)button
{
    if ([button.titleLabel.text isEqual:@"スリープするようにする"]) {
        [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
        [button setTitle:@"スリープしないようにする" forState:UIControlStateNormal];
    } else {
        [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
        [button setTitle:@"スリープするようにする" forState:UIControlStateNormal];
    }
}

@end