Ios AppDelegate发送响应swift 3

Ios AppDelegate发送响应swift 3,ios,swift,swift3,appdelegate,google-signin,Ios,Swift,Swift3,Appdelegate,Google Signin,我使用google signIn Api,当我在第一个视图中单击我的登录按钮时,我就可以进入google webview页面,在我使用performe for segue访问第二个视图后,我可以自己登录并获取数据。 当我尝试在第二个视图中注销时,他会打印我的字符串“deco”,然后我返回到我的第一页。但当我再次尝试登录时,他因以下错误崩溃: 由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“uiDelegate必须是 |UIViewControlle

我使用google signIn Api,当我在第一个视图中单击我的登录按钮时,我就可以进入google webview页面,在我使用performe for segue访问第二个视图后,我可以自己登录并获取数据。 当我尝试在第二个视图中注销时,他会打印我的字符串“deco”,然后我返回到我的第一页。但当我再次尝试登录时,他因以下错误崩溃:

由于未捕获异常而终止应用程序 “NSInvalidArgumentException”,原因:“uiDelegate必须是 |UIViewController |或实现|登录:presentViewController:| 和| signIn:dismissViewController:|来自的方法 |GIDSignInUIDelegate |'

我认为注销功能不起作用,而且我也不认为在appDelegate中使用performe for segue是最好的方法,您是否使用其他方法?(除通知外)

我的第一个视图控制器

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().disconnect()

我的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
}

看起来您将您的
ViewController
AppDelegate
都声明为实现
GIDSignInUIDelegate
,但您只将实现放在
AppDelegate

您应该决定是在其中一个中实现,还是在另一个中实现,并且只声明其中支持的协议。我猜您崩溃是因为
ViewController
是实际的代理:

 GIDSignIn.sharedInstance().uiDelegate = self
但它没有必要的方法。如果查看,可以看到它建议您在
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.
    // ...
}

当我没有在我的视图控制器中写入GIDSignIn.sharedInstance().uiDelegate=self时,我收到了一个带有相同消息的崩溃,但我甚至不能记录一次