gpt4 book ai didi

ios - Objective-C:UILocalNotification

转载 作者:行者123 更新时间:2023-11-29 11:58:41 25 4
gpt4 key购买 nike

View Controller :

- (void)viewDidLoad
{
`[super viewDidLoad];`

dateFormatter.dateFormat = @"dd/MM";
timeFormatter.dateFormat = @"HH:mm";
NSString *dateString = [dateFormatter stringFromDate:dateTime];
NSString *timeString = [timeFormatter stringFromDate:dateTime];

if ([dateString isEqual:@"23/06"]) {
if ([timeString isEqual:@"23:30"]) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:@"It's 11:30 PM 23th June!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
}
}

应用委托(delegate):

[[UIApplication sharedApplication] scheduledLocalNotifications];

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){

[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge| UIUserNotificationTypeSound categories:nil]];
}

当时间等于字符串和日期时,未收到通知。请帮忙!

最佳答案

此问题的核心问题(除了使用字符串比较日期之外)是 viewDidLoad 在此应用程序中仅被调用一次。因为您只在当前日期为 11:30 时安排本地通知,所以永远不会调用它,因为您还将 fireDate 设置为相同的确切时间。

问题是,本地通知会在事件发生之前安排好。实际上,您应该将 dateTime 设置为所需的计划时间,然后设置本地通知。

例如:

- (void)viewDidLoad {

[super viewDidLoad];

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:2016];
[dateComponents setMonth:6];
[dateComponents setDay:23];
[dateComponents setHour:23];
[dateComponents setMinute:30];

dateTime = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = dateTime;
localNotification.alertBody = [NSString stringWithFormat:@"It's 11:30 PM 23th June!"];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.applicationIconBadgeNumber = 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

根据您上传的代码,您似乎要从 datePicker 中获取日期,因此最终,您会将 UILocalNotification 部分移动到按钮方法并使用 [ datePicker 日期] 作为 fireDate。也就是说,您可以忽略 dateComponents 部分。

关于ios - Objective-C:UILocalNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38001266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com