iOS本地闹钟提醒实现
2015-06-15 15:57
591 查看
有的时候我们在应用中需要做一个类似于闹钟提醒的功能,在每周的特定的几天中,在固定时间进行提醒。比如每周一到周五下午六点,需要吃药的时间就弹窗提醒:快去吃药!不要放弃治疗!
本地固定时间提醒,需要用到 UILocalNotification
如果只是添加一次,并且设置提醒的周期为 NSCalendarUnitWeekOfYear ,那么就是每周只提醒一次。所以如果要达到每周固定星期几提醒则需要添加提醒天的次数,比如周一到周五提醒那么就需要添加五次,每个提醒的间隔都为一个星期。这样就可以做到每周固定星期提醒了。
取消提醒比较简单,如果想取消所有的本地提醒:
取消特定的提醒:
本地固定时间提醒,需要用到 UILocalNotification
如果只是添加一次,并且设置提醒的周期为 NSCalendarUnitWeekOfYear ,那么就是每周只提醒一次。所以如果要达到每周固定星期几提醒则需要添加提醒天的次数,比如周一到周五提醒那么就需要添加五次,每个提醒的间隔都为一个星期。这样就可以做到每周固定星期提醒了。
for(id day in _weekArr) {// _weekArr 为周一到周日需要提醒的星期 UILocalNotification* beginLocalNotification = [[UILocalNotification alloc] init]; NSDictionary* beginInfo = [NSDictionary dictionaryWithObject:kLocalNoti forKey:@"beginNoti"]; beginLocalNotification.userInfo = beginInfo; beginLocalNotification.fireDate = [dateFormatter dateFromString:[NSString stringWithFormat:@"2015-06-%d %@:00",15+[day integerValue]-1,_beginTime]];// 在过去的某个星期内添加需要的本地提醒。并且设置提醒间隔为NSCalendarUnitWeekOfYear,如果添加在未来,那么提醒不会发生。 beginLocalNotification.alertBody = @"该吃药啦!"; beginLocalNotification.soundName = @"ping.caf"; beginLocalNotification.timeZone = [NSTimeZone defaultTimeZone]; beginLocalNotification.alertAction = @"不要忘记吃药哦!"; beginLocalNotification.repeatInterval =NSCalendarUnitWeekOfYear;// 每周循环提醒 [[UIApplication sharedApplication] scheduleLocalNotification:beginLocalNotification]; }
取消提醒比较简单,如果想取消所有的本地提醒:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
取消特定的提醒:
NSArray *narry=[[UIApplication sharedApplication] scheduledLocalNotifications]; NSUInteger acount=[narry count]; if (acount<1) { return false; } for (int i=0; i<acount; i++) { UILocalNotification *myUILocalNotification = [narry objectAtIndex:i]; NSDictionary *userInfo = myUILocalNotification.userInfo; NSString *obj = [userInfo objectForKey:@"beginNoti"]; if ([obj isEqualToString @"kLocalNoti "]) { [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification]; return true; } }
相关文章推荐
- IOS用正则验证手机号
- iOS--通过runtime完成归档,反归档
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!
- IOS - CoreData的使用
- IOS第三方之SVProgressHUD
- [iOS]判断当前时间是否在指定的时间段内
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
- iOS知识小集 第二期(2015.05.31)
- 修正CGBitmapContextCreate iOS8上的错误
- 关于iOS Push Notification响应的问题
- iOS中-Xcode Debug、Release、Archive、Profile、Analyze的概念和区别
- ios-时钟动画小案例
- iOS开发 纯代码适配技巧 baseViewController
- 浅谈 iOS 版本号
- iOS Crash文件的解析(一、符号化Crash文件)
- iOS Crash文件的解析(一、分析Crash文件)
- iOS 中的 CFBundleShortVersionString 与 CFBundleVersion
- IOS开发之利用通知(NSNotification)进行数据传递
- IOS第三方之MBProgressHUD
- iOS通过iTunes search检测版本更新,并提示用户更新!