ios - 检测何时是晚上/晚上 - NSDate - iOS

标签 ios objective-c time nsdate nscalendar

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

无论如何,我想出了一个“拙劣”的小方法,可以判断现在是早上/下午还是晚上。 (它也影响季节)。

但是我不确定它是否有任何好处,我正在徘徊是否有官方的方法来解决这个问题?也许使用一些 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");
 }

谢谢你抽出时间,丹。

最佳答案

以防万一您被允许使用 Wea​​ther 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
}

关于ios - 检测何时是晚上/晚上 - NSDate - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30402578/

相关文章:

ios - Facebook 的 invitable_friends 端点导致好友丢失

ios - 在水平 ASStackLayout 中垂直居中节点

ios - 如何跳出 IBAction?

java - LocalDate 解析抛出异常‽

android - 像 Google Agenda App 这样的新 TimePicker 对话框

ios - 显示对应物的 Xcode 快捷方式

ios - ios中的照片序列是混合的

iphone - Objective-c 调用 C 函数

java - 计算lucene搜索的时间

ios - 在 Swift 中从 URL 播放视频