ios - AppDelegate 发送响应 swift 3

标签 ios swift swift3 appdelegate google-signin

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

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|.'

我认为注销功能不起作用,而且我也不认为在appDelegate中使用performe进行segue是最好的方法,你用其他东西来做吗? (期待通知)

我的第一个ViewController

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()
}}

在我的第二个 View 中,我有同样的事情,唯一的变化是 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 中。

您应该决定是否要在其中之一中实现,并且仅声明那里支持的协议(protocol)。我的猜测是你崩溃是因为 ViewController是实际的委托(delegate):

 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.
    // ...
}

关于ios - AppDelegate 发送响应 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42233177/

相关文章:

ios - UITableView 不显示滚动

swift - 如何快速获取枚举名称而不是案例名称?

ios - 如何查看外围设备的 BLE 服务?

ios - 如何在 Swift 3 的 UIView 上添加右边框?

ios - 无法使用 Xcode 6.4 构建核心图

ios - 单个 ViewModel 的多个 View (iOS)

ios - AVPlayer 不播放,图层不显示

ios - 刷新 webview 上的标签栏项目

swift - 如何使用对考虑的位数的限制来搜索数组?

ios - UIColor 饱和度亮度值不同于颜色的常规饱和度亮度值