//時間の設定値(アプリの設定で入力できるようにした) NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSString *startTime = [[userDefaults stringForKey: @"startTime"] stringByReplacingOccurrencesOfString:@":" withString:@""]; NSString *endTime = [[userDefaults stringForKey: @"endTime"] stringByReplacingOccurrencesOfString:@":" withString:@""]; NSDate *date = [NSDate date]; // 現在の日時を得る NSCalendar *calendar = [NSCalendar currentCalendar]; // カレントカレンダー取得 NSCalendarUnit unit = NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; //時分秒を指定 NSDateComponents *dc = [calendar components:unit fromDate:date]; NSString *hh = [NSString stringWithFormat:@"%02ld",[dc hour]]; NSString *mm = [NSString stringWithFormat:@"%02ld",[dc minute]]; NSString *ss = [NSString stringWithFormat:@"%02ld",[dc second]]; NSString *now = [[hh stringByAppendingString:mm] stringByAppendingString:ss]; if (startTime.intValue < endTime.intValue) { // 日をまたがない場合 if (startTime.intValue <= now.intValue && now.intValue <= endTime.intValue) { //処理 [[UIScreen mainScreen] setBrightness:0.0f]; }else{ [[UIScreen mainScreen] setBrightness:1.0f]; } }else{ // 日をまたぐ場合 if (startTime.intValue <= now.intValue || now.intValue <= endTime.intValue) { //処理 [[UIScreen mainScreen] setBrightness:0.0f]; }else{ [[UIScreen mainScreen] setBrightness:1.0f]; } }