2016-10-30 5 views
0

私は自分のアプリケーションでFirebaseを使用しています.Google Signin関数を追加したいと思います。 Googleによると、サイン機能を私のAppdelegateに追加する必要があります。私の問題は、私がappdelegateからログインを実行したいのであれば、どのようにしてユーザーにエラーを表示できるかということです。私は次のコードを試しました。App Delegateからエラーを表示する - Google Signin

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

    if let error = error { 
     print() 
     let importantAlert: UIAlertController = UIAlertController(title: "Error", message: "\(error.localizedDescription)", preferredStyle: .alert) 
     self.window?.rootViewController?.present(importantAlert, animated: true, completion: nil) 

     return 
    } 
    let authentication = user.authentication 
    let credential = FIRGoogleAuthProvider.credential(withIDToken: (authentication?.idToken)!, 
                 accessToken: (authentication?.accessToken)!) 
    FIRAuth.auth()?.signIn(with: credential, completion: { [weak self](user, error1) in 
     if(error != nil){ 

      let importantAlert: UIAlertController = UIAlertController(title: "Error", message: "\(error1!.localizedDescription)", preferredStyle: .alert) 
      self?.window?.rootViewController?.present(importantAlert, animated: true, completion: nil) 
     } 
    }) 
} 

そして私は

Warning: Attempt to present <UIAlertController: 0x7f864b62bef0> on <SFSafariViewController: 0x7f864b40edb0> whose view is not in the window hierarchy! 

私は何をすべき次のエラーを取得しますか?

+0

をあなたの 'self.window .rootViewController.view'はあなたのビューではではないように思われます。 self.window?.rootViewControllerを設定してコードを投稿してください。 – shallowThought

+0

サインインメソッドを呼び出すView Controllerがあります。私は実際にはrootViewControllerを設定していません。 –

+0

ナビゲーションコントローラを使用していますか?また、ストーリーボード階層でQを更新してください。 – Dravidian

答えて

1

self.window?.rootViewControllerは、sign()を呼び出している瞬間に、SFSafariViewControllerとその.view is not in the window hierarchyと表示されています。 )>))

おそらくあなたは、符号(コール現時点では別のUIViewControllerを示している - あなたがプログラム的rootViewControllerを設定しない場合(、おそらく(左側に大きな矢印付きのViewControllerは(Storyboardでそれを設定します。

つのオプション:

1)あなたが持っていると仮定すると、それ

を呼び出しているUIViewControllerからsign()方法を移動UIViewControllerあなたを移動し、ログインのMyLoginViewController命名selfMyLoginViewControllerからAppDelegateと置き換えるself?.window?.rootViewController?から方法:

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

    if let error = error { 
     print() 
     let importantAlert: UIAlertController = UIAlertController(title: "Error", message: "\(error.localizedDescription)", preferredStyle: .alert) 
     self.present(importantAlert, animated: true, completion: nil) 

     return 
    } 
    let authentication = user.authentication 
    let credential = FIRGoogleAuthProvider.credential(withIDToken: (authentication?.idToken)!, 
                accessToken: (authentication?.accessToken)!) 
    FIRAuth.auth()?.signIn(with: credential, completion: { [weak self](user, error1) in 
    if(error != nil){ 

     let importantAlert: UIAlertController =  UIAlertController(title: "Error", message: "\(error1!.localizedDescription)", preferredStyle: .alert) 
      self.present(importantAlert, animated: true, completion: nil) 
     } 
    }) 
} 

2)パスのViewControllerあなたはAppDelegate、すなわち、グローバルな場所でsign()方法を維持したい場合は

、引数を追加引数として合格するためにあなたのsign()方法にUIViewCOntrollerを呼び出す:AppDelegateで

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

    if let error = error { 
     print() 
     let importantAlert: UIAlertController = UIAlertController(title: "Error", message: "\(error.localizedDescription)", preferredStyle: .alert) 
     viewController.present(importantAlert, animated: true, completion: nil) 

     return 
    } 
    let authentication = user.authentication 
    let credential = FIRGoogleAuthProvider.credential(withIDToken: (authentication?.idToken)!, 
                accessToken: (authentication?.accessToken)!) 
    FIRAuth.auth()?.signIn(with: credential, completion: { [weak self](user, error1) in 
    if(error != nil){ 

     let importantAlert: UIAlertController =  UIAlertController(title: "Error", message: "\(error1!.localizedDescription)", preferredStyle: .alert) 
      viewController.present(importantAlert, animated: true, completion: nil) 
     } 
    }) 
} 

と現在提示UIViewControllerでこのようにそれを呼び出す:?

sign(yourGIDSignIn, didSignInFor: yourGIDGoogleUser, presenting: self, withError: yourError) 
関連する問題