How to show UIAlertController from Appdelegate

前端 未结 6 1722
后悔当初
后悔当初 2020-11-30 01:32

I\'m working with PushNotification on iOS app. I would like to show a UIalertcontroller when the app receive a notification.

I try this code below in the AppDelegate

6条回答
  •  眼角桃花
    2020-11-30 02:21

    I have written a static class to make code reusable in swift 4, This class provied different methods for showing alerts.

            class AlertUtility: NSObject {
    
            static func showAlert(title: String!, message : String!, viewController: UIViewController) {
                let alert = UIAlertController(title: title, message: message ,preferredStyle: UIAlertControllerStyle.alert)
    
                alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "ok"), style: UIAlertActionStyle.cancel, handler: nil))
    
                viewController.present(alert, animated: true, completion: nil)
            }
    
            static func showAlertAutoDismiss(title: String!, message : String!) -> Void {
                //let appDelegate = UIApplication.shared.delegate as! AppDelegate
    
                // the alert view
                let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    
                let topWindow = UIWindow(frame: UIScreen.main.bounds)
                topWindow.rootViewController = UIViewController()
                topWindow.windowLevel = UIWindowLevelAlert + 0.8
    
                topWindow.makeKeyAndVisible()
                topWindow.rootViewController?.present(alert, animated: true, completion: {})
    
                // change to desired number of seconds (in this case 5 seconds)
                let when = DispatchTime.now() + 1
                DispatchQueue.main.asyncAfter(deadline: when){
                    // your code with delay
                    alert.dismiss(animated: true, completion: nil)
                    topWindow.isHidden = true
                }
            }
    
            // Show alert view with call back
            static func showAlertWithCB(title: String, message: String, isConditional: Bool, viewController: UIViewController, completionBlock: @escaping (_: Bool) -> Void) {
    
                let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
    
                // Check whether it's conditional or not ('YES' 'NO, or just 'OK')
                if isConditional
                {
                    alert.addAction(UIAlertAction(title: NSLocalizedString("yes", comment: ""), style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in
                        alert.dismiss(animated: true, completion: nil)
                        completionBlock(true)
                    }))
    
                    alert.addAction(UIAlertAction(title: NSLocalizedString("no", comment: ""), style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in
                        alert.dismiss(animated: true, completion: nil)
                        completionBlock(false)
                    }))
                }
                else
                {
                    alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "ok"), style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in
                        alert.dismiss(animated: true, completion: nil)
                        completionBlock(true)
                    }))
                }
    
                viewController.present(alert, animated: true, completion: nil)
            }
    
            static func showAlert(title: String!, message : String!) -> Void {
                //let appDelegate = UIApplication.shared.delegate as! AppDelegate
    
                // the alert view
                let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
    
                let topWindow = UIWindow(frame: UIScreen.main.bounds)
                topWindow.rootViewController = UIViewController()
                topWindow.windowLevel = UIWindowLevelAlert + 1
                topWindow.makeKeyAndVisible()
                topWindow.rootViewController?.present(alert, animated: true, completion: {})
    
                alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "ok"), style: UIAlertActionStyle.cancel, handler: {(_ action: UIAlertAction) -> Void in
                    // continue your work
                    // important to hide the window after work completed.
                    // this also keeps a reference to the window until the action is invoked.
                    topWindow.isHidden = true
                }))
    
            }
    
            static func showComingSoon(viewController: UIViewController) {
                let alert = UIAlertController(title: "", message: "Coming Soon", preferredStyle: .alert)
    
                viewController.present(alert, animated: true, completion: {})
    
                // change to desired number of seconds (in this case 1 seconds)
                let when = DispatchTime.now() + 0.6
                DispatchQueue.main.asyncAfter(deadline: when){
                    // your code with delay
                    alert.dismiss(animated: true, completion: nil)
                }
            }
    
            static func showGenericErrorMessageAlert(viewController: UIViewController) {
                let alert = UIAlertController(title: NSLocalizedString("error", comment: ""), message: NSLocalizedString("generic.error.message", comment: "") ,preferredStyle: UIAlertControllerStyle.alert)
    
                alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "ok"), style: UIAlertActionStyle.cancel, handler: nil))
    
                viewController.present(alert, animated: true, completion: nil)
            }
    
     static func showAlertWithTextField(viewController : UIViewController,completionBlock: @escaping (_: Bool, String) -> Void) {
    
            //1. Create the alert controller.
            let alert = UIAlertController(title: "Report Event?", message: "", preferredStyle: .alert)
            alert.view.tintColor = APP_ORANGE_COLOR
            //2. Add the text field. You can configure it however you need.
            //AlertUtility.addte
            alert.addTextField { (textField) in
    
                let heightConstraint = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50)
                textField.addConstraint(heightConstraint)
                textField.placeholder = "Enter report reason here"
                textField.tintColor = APP_ORANGE_COLOR
                textField.autocapitalizationType = .sentences
            }
    
            // 3. Grab the value from the text field, and print it when the user clicks OK.
            alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (_) in
                // Force unwrapping because we know it exists.
                completionBlock(true,"")
                //print("Text field: \(textField.text)")
            }))
    
            // 3. Grab the value from the text field, and print it when the user clicks OK.
            alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: { [weak alert] (_) in
                let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
                completionBlock(true,(textField?.text)!)
                //print("Text field: \(textField.text)")
            }))
    
            // 4. Present the alert.
            viewController.present(alert, animated: true, completion: nil)
    
            let textField = alert.textFields![0]
            let v = UIView.init(frame: textField.frame)
            textField.addSubview(v)
            v.frame = textField.frame
            v.bounds = textField.bounds
            v.backgroundColor = APP_ORANGE_COLOR
            v.superview?.bringSubview(toFront: v)
           }
    
        }
    

提交回复
热议问题