2017-06-20 325 views
1

我在Doc上做了任何事情,但当我打电话给GIDSignIn.sharedInstance().signIn()时它崩溃了。iOS谷歌登录失败

代码:

在AppDelegate中:

func configGoogleServer() { 
    var configureError: NSError? 
    GGLContext.sharedInstance().configureWithError(&configureError) 
    assert(configureError == nil, "Error configuring Google services: \(configureError as Optional)") 
    GIDSignIn.sharedInstance().delegate = self 
} 

在一些的viewController:

GIDSignIn.sharedInstance().signIn() 

而且我已经配置URL方案一样com.googleusercontent.apps.598xxx...xxx

崩溃截图:

enter image description here

,没有什么显示在调试区... :(

+0

你是如何安装sdk的? – ridvankucuk

+0

您可以通过google [here]找到示例代码(https://github.com/googlesamples/google-services/tree/master/ios/signin/SignInExampleSwift) – nayem

+0

添加引发的确切错误。 – eshirima

回答

1

你把委托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 
     } 
    } 
} 
+0

是的,你是对的,我应该设置ViewController委托,它现在修复了,谢谢! – Leo

1

添加以下代码到YourViewController:

class ViewController: UIViewController,GIDSignInUIDelegate { 

override func viewWillAppear(_ animated: Bool) { 
     GIDSignIn.sharedInstance().delegate = self 
     GIDSignIn.sharedInstance().uiDelegate = self 
     } 
} 

你忘实施代表:

func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) { 

    if let error = error { 
     print(error.localizedDescription) 
     return 
    } 
    else{ 
//handle response 
} 
    } 
+0

是的,你是对的,谢谢:) – Leo