【问题标题】:Local notification set default time 9:00 am for user timezone in iOS本地通知为 iOS 中的用户时区设置默认时间上午 9:00
【发布时间】:2015-09-09 00:27:36
【问题描述】:

我想为每个用户时区时间设置通知默认上午 9:00

如何为默认时间 9:00 am 创建 触发日期

【问题讨论】:

    标签: ios iphone xcode timezone


    【解决方案1】:
    1. This 是关于本地通知的完整教程

    2. 下面是解决上面提到的时区问题。请详细查看这些时区函数并在您的函数中设置适当的时区。


    [NSTimeZone defaultTimeZone]
    
    [NSTimeZone systemTimeZone]
    
    [NSTimeZone localTimeZone]
    

    NSTimeZone: what is the difference between localTimeZone and systemTimeZone?

    【讨论】:

    • #Renish 你是对的,但我需要为每个时区设置默认时间,所以我得到了上面的 Ans 表格。谢谢你的回答。
    • @mitulmarsonia,如果您获得用户设备时区(获取用户设备时间!),则无需更多工作。你应该怎么想?
    • 设备时区变得容易,但我不知道如何设置我们的时间,所以我有这样的问题。现在我知道了如何在任何时区设置我们的默认时间。
    【解决方案2】:

    试试这个代码。

     NSDate *pickerDate = [date_picker date];
    // Schedule the notification
    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = pickerDate;
    localNotification.alertBody = @"Alert Body Message";
    localNotification.alertAction = @"Alert Action Message";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    

    【讨论】:

      【解决方案3】:

      你好,我知道了答案:-

      NSDate *pickerDate = [NSDate date]; 
          NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
          [dateFormatter setDateFormat:DT_FORMATE_BIRTHDATE];
          NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:DT_TIME_ZONE_GMT];
          [dateFormatter setTimeZone:gmt];
      
          NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
          NSDateComponents *timeComponents = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit )
                                                         fromDate:pickerDate];
          [timeComponents setHour:9];
          [timeComponents setMinute:00];
          [timeComponents setSecond:0];
      
          NSDate *dtFinal = [calendar dateFromComponents:timeComponents];
          NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
          [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
      
          NSString *fierDate = [formatter stringFromDate:dtFinal];
      

      【讨论】:

      • 这里的DT_TIME_ZONE_GMT 是什么?
      • DT_TIME_ZONE_GMT = @"GMT"
      猜你喜欢
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 2017-07-05
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      相关资源
      最近更新 更多