Alertbox function for common utils iOS swift || Alertbox custom function in common

 Solution:

You can use the below function to show alert as a single function you can call wherever you want.

Also must to be implement the extension of the application.

    func showAlert(msg:NSString,controller:UIViewController) {

        if let topController = UIApplication.topViewController() {

            if !(topController is UIAlertController) {

                let alert = UIAlertController(title:"Apptitle" as String , message: msg as String, preferredStyle: .alert)

                alert.addAction(UIAlertAction(title: "OK", style: .default, handler:nil))

                topController.present(alert, animated: true, completion: nil)

            }

        }

    }


Use the extension of the application as like function below


extension UIApplication {

    class func topViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {

        if let nav = base as? UINavigationController {

            return topViewController(base: nav.visibleViewController)

        }

        if let tab = base as? UITabBarController {

            if let selected = tab.selectedViewController {

                return topViewController(base: selected)

            }

        }

        if let presented = base?.presentedViewController {

            return topViewController(base: presented)

        }

        return base

    }

}

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Convert NsNumber, NSDate to String in iOS Swift

Global LocationManager Singleton class iOS Swift