user3766930
user3766930

Reputation: 5829

how can i add a popup that is invoked from my appdelegate class in swift?

I'm working on a google sign in tutorial to my ios app and there's a part when we cannot log in user to my app.

So far the code section in appDelegate.swift looks like:

guard error == nil && data != nil else {
     // check for fundamental networking error
     print("error=\(error)")

     //lets put popup here that says cant connect to server

     GIDSignIn.sharedInstance().signOut()
     return
}

and now instead of printing the error I want to put the alert popup window. I tried to write there:

  let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
  alert.addAction(UIAlertAction(title: "Click", style: UIAlertActionStyle.Default, handler: nil))
  self.presentViewController(alert, animated: true, completion: nil)

but then I'm getting the xcode error near self.presentViewController saying that value of type AppDelegate has no member presentViewController.

How can I display an alert popup in that case?

Upvotes: 4

Views: 5694

Answers (2)

Imtee
Imtee

Reputation: 1393

In Swift 3

self.window?.rootViewController?.present(alert, animated: true, completion: nil)

Upvotes: 7

N.Raval
N.Raval

Reputation: 549

Try Using this line :-

self.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

Here you need is a viewController object to present the AlertController, Hope this will help you :)

Upvotes: 6

Related Questions