-3

Is there a way to call a method continuously as long as the app is running in background or foreground.This method is used to check for the time and trigger notification according to the change in time.

here is the method

-(void)checkDate
{ 
   NSDate *today = [NSDate date];
   NSString *endTime =@"24:00";
   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   [dateFormatter setDateFormat:@"HH:mm"];
   NSString *currentTime = [dateFormatter stringFromDate:today];
   NSLog(@"############ Current time is  %@",currentTime);
   if ([currentTime isEqualToString: endTime]) {
      NSLog(@"notifiction Fired");
      [[NSNotificationCenter defaultCenter]postNotificationName:@"todayEnds" object:self];
   }
}

My requirement is post a NSNotification from a method irrespective to the apps state

vishnuvarthan
  • 492
  • 1
  • 6
  • 23

2 Answers2

2

As pointed out in the comments, NSTimer will only work if your app is currently in the foreground. Code execution for apps that are in the background is quite tricky on iOS.

There are several possibilities to achieve this, which I doubt will fit for your purpose here, examples are:

  • Core Location Update: If you are using Core Location in your app, your app can receive updates on when the GPS position of the device changes and gets the chance to perform some operations in the background based on the new GPS data
  • Voice Over IP: The app provides Voice-over-IP services. Apps with this key are automatically launched after system boot so that the app can reestablish VoIP services. Apps with this key are also allowed to play background audio. (from the Apple Docs)
  • Background fetches: With background fetches you can perform network requests on a regular basis, however you are still not able to perform the operations at points in time that you can precisely specify, you can rather tell iOS that you want to perform network requests in regular intervals and iOS will schedule the requests for you. Here is an excellent read on background fetches.

For more information check the Apple Docs on background execution!

For your case, I would recommend you to take a look at UILocalNotification. It's not 100% clear to me what exactly you are trying to achieve, but UILocalNotification might be the way to go for you, as it sounds like you want to notify the user based on different times... UILocalNotification works in the way that you can schedule a notification while the app is in the foreground (or background if you are using these background execution modes) which will notify the user at a specific point in time.

Hope it helps!

nburk
  • 22,409
  • 18
  • 87
  • 132
  • i dont want to notifify user anything i just want to post the notification when a day ends and that notification will have its selector method to do some actions. So i dont need a UILocalNotification. – vishnuvarthan Oct 13 '14 at 07:15
  • My requirement is simple.Post a NSNotification from a method irrespective to the apps state. – vishnuvarthan Oct 13 '14 at 07:17
  • 1
    As I said, you should take a look at the docs on background execution,... but if your requirement is to perform background operations at a specific point in time, I can already tell you that this won't work. You will have to find a way to reach your goal given the limited background execution possibilities Apple gives you. – nburk Oct 13 '14 at 07:24
0

My requirement is simple.Post a NSNotification from a method irrespective to the apps state.

That's not possible in iOS. As described by nburk there are few exceptions to the general rule that apps can't run in the background. You have to use one of those exceptions or find a different way to achieve what you want.

By the way: Your code to detect a point in time looks fishy to say the least. Maybe you should describe what your goal really is so we can help you to find a viable solution.

Here's an answer that describes how to detect day changes (fire a notification at midnight). It includes a working Xcode project.

Community
  • 1
  • 1
Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • As others have pointed out, an `NSTimer` is the way to go to execute code at a given time. Your concern that it doesn't run in the background is not valid as this is true for every other code, including your polling. – Nikolai Ruhe Oct 13 '14 at 07:33