我正在编写一个闹钟 iOS 应用程序。这是我第一次使用 UILocalNotification。我从日期选择器中获取日期。我已经格式化了日期以检查我的函数是否传递了正确的日期,它是。我检查了 UILocalNotification 所需的所有属性,我拥有了所有属性,但我的通知仍然不会触发。关于为什么的任何想法?谢谢您的帮助。
#import "BIDAlarmViewController.h"
@interface BIDAlarmViewController ()
@end
@implementation BIDAlarmViewController
@synthesize datePicker;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib
}
- (void)didReceiveMemoryWarning{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)setReminderUsingDateFromDatePicker: (id)sender{
[self scheduleNotificationForDate: datePicker.date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
NSString *formattedDateString = [dateFormatter stringFromDate:datePicker.date];
NSLog(@"Button Pressed.. date: %@", formattedDateString);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alarm activated"
message:@"Alarm has been set"
delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
-(void) scheduleNotificationForDate: (NSDate*)date {
UILocalNotification *alarm = [[UILocalNotification alloc] init];
if (alarm) {
alarm.fireDate = date;
alarm.timeZone = [NSTimeZone defaultTimeZone];
alarm.repeatInterval = 0;
alarm.soundName = @"alarmsound.caf";
alarm.alertBody = @"Test message...";
[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
}
}
@end