简体   繁体   中英

How to show a specific view controller even if user does not tap on the notification

I have 2 ViewControllers which will pop up depending on the local notification. How can I show these ViewControllers when user taps on the app icon directly instead of the notification ?

Is there any way to call

 - (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification

from

- (void)applicationDidBecomeActive:(UIApplication *)application 

?

You can just call that method directly by referencing your app delegate and giving it those parameters (you will have to create a dummy UILocalNotification).

However, this is weird.

What you should do is properly SEPARATE the code that shows the view controllers into its own function. Then you can call this function in either of the methods you specified above.

Wherever you schedule the local notification, add a key to NSUserDefaults specifying which VC to load.

UserDefaults.standard.setValue("name_of_vc_to_load", forKey: "vcToLoad")
UserDefaults.standard.synchronize()

Finally in

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

and in

func applicationDidBecomeActive(_ application: UIApplication) {

Check if the VC to load available and load the VC accordingly.

if let vcName : String = UserDefaults.standard.value(forKey: "vcToLoad") as? String {
        switch vcName {
        case "VCA" : 
            //load VCA
            break
        default:
            //load VCB
            break
        }
    }

You can either present a VC of your choice over the rootVC or you can replace the rootVC itself.

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