Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.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 无法在swift中安排在特定时间和日期发出本地通知_Ios_Swift_Notifications - Fatal编程技术网

Ios 无法在swift中安排在特定时间和日期发出本地通知

Ios 无法在swift中安排在特定时间和日期发出本地通知,ios,swift,notifications,Ios,Swift,Notifications,我正试图在swift中的特定时间安排通知。我的时间字符串是一个看起来像08:32或12:23的字符串。当我试图在这个特定的日期和时间发出通知时,通知只在特定的日期发出,而不是在特定的时间发出。例如,如果我将年份改为2016年,它将不会启动。如果我改变月份,它就不会开火。然而,不管我输入的时间是什么,如果日期与当前日期匹配,我将在2015年1月28日写这篇文章,通知会在任何时间发出,事实上,当我关闭应用程序时,它会在代码输入时立即发出 func applicationDidEnterBackgro

我正试图在swift中的特定时间安排通知。我的时间字符串是一个看起来像08:32或12:23的字符串。当我试图在这个特定的日期和时间发出通知时,通知只在特定的日期发出,而不是在特定的时间发出。例如,如果我将年份改为2016年,它将不会启动。如果我改变月份,它就不会开火。然而,不管我输入的时间是什么,如果日期与当前日期匹配,我将在2015年1月28日写这篇文章,通知会在任何时间发出,事实上,当我关闭应用程序时,它会在代码输入时立即发出

func applicationDidEnterBackground(application: UIApplication) {
}
我的代码是编译的,所以我认为我的代码中没有错误

是否有任何方法可以在特定时间而不仅仅是swift中的某个日期发出通知,如果有,我该如何实施

            var timeString : String =  "2015-01-28 " + TimeString;
            println(timeString);

            let formatter = NSDateFormatter()
            formatter.dateFormat = "yyyy-MM-dd HH:mm"
            formatter.timeZone = NSTimeZone(abbreviation: "GMT");
            var date = formatter.dateFromString(timeString)



            var localNotification:UILocalNotification = UILocalNotification()
            localNotification.alertAction = "Block"
            localNotification.alertBody = block.description() + " Started";
            localNotification.fireDate = date
            UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

尝试使用此方法设置日期:

let calendar = NSCalendar.currentCalendar()
    let calendarComponents = NSDateComponents()
    calendarComponents.hour = 8   //you can get you time components 
    calendarComponents.second = 0 //from your string rather than 
    calendarComponents.minute = 0 //using fixed values
    calendarComponents.day = 28
    calendarComponents.year = 2015
    calendarComponents.month = 1
    let dateToFire = calendar.dateFromComponents(calendarComponents)

这对我有用。希望这有帮助。:)

你能在设置日期变量后打印出日期变量并检查它是否符合你的预期时间吗?我是这个项目的合作者,我可以确认它确实符合预期时间。在设置fireDate之前,你忘记为你的localNotification设置所需的时区。你必须检查fireDate时间是否在或之后在实际时间之前,如果fireDate在“现在”之后,您可以设置为同一天,但如果fireDate在“现在”之前,您必须将其设置为“明天”,我认为您应该使用本地时区来处理通知和日期组件