检测何时为 evening/night - NSDate - iOS

Detect when it is evening/night - NSDate - iOS

我有一个 iOS 应用程序。该应用程序的其中一个部分显示了特定位置的简单小天气预报。作为此预测的一部分,该应用程序还显示了一些图像。这些图像要么是基于白天的,在这种情况下它们将具有 'sun' 图像,要么基于夜间,在这种情况下它们将具有 'moon icon'.

总之,我想出了一个'botch-job'的小方法,可以判断现在是morning/afternoon还是晚上。 (它也影响季节)。

但是我不确定它是否有任何好处,我正在徘徊是否有官方的方法来解决这个问题?也许用一些 Apple API?解决我的问题的更好方法是什么?这是我的代码:

注意:确保你滚动浏览我的代码,有很多。

 NSDateComponents *component = [[NSCalendar currentCalendar] components:(NSCalendarUnitMonth | NSCalendarUnitHour) fromDate:[NSDate date]];

 NSInteger hours = [component hour];
 NSInteger month = [component month];

 NSLog(@"\n\nHOUR IS: %ld", (long)hours);
 NSLog(@"MONTH IS: %ld", (long)month);

 NSInteger eveningHour = 0;

 switch (month) {

     case 12: case 1: case 2:
          // It is winter time... so days are very short.
          // December, January, February.
          eveningHour = 16;
       break;

     case 3: case 4: case 5:
          // Its spring time... days are getting a bit longer.
          // March, April, May.
          eveningHour = 17;
       break;

     case 6: case 7: case 8:
          // Its summer time... so days are longer.
          // June, July, August.
          eveningHour = 20;
       break;

     case 9: case 10: case 11:
          // Its fall (autumn)... days are getting shorter.
          // September, October, November.
          eveningHour = 17;
       break;

     default: break;
 }

 if ((hours >= 0) && (hours < 12)) {
      // Morning...
      // Display normal sun icon.
      NSLog(@"Morning");
 }

 else if ((hours >= 12) && (hours < eveningHour)) {
      // Afternoon...
      // Display normal sun icon.
      NSLog(@"Afternoon");
 }

 else if ((hours >= eveningHour) && (hours <= 24)) {
      // Evening/Night...
      // Display moon icon.
      NSLog(@"Evening");
 }

谢谢你的时间,丹。

以防您被允许使用天气 API。

您可以使用以下库:OpenWeatherMapAPI

它给出了以下格式的数据:

NSDictionary 看起来像这样 (json):

{
    coord: {
        lon: 10.38831,
        lat: 55.395939
    },
    sys: {
        country: "DK",
        sunrise: 1371695759, // this is an NSDate
        sunset: 1371758660   // this is also converted to a NSDate
    },
    weather: [
        {
            id: 800,
            main: "Clear",
            description: "Sky is Clear",
            icon: "01d"
        }
    ],
    base: "global stations",
    main: {
        temp: 295.006,      // this is the the temperature format you´ve selected
        temp_min: 295.006,  //                 --"--
        temp_max: 295.006,  //                 --"--
        pressure: 1020.58,
        sea_level: 1023.73,
        grnd_level: 1020.58,
        humidity: 80
    },
    wind: {
        speed: 6.47,
        deg: 40.0018
    },
    clouds: {
        all: 0
    },
    dt: 1371756382,
    id: 2615876,
    name: "Odense",
    cod: 200
}
-(void)ShowTimeMessage
{
// For calculating the current date
NSDate *date = [NSDate date];

// Make Date Formatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"hh a EEEE"];

// hh for hour mm for minutes and a will show you AM or PM
NSString *str = [dateFormatter stringFromDate:date];
// NSLog(@"%@", str);

// Sperate str by space i.e. you will get time and AM/PM at index 0 and 1 respectively
NSArray *array = [str componentsSeparatedByString:@" "];

// Now you can check it by 12. If < 12 means Its morning > 12 means its evening or night

NSString *message;
NSString *timeInHour;
NSString *am_pm;

NSString *DayOfWeek;
if (array.count>2)
{
    // am pm case
    timeInHour = array[0];
    am_pm = array[1];
    DayOfWeek  = array[2];
}
else if (array.count>1)
{
    // 24 hours case
    timeInHour = array[0];
    DayOfWeek = array[1];
}

if (am_pm)
{

    if ([timeInHour integerValue]>=4 && [timeInHour integerValue]<=9 && [am_pm isEqualToString:@"AM"])
    {
        message = [NSString stringWithFormat:@"Morning"];
    }
    else if (([timeInHour integerValue]>=10 && [timeInHour integerValue]!=12 && [am_pm isEqualToString:@"AM"]) || (([timeInHour integerValue]<4 || [timeInHour integerValue]==12) && [am_pm isEqualToString:@"PM"]))
    {
        message = [NSString stringWithFormat:@"Afternoon"];
    }
    else if ([timeInHour integerValue]>=4 && [timeInHour integerValue]<=9 && [am_pm isEqualToString:@"PM"])
    {
        message = [NSString stringWithFormat:@"Evening"];

    }
    else if (([timeInHour integerValue]>=10 && [timeInHour integerValue]!=12 && [am_pm isEqualToString:@"PM"]) || (([timeInHour integerValue]<4 || [timeInHour integerValue]==12) && [am_pm isEqualToString:@"AM"]))
    {
        message = [NSString stringWithFormat:@"Night"];
    }

}
else
{
    if ([timeInHour integerValue]>=4 && [timeInHour integerValue]<10)
    {
        message = [NSString stringWithFormat:@"Morning"];

    }
    else if ([timeInHour integerValue]>=10 && [timeInHour integerValue]<16)
    {
        message = [NSString stringWithFormat:@"Afternoon"];

    }
    else if ([timeInHour integerValue]>=16 && [timeInHour integerValue]<22)
    {
        message = [NSString stringWithFormat:@"Evening"];

    }
    else
    {
        message = [NSString stringWithFormat:@"Night"];

    }
}

if (DayOfWeek)
{
    _timeLbl.text=[NSString stringWithFormat:@"%@ %@",DayOfWeek,message];
}

}