0

I have some confusion about the above two app delegation method: I read this link, and it shows that the applicationWillEnterForeground will gets called before applicationDIdBecomeActive gets called. I am not sure about the before means.

If I have a social app: in my applicationWillEnterForeground function, I will check if the there is a current user session; in my applicationDIdBecomeActive, I will reload the content on timelineViewController: Thus, if there is no current user session, the timeline cannot be shown. Suppose a user enter the app from background with no current user session, the applicationWillEnterForeground will show a login page to indicate that there is no user, however, will the next-get-called applicationDIdBecomeActive return back to timeline which are not supposed to show?

If I don't want to modify code in my other viewcontroller, or check user session in applicationDIdBecomeActive. Is there any code I can add in applicationWillEnterForeground function to prevent applicationDIdBecomeActive function running?

Another question: I notice that for some app like Facebook, if I press the home button, turning it to background, but immediately turn it back to screen, the app doesn't show a lot of changes; however, if I let it stay in background for like an hour, it will "freeze" for a while (looks like it is refreshing) when I turn it back to foreground. How does the delegation method design to realize that? Is the system decides which delegation method(the above two) to call basing on the time that the app stayed in background?

BTW, I am using swift as the main programming language of iOS

Community
  • 1
  • 1

2 Answers2

0

You can't prevent applicationDidBecomeActive from getting called.

You could set an instance variable in applicationWillEnterForeground which is read later to determine the flow your application uses.

Aaron Brager
  • 65,323
  • 19
  • 161
  • 287
  • Another question: I notice that for some app like Facebook, if I press the home button, turning it to background, but immediately turn it back to screen, the app doesn't show a lot of changes; however, if I let it stay background for like an hour, it will "froze" for a while (looks like it is refreshing) when I turn it back to foreground. How does the delegation method design to realize that? Is the system decides which delegation method to call basing on the time that the app stayed in background? – Zhehao Victor Zhou Feb 10 '15 at 01:08
0

To refresh or initiate VC your app after certain interval like Facebook you can do as follows:

In applicationDidEnterBackground

let app = UserDefaults.standard
    let date = Date()
    app.set(date, forKey: "activeDate")

In applicationWillEnterForeground

let user = UserDefaults.standard
    var interval = 0
    if let date = user.object(forKey: "activeDate") as? Date {
        interval = Int(Date().timeIntervalSince(date)); print ( "AppDeligate: Inactive interval: \(interval)")
    }
if interval > 7200 { // 2hr
// Initiate VC OR Refresh data here
}

if you check time interval in applicationDidBecomeActive, then app get initiate or refresh data when yo view notification from swiping down or swipe up for control center. so applicationWillEnterForeground is the best place to do so.

Shrestha Ashesh
  • 1,769
  • 2
  • 13
  • 12