Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 本地通知设定点火时间_Ios_Swift - Fatal编程技术网

Ios 本地通知设定点火时间

Ios 本地通知设定点火时间,ios,swift,Ios,Swift,我想根据用户输入设置每日报警。就像用户将从日期选择器“10:30”中选择时间一样,然后我需要每天在该时间设置警报。我编写以下代码: func setAlarmAtTime(#time:NSString, withMessage message:NSString){ var loacalNotification = UILocalNotification(); var calendar = NSCalendar.currentCalendar(); calendar.tim

我想根据用户输入设置每日报警。就像用户将从日期选择器“10:30”中选择时间一样,然后我需要每天在该时间设置警报。我编写以下代码:

func setAlarmAtTime(#time:NSString, withMessage message:NSString){
    var loacalNotification = UILocalNotification();
    var calendar = NSCalendar.currentCalendar();
    calendar.timeZone = NSTimeZone.localTimeZone()
    var components = calendar.components(NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.MonthCalendarUnit | NSCalendarUnit.DayCalendarUnit, fromDate: NSDate.date());
    NSLog("%@",NSDate.date())
    NSLog(time);
    var timeComponents = time.componentsSeparatedByString(":");
    components.hour = timeComponents[0].integerValue;
    components.minute = timeComponents[1].integerValue;
    if components.isValidDateInCalendar(calendar){
        var fireDate = calendar.dateFromComponents(components);
        NSLog("%@",fireDate!);
        loacalNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay;
        loacalNotification.timeZone = NSTimeZone.localTimeZone();
        loacalNotification.fireDate = fireDate;
        loacalNotification.repeatInterval = NSCalendarUnit.CalendarUnitDay;
        loacalNotification.soundName = UILocalNotificationDefaultSoundName;
        loacalNotification.alertBody = message;
    }
但它根据时区显示不同的时间,以下是我尝试将警报设置为6:40时的行为:

Current Date:  2014-08-12 12:07:21 +0000
Alarm Time: 6:40
Fire Date: 2014-08-12 01:10:00 +0000

我试图将时区设置为本地和当前,但没有任何效果:(

给定的时间是格林威治标准时间(GMT)

我们只需要设置

localNotification.timeZone = [NSTimeZone systemTimeZone]; 
因为它将代表您的设备或系统时区并计划您的应用程序

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

您的本地时区是什么?例如,如果您的本地时区是GTM+0530,则火灾日期是正确的。请记住,
NSLog
在打印日期时不考虑时区。
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"dd-MMM-yyyy HH:mm"];
        NSDate *reminderDate = [df dateFromString:self.lblDate.text];
        // Schedule the notification
        UILocalNotification* localNotification = [[UILocalNotification alloc] init];
        localNotification.fireDate = reminderDate;
        localNotification.alertBody = self.txtName.text;
        localNotification.alertAction = @"Show me the item";
        localNotification.timeZone = [NSTimeZone defaultTimeZone];
        localNotification.soundName =@"sound.mp3";
        NSMutableDictionary *notifyDict = [[NSMutableDictionary alloc] init];
        [notifyDict setValue:self.lblType.text forKey:NOTIFICATION_TYPE];
        [notifyDict setValue:self.txtName.text forKey:NOTIFICATION_TITLE];
        if (![self.tvDescription.text isEqualToString:@"Write Description"]) {
            [notifyDict setValue:self.tvDescription.text forKey:NOTIFICATION_DESCRIPTION];
        }
        [notifyDict setValue:self.lblDate.text forKey:NOTIFICATION_DATE];
        localNotification.userInfo = notifyDict;
        localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
        [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
        [self.navigationController popViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadData" object:self];