关于 swift:防止 UIAlertController 在 UIAlertAction 上解除 | 珊瑚贝

Prevent UIAlertController from dismissing on UIAlertAction


我正在开发一个应用程序,我正在使用 UIAlertController 促进用户进行强制更新,在该应用程序中我不想允许用户执行任何活动,除非他从 Appstore 更新应用程序。

为了实现这一点,我编写了以下代码。

1
2
3
4
5
6
7
8
9
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)
    }

它运行良好,但只要用户按下更新按钮,它就会进入应用商店,如果用户不更新应用程序。他/她将返回应用程序并可以执行任何不期望的活动。

即使用户按下更新按钮,有什么方法可以显示 UIAlertController 吗?

  • 仅供参考 – Apple 真的不希望应用程序做你正在做的事情。让用户决定是否要更新。就我个人而言,我讨厌那些在我不想更新时强迫我更新的应用程序。
  • 虽然我基本上同意上面的评论,但我不得不问:如果你不希望它关闭,你为什么要调用 dismissViewControllerAnimated?
  • 可能这个链接会帮助你stackoverflow.com/questions/28919670/…
  • @PhillipMills 对 dismissViewController 的调用毫无意义,甚至不应该存在。一旦点击按钮,您就无法阻止警报控制器被关闭。它会自动关闭。


您可以在 appDelegate 中查看应用的版本。

1
2
3
4
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.
}


来源:https://www.codenong.com/47777461/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_8994.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?