0

I want to display alert for check new version of my app from API. And from that view if userDefault data is store so base on that I want to redirect to another view. My redirection code is work perfectly but when I add alert code so redirection doesn't work. I think alert present in "self" and that time I also try to redirect from that view so it's may create problem. Here is my code..

    override func viewWillAppear(_ animated: Bool) {
            super.viewWillAppear(animated)
            updateUserData() //base on userDefault redirection 
        }
    override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            checkNewVersion() //Alert function for API calling
        }

func checkNewVersion() -> Void {

    //TODO: For check App new version
       WebRequester.shared.getAppNewVersion { (result, error) in
        if result != nil {
            if result?.value(forKey: "status") as! Bool {
                let strVer = (result?.object(forKey: "data") as! NSDictionary).object(forKey: "platform_version") as! String
                if UserData.getAppVersion() < strVer {
                    let alert = UIAlertController(title: "Alert", message: "New version of App available", preferredStyle: .alert)
                    let ok = UIAlertAction(title: "Ok", style: .default, handler: { (action) in
                    })
                    let AppStore = UIAlertAction(title: "App Store", style: .default, handler: { (action) in
                        if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
                            UIApplication.shared.canOpenURL(url){
                            UIApplication.shared.openURL(url)
                        }
                    })
                    alert.addAction(ok)
                    alert.addAction(AppStore)
                    self.present(alert, animated: true, completion: nil)
//                    OperationQueue().addOperation {
//                        // Put queue to the main thread which will update the UI
//                        OperationQueue.main.addOperation({
//                           self.present(alert, animated: true, completion: nil)
//                        })
//                    }

                }
            }
            else {
                if (result?.object(forKey: "data") as! NSArray).object(at: 0) as? String ?? "" == "Unauthorised access." {
                    Model.shared.deleteAllCoreDataRecord(entity: "CartItem")
                    UIApplication.topViewController()?.navigationController?.popToRootViewController(animated: true)
                }
                let msg = result?.value(forKey: "data") as! [String]
                Model.shared.showAlert(title: "Error", msg: msg[0], controller: self)
            }
        }
        else {
            Model.shared.showAlert(title: "Error", msg: error?.localizedDescription ?? "Something went wrong at add new address", controller: self)
        }
    }
}

func updateUserData() -> Void {
    if UserData.getAppVersion() == "1.0" {

        if UserData.getUserData() != nil  {

            //TODO: check for user Updated data
            let params = ["mobile":UserData.getUserMobile()] as [String : Any]
            let propic = UIImage(named: "temp")
            weak var objWeek = self

            Model.shared.showActivity(WithTouchEnable: false,controller: self)
            WebRequester.shared.customerSignup(params: params as NSDictionary, proImg: propic!){ (result,error) -> Void in
                Model.shared.HideActivity(controller: self)
                if (error == nil) {
                    print("login result:",result ?? "")
                    //handle response of sign up
                    let statusstr = result?["status"] as! Bool
                    if (statusstr == false) {
                        //This condition for pepsi welcome offer is expire or not
                        let user = result!["user_info"] as! NSDictionary
                        self.storeUserData(user: user)

                        if UserData.getUserMobileNumVerify() == "No" {
                            let storyboard = UIStoryboard(name: "Main", bundle: nil)
                            let registerScreen = storyboard.instantiateViewController(withIdentifier: "UserRegisterPhoneVC") as! UserRegisterPhoneVC

                            objWeek?.navigationController?.pushViewController(registerScreen, animated: true)
                        }
                        else {
                            if UserData.getPepsiOfferRedim() == "1" || UserData.getPepsiOfferRedim() == "2" {
                                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                                let offerScreen = storyboard.instantiateViewController(withIdentifier: "OfferViewController") as! OfferViewController
                                objWeek?.navigationController?.pushViewController(offerScreen, animated: true)
                            }
                            else {
                                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                                let promoScreen = storyboard.instantiateViewController(withIdentifier: "PromotionViewController") as! PromotionViewController
                                objWeek?.navigationController?.pushViewController(promoScreen, animated: true)
                            }
                        }
                    }
                }
            }
            else {
                Model.shared.showAlert(title: "Error", msg: (error?.localizedDescription)!, controller: self)
            }
        }

    }

}
}
shallowThought
  • 19,212
  • 9
  • 65
  • 112
NiravS
  • 1,144
  • 1
  • 12
  • 24

1 Answers1

0

To achieve this , you need to click on alert OK button then only it will automatically navigate to other controller , without this not possible .

Here is code :

Alert controller block help you to achieve this :

 //show an alert and navigate to previous controller
            let alertController: UIAlertController = UIAlertController(title: "Password updatd", message: "your alert message", preferredStyle: .alert)

            let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in

           //Redirect to new viewcontroler 
           let newVC = self.storyboard.instantiateViewcontroller(identifier: "newvc") as? NewVC
           self.navigationController?.pushViewController(newVC,animated: true)
            }

            alertController.addAction(okAction)
            self.present(alertController, animated: true, completion: nil)

Feel free to comment. Thanks

Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71