UIApplicationDidBecomeActiveNotification is called any time? - iphone

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.

Related

applicationDidBecomeActive getting called twice

My app delegate method applicationDidBecomeActive: is getting called twice for the first time launch of the application. I have some portion of code which I want to execute only once & that I have put into applicationDidBecomeActive:
What should I do?
I got the issue. I am using Location Services. When launching for the first time after I tap on "OK" on the location services alert, my applicationDidBecomeActive gets called one more time which is the normal iOS behavior.
If you want to call your code only once when app becomes active, try calling it from two methods.
didFinishLaunchingWithOptions
applicationWillEnterForeground
instead of calling it only from applicationDidBecomeActive.
This is because of location or push notification alert.
After the native location/push notification has been dismissed, applicationDidBecomeActive will be called.
I don't know if this will help, but I just had the same issue with a totally simple app that doesn't use Location Services, and I found out it's an illusion. Look at the logging messages I got:
2012-12-22 10:47:45.329 Bizarro[10416:907] start applicationDidBecomeActive:
2012-12-22 10:47:45.333 Bizarro[10416:907] end applicationDidBecomeActive:
2012-12-22 10:47:45.329 Bizarro[10416:907] start applicationDidBecomeActive:
2012-12-22 10:47:45.333 Bizarro[10416:907] end applicationDidBecomeActive:
Look closely. Look at the times. The first and third messages have the same time. The second and fourth messages have the same time. They are the same messages! It's an Xcode bug; it has nothing to do with my code. Xcode is reporting the same log messages twice.
In my case, I was able to prevent this by turning off all Behaviors for Running -> Generates Output.
What about:
Increment on applicationDidBecomeActive
Decrement on callback events of permissions requests or other alerts that trigger another applicationDidBecomeActive when closed.
With Xcode 6 there's a new reason this can happen: when you launch an app in a resizable simulator, applicationDidBecomeActive: will get called twice.
It launches the app with the default size class, and then applies the size you were last using—even if you were using the defaults. Any time a change in size class is applied, applicationDidBecomeActive: gets called.
When app launches first time
it calls sequentially,
didFinishLaunchingWithOptions
applicationDidBecomeActive (Twice)
When we open the Control Center it calls only,
applicationDidBecomeActive
When app come from background to foreground it calls sequentially
applicationWillEnterForeground
applicationDidBecomeActive
I'm currently seeing double notifications.
It's happening because my AppDelegate's init code is being called twice.
It's being called once when the main() does [[NSBundle mainBundle] loadNibNamed:#"MainMenu" owner:application topLevelObjects:&tl] (ie, when the .XIB file is loaded), becaues the .XIB file is setting up FirstResponder to my custom AppDelegate, and then it's being called again when main() calls [[myAppDelegate alloc] init].
The init code is what does the addObserver calls, so two observers are being set up for each notification I care about, which is why my notifications get called twice.
I'm a newbie OS X programmer, so I'm not yet sure of the best way to resolve these two, but wanted to post it here in case it's of help to others, another place to look.
Have you possibly created an instance of your class in Interface Builder AND in your AppDelegate code, perhaps?
If you have code you want called only once when the app starts up, then use
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
instead.
Otherwise, applicationDidBecomeActive will be called whenever your app becomes active again, so that doesn't just mean twice as in your case, but every time the user switches back to your app after switching to another.
If you use RxSwift, then you can just throttle the application event, so it doesn't call twice in the same second:
NotificationCenter.default.rx.notification(Notification.Name.UIApplicationDidBecomeActive)
.throttle(1, scheduler: MainScheduler.instance)
.subscribe { (event) in
self.appEnteredFromBackground()
}.disposed(by: disposeBag)
private func appEnteredFromBackground() {
Analytics.trackPageView(withScreen: .home)
dataStore.checkIfAllowingRides()
}
I put this code in my actual view controller where the logic is supposed to happen on ApplicationDidBecomeActive.
I just check at top of applicationDidBecomeActive: if the request was really sent (I made a function for this, checking status), if so I return already.
The second time in applicationDidBecomeActive:, the function reads the status as Deny or Allowed, because this is after the User answered the Alert.

