Notify app when iPad date time settings changed - iphone

I would like to get notified when ipad's date-time settings is changed. Is there any way for that?.
I am using NSDateFormatter to find whether iPad/iphone time mode is 12 or 24 hr format. NSDateFormatter is seems to take lots of time( seen in time profiling). So I would like to check use it only when settings is changed.

You can do it using two ways:
Implement - (void)applicationSignificantTimeChange:(UIApplication *)application in your app delegate.
Add a observer for UIApplicationSignificantTimeChangeNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(timeChanged:) name:UIApplicationSignificantTimeChangeNotification object:nil];
applicationSignificantTimeChange:
Tells the delegate when there is a significant change in the time.
- (void)applicationSignificantTimeChange:(UIApplication *)application
Parameters
application
The delegating application object.
Discussion
Examples of significant time changes include the arrival of midnight,
an update of the time by a carrier, and the change to daylight savings
time. The delegate can implement this method to adjust any object of
the application that displays time or is sensitive to time changes.
Prior to calling this method, the application also posts a
UIApplicationSignificantTimeChangeNotification notification to give
interested objects a chance to respond to the change.
If your application is currently suspended, this message is queued
until your application returns to the foreground, at which point it is
delivered. If multiple time changes occur, only the most recent one is
delivered. Availability
Available in iOS 2.0 and later.
Declared In UIApplication.h
For more check UIApplicationDelegate_Protocol

How about adding an observer for NSCurrentLocaleDidChangeNotification ? Per Apple, "Re-create any cached date and number formatter objects whenever the current locale information changes."

You may also want to listen to this notification:
NSSystemTimeZoneDidChangeNotification

Related

IOS Background task with specific time interval

I wanted to call a service with some interval like 24 hour. So if my app is in background also at that time also it should call for that service. I am supporting location updates, so my app will run in background. But i want to know how can i execute some task with time interval without informing user.
Thanks
The best thing I can think of would be to integrate with Parse. There you can set up cloud code that runs in the background at a specific time. You can also schedule push notifications as needed. Otherwise, this is a fairly difficult situation.
You can use NSThread methods to call your service in background after some time interval.
You can follow this link1 or link2 for understanding of thread management.
Hope this will help..
You can use this code in your app delegate didfinishlaunching method
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationDidTimeout:) name:kApplicationDidTimeoutNotification object:nil];
and set timer according to that to perform your task at a particular time

UIApplicationDidBecomeActiveNotification is called any time?

I use UIApplicationDidBecomeActiveNotification/UIApplicationWillResignActiveNotification pair to compute app running time. in callback of UIApplicationDidBecomeActiveNotification, I record the startTime, and in callback of UIApplicationWillResignActiveNotification, I record endTime.
in most case, I find the running time is correct. but there are some special case in server's log, I find the running time is strange. like the end time is less than start time, or the end time is much more than the start time. so I suspect the UIApplicationDidBecomeActiveNotification is not called in some times. if some one meet such kind of case, and give me some suggestion.
If you are listening for UIApplicationDidBecomeActiveNotification in a view controller, then viewDidLoad is not called until after UIApplicationDidBecomeActiveNotification is posted. So if you register for the notification in viewDidLoad, you'll miss the first notification, which happened before the view gets loaded. So every time your application launches, you'll miss one such notification. All subsequent notifications will be caught though, including returning from background etc.
UIApplicationDidBecomeActiveNotification will be called everytime your application is coming from background to forgnound.means everytime your application is becoming active from a nonactive state.I think that is the reason for the changing time.

iPhone - Get user interaction event and automatic logout

