1

When I am using alert in swift at the line of present alert I get this error use of unresolved identifier.

File name : CommonFunctions.swift

class name : class CommonFunctions : NSObject

I am using this code

class func gotoSettingScreen()
{
    let alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
        guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
            return
        }

        if UIApplication.shared.canOpenURL(settingsUrl) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                    print("Settings opened: \(success)") // Prints true
                })
            } else {
                // Fallback on earlier versions
            }
        }
    }
    alertController.addAction(settingsAction)
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    alertController.addAction(cancelAction)


    present(alertController, animated: true, completion: nil)
}

Line Error Come :present(alertController, animated: true, completion: nil)

Error is : enter image description here

Thanks for your help ! and Appreciation !

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Bhaumik Joshi
  • 245
  • 1
  • 7
  • 17

3 Answers3

1

present(_:animated:completion:) is an instance method of UIViewController and derived types (like UINavigationController). So calling it from class (static) function of some random class will of course fail.

So what you have here is design issue.

Workaround to call alert from anywhere in the app:

let alertController = UIAlertController(title: "title", message: "message", preferredStyle: .alert)
//...
var rootViewController = UIApplication.shared.keyWindow?.rootViewController
if let navigationController = rootViewController as? UINavigationController {
    rootViewController = navigationController.viewControllers.first
}
if let tabBarController = rootViewController as? UITabBarController {
    rootViewController = tabBarController.selectedViewController
}
rootViewController?.present(alertController, animated: true, completion: nil)

Depending on your UI architecture, you can remove unneeded checks.

Suggested code snippet taken from here.

Hexfire
  • 5,945
  • 8
  • 32
  • 42
0

You need a reference for the ViewController where you wanna show the alert. Since you are using a custom class, NSObject: CommonFunctions, when you wanna show the alert, you need to either pass in the instance of view controller or set the view controller as one of the properties of the CommonFunctions class.

Example:

// Basic.Swift 
......

var viewController : UIViewController?

When calling the method to show the alert, set the view controller property to the current instance like

CommonFunctions.viewController = self

You would have to modify your CommonFunctions's getSettingScreen() function to use

viewController.present()

instead of present()

This should help.

Umar Farooque
  • 2,049
  • 21
  • 32
0

If you want to use alert from non uiviewcontroller u can pass uiviewcontroller instance form method like

Method in NonUicontroller class

func alertDilog(viewController :UIViewController)
{
   let alertController = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert)
let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
            (result : UIAlertAction) -> Void in
            print("OK")
   alertController.addAction(okAction)
        vc.present(alertController, animated: true, completion: nil)
    }
        }

To use alertDialog in UiViewcontrollerclass

yourclassName.alertDilog(viewController :self)
Jhony
  • 187
  • 1
  • 4
  • 16