viewWillAppear and UIApplicationDidBecomeActiveNotification

I am using the UIApplicationDidBecomeActiveNotification to refresh my tableview when the app becomes active. My problem is that in my ViewWillAppear, I am also calling a method to refresh this table's data.
This is causing the table to be refreshed twice upon application launch. How can I get one of them not to fire when the app is initially launched? Refreshing the table has some intensive processing of network and local data.. so I would really like o only perform this action once.
Thanks.
You need to use UIApplicationWillEnterForegroundNotification instead of UIApplicationDidBecomeActiveNotification.
The latter is posted every time your app becomes active (initial launch, back to app after call/sms interruption, etc.). But the former is posted only in case of wake up from background. Note that in this case viewWillAppear is not called (as it should seems to be at the first sight).
One way to do it would be with a flag, that you can set up in didFinishLaunching, since that is only executed once per launch.

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.

Home button press , Which AppDelegate method should i use for scheduling a local notification

I would like to schedule a local notification as soon as the user hits the home button.
Which App delegate method should I use in this case :
applicationWillResignActive
applicationDidEnterBackground
applicationWillTerminate
Well I guess I shouldn't use the third one, but what is the difference between the first two ?
Is there any way to distinguish getting interrupted by a phone call/other notification and actually pressing the home button ?
Thanks in advance.
To schedule local notification you shold use applicationDidEnterBackground instead of using applicationWillResignActive because applicationWillResignActive call every time when app get some specific interruption line phone call, sms. You want to schedule notification when user press home button and in this case applicationDidEnterBackground is the appropriate place to do this.
One thing that should be remember before using applicationDidEnterBackground is that this delegate has approximately five seconds to perform any task, if any task in this delegate will take more time then os will terminate your app. You can also request for additional time for execution by using beginBackgroundTaskWithExpirationHandler and then use a secondary thread to perform a task. For more detail about application delegates follow the links -
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html
http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/
You should use applicationDidEnterBackground.
applicationWillResignActive gets called anytime your app is interrupted such as a phone call or SMS message. In this case if the user ignores these then your app will keep running in the foreground.
applicationDidEnterBackground only gets called when your app actually goes to the background.
You should do this in applicationDidEnterBackground:
applicationWillTerminate will not be
called when the user hits the home
button. With app switching this is
only sent when the user explicitly
quits the app or possibly in low
memory situations.
applicationWillResignActive is
additionally called when the app is
briefly interrupted, say by an SMS or
phone call alert. (Though if the user
then switches to Messages or Phone
app your app will eventually get a
applicationDidEnterBackground
message).
So it sounds like you're specifically interested in the point when the user taps the home button and the app goes to the background. applicationDidEnterBackground is the place.
You could also always schedule the local notification and only respond to it if the app isn't running when it occurs. Not better necessarily, just an option to consider.

How to respond to any kind of interruption?

My app is playing a pretty complex animation. It's like a flipbook.
What I do is: I have a huge loop with selectors, and after every delayed call the next one is called.
Now someone calls the user and the device suddenly shows up this fat green status bar and maybe some big pick-up-the-phone-call overlay. Or: The alarm clock rings, and a big alert sheet appears in front of just about everything.
It would be great to just pause the whole animation in case of ANY interruption. Probably I've also missed like 5 more possible interruptions.
How are you doing that? How do you get notified for all interruptions and then call one single -stopEverything method?
Whenever the app becomes inactive, the UIApplicationWillResignActiveNotification local notification will be posted. In the opposite suituation, the UIApplicationDidBecomeActiveNotification notification will be posted.
Your animation logic can listen to this and respond appropriately. There is an +setAnimationsEnabled: method to kill all current and future animations, but there isn't a documented "global pause" method.
Depending on the animation, you might be better off using CoreAnimation directly by using a single CAKeyframeAnimation on the view's -layer rather than having one animation's completion selector start another animation.