我想在 iOS 应用中设置闹钟。
我根据How to set an Alarm in iOS?尝试了这个。这是我的主要代码:
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
[localNotif setFireDate:self.datePicker.date];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
NSLog(@"fireDate %@", localNotif.fireDate);
NSLog(@"datepicker %@", self.datePicker.date);
[localNotif setRepeatInterval:0];
localNotif.alertBody = @"some text";
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.alertAction = @"...";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
我还发现:在 iOS8 ,
在iOS 8.0及更高版本中,您的应用程序必须注册用户 通知使用 - [UIApplication registerUserNotificationSettings:] 在能够安排和呈现UILocalNotifications之前
所以,我在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) {
UIUserNotificationType types = UIUserNotificationTypeBadge |
UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *mySettings =
[UIUserNotificationSettings settingsForTypes:types categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
}
令我失望的是,当火灾发生时,没有任何事情发生。我错过了其他重要的代码吗?
答案 0 :(得分:3)
本地通知仅在应用程序关闭时由iOS显示。如果应用程序已打开,您需要实现应用程序委托方法以接收本地通知详细信息并自行显示该事件。