0

Here is my generic class which I'm using everywhere in my project but it's making issues on iOS 13, i.e AlertView appear n automatically disappear before any option selection.

//var status = false
class AlertClass{//This is shared class
    static let sharedInstance = AlertClass()

    //Show alert
    func alertWindow(title: String, message: String, completion:@escaping (_ result: Bool) -> Void) {
            let alertWindow = UIWindow(frame: UIScreen.main.bounds)
            alertWindow.rootViewController = UIViewController()
            alertWindow.windowLevel = UIWindow.Level.alert + 1

            let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
            let defaultAction1 = UIAlertAction(title: "Yes", style: .destructive, handler: { action in
                completion(true)

            })
            let defaultAction2 = UIAlertAction(title: "No", style: .destructive, handler: { action in
                print("No")
                completion(false)
            })
            alert2.addAction(defaultAction1)
            alert2.addAction(defaultAction2)

            alertWindow.makeKeyAndVisible()

            alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)
    }

    func alertOkWindow(title: String, message: String, completion:@escaping (_ result: Bool) -> Void) {
        let alertWindow = UIWindow(frame: UIScreen.main.bounds)
        alertWindow.rootViewController = UIViewController()
        alertWindow.windowLevel = UIWindow.Level.alert + 1

        let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let defaultAction1 = UIAlertAction(title: "Ok", style: .default, handler: { action in
            completion(true)

        })
        alert2.addAction(defaultAction1)

        alertWindow.makeKeyAndVisible()

        alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)
    }


    func alertDialogWithImage(msg: String, Icon : UIImage?) {
        let alertDialogWithImage = UIWindow(frame: UIScreen.main.bounds)
        alertDialogWithImage.rootViewController = UIViewController()
        alertDialogWithImage.windowLevel = UIWindow.Level.alert + 1
        let alrt = UIAlertController(title:  " ", message: msg, preferredStyle: .alert)

        let cancel = UIAlertAction(title: "Ok", style: .destructive) { (action) in


        }



        alrt.addAction(cancel)

        let subview = (alrt.view.subviews.first?.subviews.first?.subviews.first!)! as UIView

//        subview.backgroundColor = UIColor(red: (145/255.0), green: (200/255.0), blue: (0/255.0), alpha: 1.0)

//        alrt.view.tintColor = UIColor.black
        alrt.view.tintColor = UIColor.red

        let imageView = UIImageView(frame: CGRect(x: 120, y: 10, width: 35, height: 35))

        imageView.image = Icon

        alrt.view.addSubview(imageView)

        alertDialogWithImage.makeKeyAndVisible()
        alertDialogWithImage.rootViewController?.present(alrt, animated: true, completion: nil)
    }

    func alertWith3Actions(title: String, message: String, action1: String, action2: String, action3: String, completion:@escaping (_ result: Bool) -> Void) {
        let alertWindow = UIWindow(frame: UIScreen.main.bounds)
        alertWindow.rootViewController = UIViewController()
        alertWindow.windowLevel = UIWindow.Level.alert + 1

        let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let defaultAction1 = UIAlertAction(title: action1, style: .destructive, handler: { action in
            completion(true)

        })
        let defaultAction2 = UIAlertAction(title: action2, style: .destructive, handler: { action in
            print("No")
            completion(false)
        })
        let defaultAction3 = UIAlertAction(title: action3, style: .destructive, handler: { action in
            print("Later")
            completion(false)
        })
        alert2.addAction(defaultAction1)
        alert2.addAction(defaultAction2)
        alert2.addAction(defaultAction3)

        alertWindow.makeKeyAndVisible()

        alertWindow.rootViewController?.present(alert2, animated: true, completion: nil)
    }

    func alertTitleandMessage(title: String, message: String) {
        DispatchQueue.main.async(execute: {
            let alertTitleandMessage = UIWindow(frame: UIScreen.main.bounds)
            alertTitleandMessage.rootViewController = UIViewController()
            alertTitleandMessage.windowLevel = UIWindow.Level.alert + 1

            let alert2 = UIAlertController(title: title, message: message, preferredStyle: .alert)
            let defaultAction2 = UIAlertAction(title: "OK", style: .destructive, handler: { action in
            })
            alert2.addAction(defaultAction2)

            alertTitleandMessage.makeKeyAndVisible()

            alertTitleandMessage.rootViewController?.present(alert2, animated: true, completion: nil)
        })
    }

}

I have change my code like I define these function in UIViewController Extension but after that I'm unable to call them in UITableViewCell & UICollectionViewCell. Your help n guidance will be much appreciated. Thanks in advance. Br.

  • Does this answer your question? [Every UIAlertController disappear automatically before user responds - since iOS 13](https://stackoverflow.com/questions/58131996/every-uialertcontroller-disappear-automatically-before-user-responds-since-ios) – pepsy Feb 28 '20 at 15:36
  • Yes but I found a solution. – Muhammad Hasan Irshad Feb 29 '20 at 03:34

0 Answers0