jose920405
jose920405

Reputation: 8049

performSegue error when come from appDelegate

My case is something special, since this works me correctly if the application is opened in normal mode.

This is when I try to do so after receiving a push notification.

inside didReceive notification:

func application(_ application: UIApplication, didReceive notification: UILocalNotification) {

i'm calling a function from my initial view controller(HOME)

That is just the view that is displayed when the application opens when you click on the notification

let homeVC : HomeViewController! = HomeViewController()
homeVC.goAdvertisement(id, data: dataInfo)

Within goAdvertisement you can find:

self.performSegue(withIdentifier: "mySegue", sender: itemData)

And that is where the error occurs.

libc++abi.dylib: terminating with uncaught exception of type NSException

enter image description here

At first I thought that it could be that it was giving time to load the view so that I could execute the performSegue, and try to give it a delay using:

func delay(delay:Double, closure:@escaping ()->()) {
        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
            closure()
        }
    }

self.delay(delay: 4, closure: {
    let homeVC : HomeViewController! = HomeViewController()
    homeVC.goAdvertisement(id, hardwareAd: dataInfo)
}

But it does not work

Another idea that I had, was to leave aside the performSegue and try to use the present.

let vc = self.storyboard?.instantiateViewController(withIdentifier: "myVcId") as! MyController
vc.data = itemData
self.present(vc, animated: true, completion: nil)

But this did not work either (The crash points to instantiateViewController line)

Any help will be greatly appreciated, thanks.

UPDATE

Following APK APPS answer. Finally found a way.

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeVC: HomeViewController = storyboard.instantiateViewController(withIdentifier: "homeView") as! HomeViewController

let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(homeVC, animated: false);

Upvotes: 0

Views: 512

Answers (1)

Pushpendra
Pushpendra

Reputation: 1519

From the AppDelegate class you can not perform segue. So this line will never work from your AppDelegate class.

self.performSegue(withIdentifier: "mySegue", sender: itemData)

You Need to simple

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let scheduleController = storyboard.instantiateViewController(withIdentifier: "yourIdentiFier")
self.window!.rootViewController = scheduleController
self.window!.makeKeyAndVisible()

You can provide Identifier to you controller from your Main.storyboard Follow these steps.

  • Open Main StoryBoard
  • Click Your View Which you want to open
  • Select Yellow/ Left button from top of that controller
  • Now Open your Utility box which would be in the Right side
  • Now Click on the Show the Identical Inspector
  • Now here you can give your Identity as StoryBoard Id

Upvotes: 1

Related Questions