【问题标题】:Local notification repeatInterval本地通知repeatInterval
【发布时间】:2014-08-06 01:42:55
【问题描述】:

我的应用程序是关于显示学生时间表的,我想添加一个本地通知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];
}
    }

因此,我需要在这方面提供更多指导,并就如何实现这一目标提出一些建议。

【问题讨论】:

  • 您面临的问题是什么?没有通知或补货问题?
  • 问题是当我打开我的应用程序时出现故障

标签: ios notifications uilocalnotification


【解决方案1】:

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

在 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);
}

【讨论】:

  • 我的问题是当我打开开关时,我的应用程序崩溃并说 return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 。我该怎么办?
  • 看起来当您尝试添加本地通知应用程序时会崩溃,请您发布您的AppDelegate.m 文件的didFinishLaunching 方法代码。
  • appDelegate.m ` // 处理从通知启动 UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey]; if (localNotification) { NSLog(@"收到通知 %@", localNotification); } 返回是; }` 注意:我在 view controller.m 中添加了我的第一个代码
  • 你能帮帮我吗??
  • @Faisal:我需要查看您的AppDelegate.mViewController.m 文件或相关代码。你可以分享你的代码吗?
猜你喜欢
  • 2013-01-24
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多