gpt4 book ai didi

ios - Google 登录崩溃和错误 : uiDelegate must either be a |UIViewController|

转载 作者:搜寻专家 更新时间:2023-10-31 22:55:45 25 4
gpt4 key购买 nike

我正在尝试对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com