In my iPhone app I want to logout the user if nothing happens till about 2 minutes (e.g. the user puts down the phone). Does anybody has such issue? What is the best way to implement this feature? I think I save the date of last event to NSUserDefaults, then on the next event first I check the current date. If the difference is larger than 2 minutes go to login screen, else refresh the stored date. But how can I get the touch event generally?
Thanks, madik
There's a method in UIApplicationDelegate for that:
- (void)applicationWillResignActive:(UIApplication *)application
{
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
Note that it also will be called when the app is going to background state. That will help you store the data whenever the app is going to inactive state. If you want to check if a certain amount of time has passed, you will have to use a NSTimer and store the last touch event. I think it cannot be done because you can't intercept all the touch events (Maybe it's over an object managed by the system. The status bar is an example). I guess is better to let the system to manage all the activity/inactivity stuff and store your data when necessary.
EDIT: I didn't understand what you mean the first time. Check this accepted answer, it accomplish what you need. Basically you have to subclass UIApplication and override sendEvent method.
'NSTimer'
When you say "how can I get the touch event generally?", if you mean how can you tell if the user is idle or not, you'll have to set up some system to gather all touch events at a higher level in your app. You could update the last touch time you mentioned in NSUserDefaults but that may be inefficient during the run of the app, so you could just post the touch event to your main app delegate and have it save the time of last touch. Which would also be where you could set up the 2 minute timer.
Something like:
- (void) someAppDelegateMethodThatYouCallForAnyUserEvent
{
[self.idleTimer invalidate];
self.lastEvent = [NSDate now];
self.idleTimer = [NSTimer scheduledTimerWithTimeInterval:120 target:self selector:#selector(logoutAndGotoLogin) userInfo:nil repeats:NO];
...
}
You'll also have to do some cleanup in your app delegate methods when the app goes to background etc if you support that behavior.

MPMediaLibraryDidChangeNotification called twice?

My app uses the iPodMusicPlayer and, when suspended, the user might go out and make changes in Apple's Music App, for example creating or modifying a Playlist, then return to my App.
I receive the expected MPMediaLibraryDidChangeNotification, which is fine and I deal with it updating my references etc., but I receive a second MPMediaLibraryDidChangeNotification about 2 minutes later which I really don't need.
Any ideas on avoiding this second notification?
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(notification_iPodLibraryDidChange:) name: MPMediaLibraryDidChangeNotification object:nil];
[[MPMediaLibrary defaultMediaLibrary] beginGeneratingLibraryChangeNotifications];
The notification can be called multiple times depending on what's going on. For example, if you add an album to your phone with 12 songs in it, the notification gets called 12 times. Basically it gets called every time the library changes and not just when the sync has finished (at least on iOS 5.1, not sure about older iOS versions).
Where are you adding he observer? For example, if you add in the viewWillAppear and only remove observers in dealloc, you may have multiple observers which is causing a problem. At least, when I encountered a problem like this it was because I had inadvertently added a second observer without removing all the previous.
2 minutes seems like a long lag time (mine was a few seconds), but still may be worth checking out.
Probably the best way to avoid multiple launches of update procedures after multiple notifications is to set a timer and wait some seconds before performing the actual update.
if( !self.lastModifiedDate ) self.lastModifiedDate = [[NSDate alloc] init];
if( [self.lastModifiedDate compare:[[MPMediaLibrary defaultMediaLibrary] lastModifiedDate]] == NSOrderedSame ) return;
self.lastModifiedDate = [[MPMediaLibrary defaultMediaLibrary] lastModifiedDate];
The above lines in my notification handler method deal with the extra call. Still no idea why I'm getting it.
Remove the beginGeneratingLibraryChangeNotifications command, and it will fix it. :) You just get every notification for changed, one from the notification center, and one from the default library.

How can I dismiss UILocalNotifications that appear when my app is in the background?

I'm working on an iPhone app that needs to remind a user to check in at regular intervals using UILocalNotifications. If they don't check in for a few hours, they may be reminded a couple times, but I just want to show the latest notification.
Now, if the app is open, I get a callback to didReceiveLocalNotification:(UILocalNotification *)notification, and I can keep track of whether there's a notification showing.
If the app is not running and the user clicks the -action- button, I get a callback to
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
so I can dismiss any old notifications. However, if they click 'cancel', they have to click 'cancel' on a bunch of layered notifications, because as far as I can tell I don't get a callback (how could I, if the app isn't launched) and it doesn't seem like there's a flag or something when creating the UILocalNotification to have newer reminders from an app automatically dismiss other ones.
If the app is in the background but running, it's worse - first, I don't get any sort of callback there if the user click's cancel, so I have the same problem - the user has to click cancel a bunch of times. Second, if they click the action, I get a call to ApplicationDidBecomeActive, but there's no distinguishing between that and when the user just switches back and forth; I can dismiss and reschedule them here, but it doesn't seem to work perfectly, sometimes a few pop up before they're dismissed.
Any suggestions? If there were a way for the notifications to expire automatically that would be great too. I've looked online a bit and haven't found much help, but it seems like a big oversight, so hopefully there's some way to handle this gracefully.
Thanks.
You won't be able to get any callback when the user "cancel"s as you pointed out.
Is it possible to just remind the user once in your case? Only schedule one notification at a time and renew it on app launch/resume.
I have not tried the following yet, but I believe this can be a work around for your case. Use CLLocationManager's startMonitoringSignificantLocationChanges
Every time the device's location changes significantly, your app will be launched in the background with options passed to locationManager:didUpdateLocations: and you can probably schedule a UILocalNotification from there!