因此,我有一个包含重复间隔本地通知的应用程序,我想添加一个在睡眠期间暂停通知的功能。
到目前为止,我已经为用户创建了两个日期选择器,以指定他们想要停止重复间隔的时间以及自动重新启动的时间。我还为他们添加了一个 uiswitch 来激活睡眠模式或忽略该功能。
现在,我将如何让我的主 uipickerview -(他们从这里选择通知) - 监听 uiswitch(如果它已打开),然后它会在来自我的第一个日期选择器的时间暂停通知,并在来自第二个日期选择器的时间?
我已经设置了我的 datepickers 和我的 uiswitch 但不知道如何用我的 uipickerview 实现它。它应该在 DidSelectRow 的方法下吗?或者 appdelegate 中的方法(如 DidEnterBackground)?
请询问您是否需要更多信息或代码来理解这个想法并帮助我。谢谢。
ADD ON:
这是我为日期选择器准备的代码,但是,我只是缺少将其正确添加到我的选择器视图的连接。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter.timeStyle=NSDateFormatterShortStyle;
dateFormatter.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString=[dateFormatter stringFromDate:startTime.date];
NSLog(@"Start time is %@",dateTimeString);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc]init];
dateFormatter2.timeZone=[NSTimeZone defaultTimeZone];
dateFormatter2.timeStyle=NSDateFormatterShortStyle;
dateFormatter2.dateStyle=NSDateFormatterShortStyle;
NSString *dateTimeString2=[dateFormatter2 stringFromDate:endTime.date];
NSLog(@"End time is %@",dateTimeString2);
我还有这段代码可以在不同时间之间进行比较:
if ([[NSDate date] isEqualToDate:startTime.date]) {
NSLog(@"currentDate is equal to startTime");
}
if ([[NSDate date] isEqualToDate:endTime.date]) {
NSLog(@"currentDate is equal to endTime");
}
iOS 不支持在两个特定日期/时间之间发布本地重复通知。此外,与基于地理的通知不同,应用程序不在前台时不会收到触发通知的警报。因此,我们需要通过在用户不睡觉时创建许多单独的通知来解决这个问题。
请按照以下步骤创建基本解决方案:
-
将您的控件连接到视图控制器标头中的 IBOutlet:
SomeViewController.h:
@interface SomeViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISwitch *sleepToggleSwitch;
@property (weak, nonatomic) IBOutlet UIDatePicker *notificationIgnoreStartTime;
@property (weak, nonatomic) IBOutlet UIDatePicker *notificationIgnoreEndTime;
@property (weak, nonatomic) IBOutlet UIPickerView *notificationTypePickerView;
@end
-
创建 IBAction 方法并连接视图控制器实现文件中的每个控件(两个 UIDatePicker、一个 UISwitch 和一个 UIPickerView)。每个方法都应该调用私有方法startUserOptionInteractionTimer
.
SomeViewController.m:
- (IBAction)noNotificationPeriodStartDateChanged:(id)sender
{
[self startUserOptionInteractionTimer];
}
- (IBAction)noNotificationPeriodEndDateChanged:(id)sender
{
[self startUserOptionInteractionTimer];
}
- (IBAction)sleepToggleSwitchToggled:(id)sender
{
[self startUserOptionInteractionTimer];
}
- (IBAction)notificationTypeChanged:(id)sender
{
[self startUserOptionInteractionTimer];
}
-
In the startUserOptionInteractionTimer
私有方法,我们(重新)启动一个 NSTimer。我们在这里使用计时器,这样如果用户更改日期或快速切换开关(他们很可能会这样做),您就不会连续快速地拆卸和设置通知。 (NSTimer 属性userOptionInteractionTimer
应在实现文件的接口延续中声明)。
SomeViewController.m:
- (void)startUserOptionInteractionTimer
{
// Remove any existing timer
[self.userOptionInteractionTimer invalidate];
self.userOptionInteractionTimer = [NSTimer scheduledTimerWithTimeInterval:4.f
target:self
selector:@selector(setupNotifications)
userInfo:nil
repeats:NO];
}
-
创建另一个私有方法来拆除预先存在的通知并设置新的通知。
此处设置通知取决于您想要通知用户的时间和频率。假设您希望每小时通知一次用户,并且用户启用了睡眠功能,那么您将每小时设置 14-18 条通知(取决于用户睡眠的时间),每天重复一次。
SomeViewController.m:
- (void)setupNotifications
{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// Read the notification type from the notification type picker view
NSInteger row = [self.notificationTypePickerView selectedRowInComponent:0]; // Assumes there is only one component in the picker view.
NSString *notificationType = [self.notificationList objectAtIndex:row]; // Where notificationList is the array storing the list of notification strings that appear in the picker view.
// If the user has turned the sleep feature on (via the UISwitch):
if (self.sleepToggleSwitch.on) {
// Set the first notification to start after the user selected 'noNotificationPeriodEndDate' and repeat daily.
// Add 1 hour to the notification date
// Do while the notification date < the user selected 'noNotificationPeriodStartDate' ...
// Create the notification and set to repeat daily
// Add 1 hour to the notification date
// Loop
} else {
// Set up 24 repeating daily notifications each one hour apart.
}
}
请记住,单个应用程序最多只能创建 64 个通知(重复的通知算作一个),因此,如果您希望通知在几天或几周内的不同时间触发,您可能需要重新考虑您的设计小的。
-
在 NSUserDefaults 中加载并存储用户选择的首选项:
SomeViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
// Load the user defaults
NSDate *sleepStartDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"SleepStartDate"];
self.notificationIgnoreStartTime.date = sleepStartDate ? sleepStartDate : [NSDate date];
NSDate *sleepEndDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"SleepEndDate"];
self.notificationIgnoreEndTime.date = sleepEndDate ? sleepEndDate : [NSDate date];
self.sleepToggleSwitch.on = [[NSUserDefaults standardUserDefaults] boolForKey:@"SleepEnabled"];
// Watch for when the app leaves the foreground
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive)
name:UIApplicationWillResignActiveNotification object:nil];
}
- (void)applicationWillResignActive
{
// If the timer is still waiting, fire it so the notifications are setup correctly before the app enters the background.
if (self.userOptionInteractionTimer.isValid)
[self.userOptionInteractionTimer fire];
// Store the user's selections in NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:self.notificationIgnoreStartTime.date forKey:@"SleepStartDate"];
[[NSUserDefaults standardUserDefaults] setObject:self.notificationIgnoreEndTime.date forKey:@"SleepEndDate"];
[[NSUserDefaults standardUserDefaults] setBool:self.sleepToggleSwitch.on forKey:@"SleepEnabled"];
}
另外,请注意上面的内容,如果应用程序即将进入后台(即离开前台)并且计时器仍在滴答作响,我们会强制它触发,以便在计时器被操作系统终止之前设置通知。
请记住连接所有日期选择器视图的委托 IBActions 和所有 IBActions 以及选择器视图的委托和数据源。另请记住设置委托和数据源方法,以便填充选择器视图。
就是这样!
上述设计将确保通知在正确的时间触发,无论应用程序是否处于前台、后台或终止状态。 (但是,如果收到通知时应用程序位于前台,则用户将不会收到通知。相反,application:didReceiveLocalNotification:
appDelegate 上将被调用)。
显然,上面的代码还没有“执行就绪”,但您应该能够填补空白。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)