简体   繁体   中英

iOS swift 3 reload tableview when open app from background

I implemented push notifications into my app. The user gets a push notification from the server whenever there is a new message for him.

When the user clicks on the push notification, it opens the app. I want my app to reload a tableview when this happens, showing the user the recent newsfeed.

Is it possible in Swift 3?

As I understand, you are interesting in case of app that was opened on your recent newsfeed ViewController, then is closed to background by the user. Later on while receiving push, user clicks on it and opens the same newsfeed controller.

You have to subscribe to UIApplicationWillEnterForeground notification in your UIViewController like

NotificationCenter.default.addObserver(self, selector: #selector(yourMethod), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)

and implement yourMethod like

func yourMethod() {

    // send request to update newsfeed
    // then update tableView
    // tableView.reloadData()
}

Don't forget unsubscribe

deinit {
    NotificationCenter.default.removeObserver(self)
}

It's the simplest solution and I described the simplest case. There are some more complicated cases I didn't describe, for example

The app is closed and you have to make hierarchy based on the push received in willFinishLaunchingWithOptions .

The app is opened on the other page, in this case you have to reconfigure viewControllers hierarchy to show required ViewController. In this case you have to pass some data to required ViewController from the pressed Push Notification.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM