Advertisement
Guest User

Untitled

a guest
Sep 18th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. [self scheduleCheckTimer];
  4. }
  5. -(void)applicationWillEnterForeground:(UIApplication *)application
  6. {
  7. if([NSDate date] compare: self.checkDate] == NSOrderedDescending) {
  8. [self checkTimerTimeout];
  9. } else {
  10. [self scheduleCheckTimer];
  11. }
  12. }
  13.  
  14. - (void)applicationDidEnterBackground:(UIApplication *)application
  15. {
  16. [self.timer invalidate];
  17. }
  18.  
  19. -(void) scheduleCheckTimer
  20. {
  21. [self.timer invalidate];
  22.  
  23. self.checkDate = [self nextCheckDate];
  24. self.timer = [NSTimer scheduledTimerWithTimeInterval: [self.checkDate timeIntervalSinceNow] target: self selector: @selector(checkTimerTimeout) userInfo: nil repeats: NO];
  25. }
  26.  
  27. -(void) checkTimerTimeout
  28. {
  29. [[UIApplication sharedApplication] cancelAllLocalNotifications];
  30. [self scheduleCheckTimer];
  31. }
  32.  
  33. -(NSDate*) nextCheckDate {
  34.  
  35. NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
  36.  
  37. NSDateComponents* dateComponents = [gregorian components: kCFCalendarUnitEra |
  38. kCFCalendarUnitYear |
  39. kCFCalendarUnitMonth |
  40. kCFCalendarUnitDay |
  41. kCFCalendarUnitHour |
  42. kCFCalendarUnitMinute |
  43. kCFCalendarUnitSecond fromDate: [NSDate date]];
  44.  
  45.  
  46. dateComponents.hour = 0;
  47. dateComponents.minute = 0;
  48. dateComponents.second = 0;
  49. dateComponents.day = dateComponents.day + 1;
  50.  
  51. return [gregorian dateFromComponents: dateComponents];
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement