我尝试在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问题都基于过时的代码。
的viewController
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"]!
}
}
}
}
的AppDelegate:
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."])
}
}
答案 0 :(得分:1)
tl; dr:如果使用非UIViewController
作为uiDelegate
- 请检查Xcode是否警告您" 实例方法[...]几乎匹配可选要求[...] "并使用Fix-It。确保您的功能完全符合所需的签名。
遵循示例代码后我遇到了同样的错误。
在我的情况下,设置有点不同,所以我不确定我的解决方案是否适用于您的问题 - 但是因为这个StackOverflow答案显示在Google搜索中,我想我会在此提供我的解决方案任何人都磕磕绊绊:
我需要一个非UIVIewController
子类作为Google登录uiDelegate
。作为文档和错误状态,这意味着您需要实现一些方法。我已经实现了它们,所以事情崩溃很奇怪。
就我而言,问题在于Google的文档中的复制粘贴代码与Swift功能签名并不完全匹配。事实上,我在Xcode中有警告,说" 实例方法[...]几乎匹配可选要求[...] "。我使用了Xcode的自动修复功能(在Error
之类的东西,而不是NSError
或_
之前的函数的参数。)
然后一切正常。