我如何在 swift 中添加从我的 appdelegate class 调用的弹出窗口?

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

我正在为我的 ios 应用程序开发 google sign in tutorial,并且在某些情况下我们无法让用户登录我的应用程序。

到目前为止,appDelegate.swift 中的代码部分如下所示:

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
}

现在我不想打印错误,而是要弹出警报 window。我试着在那里写:

  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)

但后来我在 self.presentViewController 附近收到 xcode 错误,说 value of type AppDelegate has no member presentViewController.

在这种情况下如何显示警报弹出窗口?

尝试使用这一行:-

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

这里您需要一个 viewController 对象来呈现 AlertController,希望这对您有所帮助:)

Swift3

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