2

I am working on one of the application where i am promoting user for force update using UIAlertController in which i don't wanted to allow user to perform any activity until and unless he updates the application from AppStore.

To achieve this i have written following code.

if (needToUpdate)
{
    var alert = UIAlertController(title: "New Version Available", message: "There is a newer version available for download! Please update the app by visiting the Apple Store.", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(
        UIAlertAction(
            title: "Update",
            style: UIAlertActionStyle.Default,
            handler: { alertAction in
                UIApplication.sharedApplication().openURL(NSURL(string : "https://itunes.apple.com/app/cheapo-casino-free-casino/id637522371?ls=1&mt=8")!)
                alert.dismissViewControllerAnimated(true, completion: nil)
            }
        )
    )
    self.presentViewController(alert, animated: true, completion: nil)
}

It is working good but whenever user presses Update button it will go to app store and if user don't update the application. he/she will come back to application and can perform any activity which is not expected.

Is there any way to show the UIAlertController even if user press update button?

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Aman.Samghani
  • 2,151
  • 3
  • 12
  • 27
  • 5
    FYI - Apple really doesn't want apps doing what you are doing. Let a user decide whether they want to update or not. Personally I hate apps that force me to update when I don't want to. – rmaddy Dec 12 '17 at 16:43
  • 1
    Although I basically agree with the above comment, I have to ask: If you don't want it to close, why are you calling `dismissViewControllerAnimated`? – Phillip Mills Dec 12 '17 at 16:55
  • May be this link will help you https://stackoverflow.com/questions/28919670/prevent-uialertcontroller-to-dismiss – Kuntal Gajjar Dec 12 '17 at 16:56
  • @PhillipMills The call to `dismissViewController` is pointless and shouldn't even be there. You can't prevent an alert controller from being dismissed once a button is tapped. It's dismissed automatically. – rmaddy Dec 12 '17 at 17:12

1 Answers1

3

You can check the version of the app in appDelegate.

func applicationDidBecomeActive(_ application: UIApplication) {
     //check the app version here and if version mismatch is there show the alert.
     // if the version is different then make initial viewController as root view controller. and then present alert from that view controller.
}
Rahul Dasgupta
  • 894
  • 6
  • 18