2017-02-14 98 views
0

我使用google signIn Api,當我點​​擊我的第一個視圖上的登錄按鈕時,我得到了google webview頁面,我可以登錄自己並獲取我的數據,之後我使用performe for segue訪問第二種觀點。 當我嘗試在第二個視圖中註銷時,他將我的字符串「deco」打印出來,然後回到第一頁。但是,當我再次嘗試登錄自己,他這個錯誤崩潰:AppDelegate send reponse swift3

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'uiDelegate must either be a |UIViewController| or implement the |signIn:presentViewController:| and |signIn:dismissViewController:| methods from |GIDSignInUIDelegate|.'

我想註銷功能沒有工作,而且我不認爲使用performe在的appDelegate SEGUE是最好的梅索德,你有別的東西嗎? (期望通知)

我firstViewController

class ViewController: UIViewController, GIDSignInUIDelegate{ 

override func viewDidLoad() { 
    super.viewDidLoad() 
    GIDSignIn.sharedInstance().uiDelegate = self 
    // Do any additional setup after loading the view, typically from a nib. 
} 

@IBAction func btn(_ sender: Any) { 
    GIDSignIn.sharedInstance().signIn() 
}} 

在我的第二個觀點我也有同樣的事情,唯一的變化是GIDSignIn.sharedInstance()。斷開()

我的appDelegate包含

class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate { 

var window: UIWindow? 


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
    // Override point for customization after application launch. 
    var configureError : NSError? 
    GGLContext.sharedInstance().configureWithError(&configureError) 
    if (configureError != nil) 
    { 
      print("We have an error ! \(configureError)") 
    } 
    else 
    { 
     print("Google ready sir !") 

    } 
    GIDSignIn.sharedInstance().delegate = self 

    return true 
} 

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]) 
} 
func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) 
{ 
    if (error == nil) { 
     // Perform any operations on signed in user here. 
     let userId = user.userID     // For client-side use only! 
     let idToken = user.authentication.accessToken // Safe to send to the server 
     let fullName = user.profile.name 
     let givenName = user.profile.givenName 
     let familyName = user.profile.familyName 
     let email = user.profile.email 
     print("userId =>\(userId)") 
     print("idToken =>\(idToken)") 
     print("fullName =>\(fullName)") 
     print(" familyName=>\(familyName)") 
     print(" givenName=>\(givenName)") 
     print("email =>\(email)") 
     print("info => \(user.authentication)") 
     guard let rvc = self.window?.rootViewController as? ViewController 
      else { 
       return 
     } 
     rvc.performSegue(withIdentifier: "test", sender: nil) 



    } else { 
     print("\(error.localizedDescription)") 
    } 
} 



func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!) 
{ 
    print("Deco") 
} 
// Other func not usefull 

}

回答

0

看起來像你宣佈你的ViewController和你的AppDelegate作爲執行GIDSignInUIDelegate但你只能把執行在AppDelegate

您應該決定是否要在其中一個或另一箇中實現,並且只聲明在那裏支持的協議。我的猜測是,你正在崩潰,因爲ViewController是實際委託:

GIDSignIn.sharedInstance().uiDelegate = self 

,但它不具備所需的方法。如果你看看Google docs here,你可以看到它建議你在AppDelegate執行。這包含許多步驟。第一步是將uiDelegate設置爲您的AppDelegate實例。喜歡的東西:

GIDSignIn.sharedInstance().uiDelegate = UIApplication.shared.delegate as? GIDSignInUIDelegate 

然後添加實現:

func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!, 
    withError error: NSError!) { 
    if (error == nil) { 
     // Perform any operations on signed in user here. 
     let userId = user.userID     // For client-side use only! 
     let idToken = user.authentication.idToken // Safe to send to the server 
     let fullName = user.profile.name 
     let givenName = user.profile.givenName 
     let familyName = user.profile.familyName 
     let email = user.profile.email 
     // ... 
    } else { 
     print("\(error.localizedDescription)") 
    } 
} 

func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!, 
    withError error: NSError!) { 
    // Perform any operations when the user disconnects from app here. 
    // ... 
} 
+0

當我不寫GIDSignIn.sharedInstance()在我的視圖控制器uiDelegate =自我,我崩潰與相同的消息,但我甚至不能登錄一次 –