Action after back background application

1

I have a webview on android that always checks if there is internet coming back from the background checking if the connection status has been changed in case the offline application is sent to a "reconnect and try again" screen using the code below:

protected void onResume() {
        super.onResume();
        mWebView.onResume();
        if (isConnected(getApplicationContext())){
        } else {
            Intent i = new Intent(MainActivity.this, off.class);
            startActivity(i);
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            finish();
        }
    }

So far so good I've made a version for ios of this webview but I could not reproduce this check when the app returns from the background, how do I reproduce this "onresume" in ios swift? (the code that checks the connection state I already have)

    
asked by anonymous 31.10.2018 / 14:15

2 answers

2

Complementing Rodrigo's response, although viewDidAppear is a possibility, this method is not called when the app returns from the background. viewDidAppear is actually called when the view of the ViewController has become visible. This does not always happen when the app comes to the foreground. If the intent is to really understand when the app gets focus, instead of depending on the UIViewController lifecycle (eg viewWillAppear, viewDidAppear), the correct way is to use the application lifecycle methods ( UIApplication ). In this case you can use applicationWillEnterForeground : or applicationDidBecomeActive :. They are called in AppDelegate when events happen.

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
    }
}

If your code makes the most sense for these callbacks to be triggered in a specific class, such as a UIViewController for example, you can use the notification system.

NotificationCenter.default.addObserver(forName: UIApplication.didBecomeActiveNotification, object: nil, queue: nil) { _ in
    //...
}
    
31.10.2018 / 15:25
2

According to the life cycle of iOS, the similar of onResume Android% would be viewDidAppear() :

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //sua lógica
}

In the documentation it also says about viewWillAppear() , which would be one step before viewDidAppear() . See which one suits you best!

    
31.10.2018 / 14:51