你把委托AppDelegate.swift
里面,这不是真的,你的AppDelegate应该是这样的:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// .. something else
// GOOGLE
// Initialize sign-in
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
// No delegate here
return true
}
func application(_ application: UIApplication,
open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
return GIDSignIn.sharedInstance().handle(url,
sourceApplication: sourceApplication,
annotation: annotation)
}
@available(iOS 9.0, *)
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
if let sourceApplication = options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String {
let annotation = options[UIApplicationOpenURLOptionsKey.annotation]
return GIDSignIn.sharedInstance().handle(url,
sourceApplication: sourceApplication,
annotation: annotation)
}
return true
}
,然后把签到代表内部YourViewController其签字,在动作发生:U
class YourViewController: UIViewController {
// something else....
func doSignIn() {
GIDSignIn.sharedInstance().delegate = self
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().scopes = YOUR_GOOGLE_SCOPES
if GIDSignIn.sharedInstance().hasAuthInKeychain() {
GIDSignIn.sharedInstance().signInSilently()
} else {
GIDSignIn.sharedInstance().signIn()
}
}
}
extension YourViewController: GIDSignInDelegate, GIDSignInUIDelegate {
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!,
withError error: Error!) {
if let error = error {
self.showMessage("Authentication Error", type: .error)
self.service.authorizer = nil
} else {
self.service.authorizer = user.authentication.fetcherAuthorizer()
// PUT YOUR METHOD AFTER SIGNED-IN HERE
}
}
}
你是如何安装sdk的? – ridvankucuk
您可以通过google [here]找到示例代码(https://github.com/googlesamples/google-services/tree/master/ios/signin/SignInExampleSwift) – nayem
添加引发的确切错误。 – eshirima