通知天xcode

时间:2016-10-27 11:33:03

标签: objective-c xcode

我想在同一时间每天创建一个提醒。

示例代码如下:

- (IBAction)start:(id)sender {

    [defaults setBool:YES forKey:@"notificationIsActive"];
    [defaults synchronize];
    self.message.text=@"Напоминание включено";


    NSDate *date = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
    components.calendar = calendar;
    components.hour = 7;
    components.minute = 37;
    date = components.date;

    if([date compare:[NSDate date]] < 0) {
        date = [date dateByAddingTimeInterval:60*60*24];
    }

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];

    localNotification.alertBody = @"ТЕКСТ НАПОМИНАНИЯ";
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.repeatInterval = NSCalendarUnitDay;//NSCalendarUnitMinute; //Repeating instructions here.
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

}

提前致谢。

1 个答案:

答案 0 :(得分:2)

试试这个

NSDate *date = [NSDate date]; 
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
localNotif.fireDate = date;
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.repeatInterval=NSCalendarUnitDay;
localNotif.alertBody = @"Daily Notification"; 

localNotif.userInfo=@{
                     };
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];