AppDelegate发送响应swift3

时间:2017-02-14 18:03:35

标签: ios swift swift3 appdelegate google-signin

我使用谷歌登录Api,当我点​​击我的第一个视图上的登录按钮时,我得到了谷歌webview页面,我可以自己登录并获取我的数据,之后我使用performe for segue访问第二个视图。 当我尝试在第二个视图中注销时,他打印我的字符串“deco”,然后我回到我的第一页。但是当我再次尝试重新登录时,他会因此错误而崩溃:

  

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

我认为注销功能不起作用,而且我认为在appDelegate中使用performe for 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()。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

}

1 个答案:

答案 0 :(得分:0)

您似乎已将ViewControllerAppDelegate声明为实施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.
    // ...
}