本地通知repeatInterval

时间:2014-08-05 10:49:59

标签: ios notifications uilocalnotification

我的应用程序是关于显示学生的时间表,我想添加本地通知repeatInterval 我计划使用开关按钮。

我尝试了这段代码,现在还没有工作:

    -(IBAction)theSwitch:(id)sender{
if (switcher.on) {
    NSCalendar *gregCalendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponent = [gregCalendar components:NSYearCalendarUnit  | NSWeekCalendarUnit fromDate:[NSDate date]];

    [dateComponent setWeekday:3]; // For tuesday
    [dateComponent setHour:13];
    [dateComponent setMinute:41];

    NSDate *fireDate = [gregCalendar dateFromComponents:dateComponent];

    UILocalNotification *notification = [[UILocalNotification alloc]init];
    [notification setAlertBody:@"ALARM!"];
    [notification setFireDate:fireDate];
    notification.repeatInterval = NSWeekCalendarUnit;
    [notification setTimeZone:[NSTimeZone defaultTimeZone]];
    [[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
else  {UIApplication *app=[UIApplication sharedApplication];
[app scheduleLocalNotification:nil];
}
    }

所以我需要一些关于此的更多指导以及关于如何实现这一点的一些建议。

1 个答案:

答案 0 :(得分:0)

如果您无法获得本地通知,请查看以下代码。

在AppDelegate.m文件中添加代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Handle launching from a notification
UILocalNotification *localNotification =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotification) {
    NSLog(@"Recieved Notification %@", localNotification);
}
return YES;
}

添加处理本地通知的方法

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notification {
// Handle the notificaton when the app is running
NSLog(@"Recieved Notification %@", notification);
}
相关问题