0

I am using push notification in my app and performing segue on click of notification but the problem is if the app is on some view and notification comes of that same view, then segue is performed on same view and same view comes on that view. I don't want this to happen if the view is same then it should not perform segue. Here is my code

 if application.applicationState == UIApplicationState.background{
        let moduleName = userInfo.value(forKey: "click_action") as! String
        let timestamp = Int64(date.timeIntervalSince1970)
        let description = userInfo.value(forKey: "body") as! String
        let title = userInfo.value(forKey: "title") as! String

        let entity = NSEntityDescription.entity(forEntityName: "Notifications", in: DataBaseController.persistentContainer.viewContext)

        let managedObj=NSManagedObject(entity: entity!, insertInto: DataBaseController.persistentContainer.viewContext)

        managedObj.setValue(moduleName, forKey: "click")
        managedObj.setValue(description, forKey: "body")
        managedObj.setValue(timestamp, forKey: "timestamp")
        managedObj.setValue(title, forKey: "title")

        DataBaseController.saveContext()


        if (homeVC != nil){

            homeVC?.performSegue(withIdentifier: moduleName, sender: homeVC)

        }

    }

}

Methods In homeView Controller

  //MARK:- Navigation Methods
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    let backItem = UIBarButtonItem()
    backItem.title = "Home"
    navigationItem.backBarButtonItem = backItem // This will show in the next view controller being pushed

    if(segue.identifier == "notificationconnew"){
        let notObj = segue.destination as! NotificationsTableViewController
        notObj.homeVC = self
    }


}

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {

    if identifier == segueString && self.navigationController?.viewControllers.last is MyEarningViewController{
        return false
    }
    else{
        return true
    }
}

Declaration of homeVc object in App Delegate

class AppDelegate: UIResponder, UIApplicationDelegate{

var window: UIWindow?
var notification : NSDictionary?
var homeVC : HomeViewController?
lazy var dBhandler = DBHandler()
let date = NSDate()

And giving instance in viewDidLoad of HomeViewController

// Giving instance of HomeViewController to AppDelegate
    let appDele = UIApplication.shared.delegate as! AppDelegate
    appDele.homeVC = self

Check this enter image description here

Prathamesh
  • 140
  • 1
  • 2
  • 15

2 Answers2

3

I came across this SO thread and noticed that, when you call performSegue by your self it will never call shouldPerformSegue for that. Now there is only one and that is that you need to call performSegue only if your current Controller is not the segue's destination one. So make changes in AppDelegate like this way.

if (homeVC != nil && !(homeVC?.navigationController?.viewControllers.last is MyEarningViewController)){
     homeVC?.performSegue(withIdentifier: moduleName, sender: homeVC)
}

So remove the shouldPerformSegue from your HomeViewController now there is no need of it and call performSegue by comparing the navigationStack of homeVC.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • how to get YourviewController object, because I don't know what destination segue will be because on click of notification it can go to any view depend on the action segue which comes in the notification. here the segue comes {let moduleName = userInfo.value(forKey: "click_action") as! String} and I am using this module name in performsegue homeVC?.performSegue(withIdentifier: moduleName, sender: homeVC) – Prathamesh May 23 '17 at 12:23
  • should I check this with every view controller, it will be too much long, is there any other way – Prathamesh May 23 '17 at 12:32
  • actually, shouldperformsegue method is not getting called, I applied breakpoint in the method but not working, only prepare for segue is getting called – Prathamesh May 23 '17 at 12:51
  • @Prathamesh You have put `shouldPerformSegue` in wrong place you need to put that method in `homeVC`'s class, so add the method in your variable homeVC's type class because you are performing segue with that controller – Nirav D May 24 '17 at 05:05
  • both the method prepareforsegue and shouldPerformsegue are in HomeViewController class I have just pasted here in question one after other – Prathamesh May 24 '17 at 05:11
  • @Prathamesh Ok then check is `prepareforsegue` is getting code, put break point inside that also and check is it called or not also can you please show me the declaration of homeVC? because using that you are performing segue – Nirav D May 24 '17 at 05:14
  • class AppDelegate: UIResponder, UIApplicationDelegate{ var window: UIWindow? var notification : NSDictionary? var homeVC : HomeViewController? lazy var dBhandler = DBHandler() let date = NSDate() – Prathamesh May 24 '17 at 05:16
  • @Prathamesh Ok, Have you put breakpoint in `prepareForSegue` is it getting called or not ? – Nirav D May 24 '17 at 05:19
  • prepareForSegue is getting called but shouldperformsegue is not getting called – Prathamesh May 24 '17 at 05:23
  • @Prathamesh Ok if `prepareForSegue` is getting called than try one thing put break point in `shouldPerformsegue` at last line which is `return true` and check is it executing or not – Nirav D May 24 '17 at 05:26
  • I checked adding breakpoint in shouldPerformSegue but it is not getting called – Prathamesh May 24 '17 at 05:36
  • @Prathamesh Can you add screenshot with break point and show me that prepareForSegue is getting called and `shouldPerformSegue` is not. – Nirav D May 24 '17 at 05:41
  • @Prathamesh I understand the issue give me some minute to edit my answer. – Nirav D May 24 '17 at 06:11
  • @Prathamesh Check the edited answer and try like that and respond here is it working or not. – Nirav D May 24 '17 at 06:23
  • Nice!! It is Working, but how to check this line for different view controllers – Prathamesh May 24 '17 at 06:31
  • @Prathamesh Welcome mate :) – Nirav D May 24 '17 at 06:33
  • @Prathamesh if you want to compare same thing in other controller then you need to simply replace `homeVC` to instance object on which you are performing segue and change the compare controller to one that you want to compare – Nirav D May 24 '17 at 06:45
0

You can handle in two ways

if(yourViewcontroller.isloaded ){ do nothing }else{ perform seque }

or

you handle in the shouldPerformSegueWithIdentifier:sender: method gives you an opportunity to prevent a segue from happening. Returning NO when your view is loaded.