我在这里写的应用程序设置了一个固定的参考日,在这一天之后我想得到警报!
使用此代码我设置日期和时间:第3天(星期二)和第11小时。 这很棒!在viewDidload中使用Timer我的navigationItem.title显示目标日的日/小时/分钟......
现在我将设置一个计时器到这一天/时间,当我在星期二(星期三或星期四)之后打开我的应用程序时,计时器为0并且警报显示!
with:
NSString *currentTimestamp = [NSString stringWithFormat:@"%.f", [resetTime timeIntervalSince1970]];
_epochTimeer.text = currentTimestamp;
我将目标时间转换为我的计时器的纪元时间!
- (void) resetTimer:(id)sender {
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [gregorian components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setWeekday:3]; //2 = Monday 3 di 4 = mi 5 = do 6 = fr 7 = sa
[nowComponents setWeek: [nowComponents week]+ 1 ]; //Next week [nowComponents week] + 1]
[nowComponents setHour:11]; //8a.m.
[nowComponents setMinute:0];
[nowComponents setSecond:0];
NSDate *resetTime = [gregorian dateFromComponents:nowComponents];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MM-YYYY HH:mm:ss"];
NSDate *date1 = today;
NSDate *date2 = resetTime;
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
int days = (int)interval / 86400;
int hours = (interval / 3600)- (days * 24);
int minutes = ((interval / 60) - (days * 24 * 60) -(hours * 60));
int sekunden = (days * 86400) + (hours * 3600) +(minutes * 60);
NSString *timeDiff = [NSString stringWithFormat:@"%02d Tage : %02d Std : %02d Min",days, hours, minutes];
self.navigationItem.title = [NSString stringWithFormat:@"%@",timeDiff];
NSString *currentTimestamp = [NSString stringWithFormat:@"%.f", [resetTime timeIntervalSince1970]];
_epochTimeer.text = currentTimestamp;
NSLog(@"timer: %@",timeDiff);
NSLog(@"timer: %@",currentTimestamp);
}
如何在此纪元时间或目标日期/时间设置我的计时器? 我是计时器和NSDate的初学者,我不明白我怎么能写代码!
或是否有一个完全不同的解决方案,以便在我打开我的APP后的目标日之后获取我的警报!
谢谢Jürgen
抱歉我的英语不好..: - )
感谢您的帮助,我添加您的代码并将localNotification.fireDate设置为resetTime:
- (void) resetTimer:(id)sender {
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setLocale:[NSLocale currentLocale]];
NSDateComponents *nowComponents = [gregorian components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setWeekday:7]; //2 = Monday 3 di 4 = mi 5 = do 6 = fr 7 = sa
[nowComponents setWeek: [nowComponents week]]; //Next week [nowComponents week] + 1]
[nowComponents setHour:9]; //8a.m.
[nowComponents setMinute:25];
[nowComponents setSecond:0];
NSDate *resetTime = [gregorian dateFromComponents:nowComponents];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd-MM-YYYY HH:mm:ss"];
NSDate *date1 = today;
NSDate *date2 = resetTime;
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
int days = (int)interval / 86400;
int hours = (interval / 3600)- (days * 24);
int minutes = ((interval / 60) - (days * 24 * 60) -(hours * 60));
int sekunden = (days * 86400) + (hours * 3600) +(minutes * 60);
NSString *timeDiff = [NSString stringWithFormat:@"%02d Tage : %02d Std : %02d Min",days, hours, minutes];
_showTimer.text = [NSString stringWithFormat:@"%02d",sekunden];
self.navigationItem.title = [NSString stringWithFormat:@"%@",timeDiff];
NSString *currentTimestamp = [NSString stringWithFormat:@"%.f", [resetTime timeIntervalSince1970]];
destinationDate = [NSDate dateWithTimeIntervalSince1970:1425981600];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate =resetTime;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
NSLog(@"timer: %@",timeDiff);
NSLog(@"timer: %@",currentTimestamp);
}
这是我的提醒:
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// code to display an alert here
SCLAlertView *alert = [[SCLAlertView alloc] init];
alert.backgroundType = Blur;
UIColor *color = [UIColor colorWithRed:52/255.0 green:222/255.0 blue:192/255.0 alpha:255/255.0];
[alert showCustom:self image:[UIImage imageNamed:@"geist"] color:color title:NSLocalizedString(@"Auto Reset", nil) subTitle:NSLocalizedString(@"Automatischer Reset Aktiviert!", nil) closeButtonTitle:@"OK" duration:0.0f];
}
当时间到期并且我开始我的应用程序...警报不是,它不会发生..
我需要做的是在某处激活通知吗?
感谢
好的,我现在已经在AppDelegate.m中添加了您的代码
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
UIAlertView* alert= [[UIAlertView alloc]initWithTitle:@"My App"
message:@"Reset"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}}
现在它可以工作,当我启动我的应用程序时,警报显示,但它循环闪烁,我不能Klick OK!
现在是什么......?
答案 0 :(得分:0)
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate =<target date>;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
// code to display an alert here
}