【问题标题】:AppDelegate send response swift 3AppDelegate 快速发送响应 3
【发布时间】:2017-07-03 03:46:51
【问题描述】:

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

由于未捕获的异常而终止应用程序 'NSInvalidArgumentException',原因:'uiDelegate 必须是 |UIViewController|或实现 |signIn: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
}

【问题讨论】:

    标签: ios swift swift3 appdelegate google-signin


    【解决方案1】:

    看起来您将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.
        // ...
    }
    

    【讨论】:

    • 当我不在视图控制器中写 GIDSignIn.sharedInstance().uiDelegate = self 时,我遇到了同样的消息崩溃,但我什至无法登录
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 2020-09-28
    相关资源
    最近更新 更多