- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试对 iOS 应用实现 Google 登录,但该应用在点击登录按钮时崩溃并出现以下错误:
reason: 'uiDelegate must either be a |UIViewController| or implement the |signIn:presentViewController:| and |signIn:dismissViewController:| methods from |GIDSignInUIDelegate|.'
我不确定我哪里出错了。我关注了 sample code from Google's iOS github确切地。我也无法编译 Google 的示例。如果有人能指出我正确的方向,那就太好了。大多数 SO 问题都基于过时的代码。
View Controller
import UIKit
import GoogleSignIn
@objc(ViewController)
class ViewController: UIViewController, GIDSignInUIDelegate {
// Viewcontroller buttons
@IBOutlet weak var signInButton: GIDSignInButton!
@IBOutlet weak var signOutButton: UIButton!
@IBOutlet weak var disconnectButton: UIButton!
@IBOutlet weak var statusText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
// Sign in automatically
GIDSignIn.sharedInstance().signInSilently()
// Something to do with notifications
NotificationCenter.default.addObserver(self,
selector: #selector(ViewController.receiveToggleAuthUINotification(_:)),
name: NSNotification.Name(rawValue: "ToggleAuthUINotification"),
object: nil)
statusText.text = "Initialized Swift App..."
toggleAuthUi()
}
// Sign out tapped
@IBAction func didTapDisconnect(_ sender: AnyObject) {
GIDSignIn.sharedInstance().disconnect()
statusText.text = "Disconnecting"
}
// Toggle auth
func toggleAuthUi() {
if GIDSignIn.sharedInstance().hasAuthInKeychain() {
signInButton.isHidden = true
signOutButton.isHidden = false
disconnectButton.isHidden = false
} else {
signInButton.isHidden = false
signOutButton.isHidden = true
disconnectButton.isHidden = true
}
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIStatusBarStyle.lightContent
}
deinit {
NotificationCenter.default.removeObserver(self,
name: NSNotification.Name(rawValue: "ToggleAuthUINotification"),
object: nil)
}
@objc func receiveToggleAuthUINotification(_ notification: NSNotification) {
if notification.name.rawValue == "ToggleAuthUINotification" {
self.toggleAuthUi()
if notification.userInfo != nil {
guard let userInfo = notification.userInfo as? [String:String] else {return}
self.statusText.text = userInfo["statusText"]!
}
}
}
}
应用委托(delegate):
import UIKit
import GoogleSignIn
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
var window: UIWindow?
// Did Finished Launching
func application (_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Init Sign in
GIDSignIn.sharedInstance().clientID = "XXXXXXXXX"
GIDSignIn.sharedInstance().delegate = self
return true
}
// Open URL
func application (_app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any]) -> Bool {
return GIDSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
}
public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
if let error = error {
print("\(error.localizedDescription)")
NotificationCenter.default.post(
name: Notification.Name(rawValue: "ToggleAuthUINotificiation"), object: nil, userInfo: nil)
} else {
// User Stuff
let userID = user.userID
let idToken = user.authentication.idToken
let fullName = user.profile.name
let givenName = user.profile.givenName
let familyName = user.profile.familyName
let email = user.profile.email
NotificationCenter.default.post(
name: Notification.Name(rawValue: "ToggleAuthUINotification"),
object: nil,
userInfo: ["statusText": "Signed in user:\n\(fullName)"])
}
}
public func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) {
// Disconnect the user
NotificationCenter.default.post(
name: Notification.Name(rawValue: "ToggleAuthUINotification"),
object: nil,
userInfo: ["statusText": "User has disconnect."])
}
}
最佳答案
tl;dr:如果使用非 UIViewController
作为 uiDelegate
- 检查 Xcode 是否警告您“实例方法 [...]匹配可选要求 [...]”并使用 Fix-Its。确保您的函数与所需的签名完全匹配。
按照示例代码,我遇到了同样的错误。
在我的例子中,设置有点不同,所以我不确定我的解决方案是否适用于你的问题 - 但因为这个 StackOverflow 答案显示在谷歌搜索中,我想我会在这里为任何人提供我的解决方案绊倒它:
我需要一个非 UIVIewController
子类作为 Google 登录 uiDelegate
。作为文档和错误状态,这意味着您需要实现一些方法。我已经实现了它们,所以出现崩溃很奇怪。
在我的案例中,问题是来自 Google 文档的复制粘贴代码与 Swift 函数签名不完全匹配。事实上,我在 Xcode 中收到警告,说“实例方法 [...] 几乎匹配可选要求 [...]”。我使用了 Xcode 的自动 Fix-Its(它是诸如 Error
而不是 NSError
或函数参数之前的 _
之类的东西。)
然后一切正常。
关于ios - Google 登录崩溃和错误 : uiDelegate must either be a |UIViewController|,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48085620/
我已将委托(delegate)分配给 wkwebview uiDelegate 并实现了它的方法 create web view with configuration,但它没有被调用。我什至设置了断点
我的任务是创建一个自定义的 Swing 组件。我的组件在包含用于放大和缩小图像的 JSlider 的测试应用程序中正常运行。但是,我需要以 Model、UIDelegate 和 Component 类
我在某处阅读以使用 - (void)webView:(WebView *)webView addMessageToConsole:(NSDictionary *)message 来自 UIDelega
我正在开发一个适用于 iOS 的 WKWebView-App,它可以增强现有网站的功能,但我无法更改该网站的源代码。 当用户单击某些按钮时,它们会触发window.open。我已成功相应地覆盖了委托(
我正在尝试对 iOS 应用实现 Google 登录,但该应用在点击登录按钮时崩溃并出现以下错误: reason: 'uiDelegate must either be a |UIViewControl
从 Crashlytics,我们在 Google 登录过程中收到大量崩溃报告。崩溃报告如下: Fatal Exception: NSInvalidArgumentException uiDelegat
我是一名优秀的程序员,十分优秀!