Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift-uiDelegate必须是UIViewController或实现signIn:presentViewController:&;签名:dismissViewController:方法_Ios_Swift_Google Login - Fatal编程技术网

Ios Swift-uiDelegate必须是UIViewController或实现signIn:presentViewController:&;签名:dismissViewController:方法

Ios Swift-uiDelegate必须是UIViewController或实现signIn:presentViewController:&;签名:dismissViewController:方法,ios,swift,google-login,Ios,Swift,Google Login,通过Crashlytics,我们在谷歌登录过程中收到了大量的崩溃报告。事故报告如下: Fatal Exception: NSInvalidArgumentException uiDelegate must either be a |UIViewController| or implement the |signIn:presentViewController:| and |signIn:dismissViewController:| methods from |GIDSignInUIDelega

通过
Crashlytics
,我们在谷歌登录过程中收到了大量的崩溃报告。事故报告如下:

Fatal Exception: NSInvalidArgumentException
uiDelegate must either be a |UIViewController| or implement the |signIn:presentViewController:| and |signIn:dismissViewController:| methods from |GIDSignInUIDelegate|.

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x1837f2db0 __exceptionPreprocess
1  libobjc.A.dylib                0x182e57f80 objc_exception_throw
2  CoreFoundation                 0x1837f2cf8 -[NSException initWithCoder:]
3  Hello English                  0x100583d18 -[GIDSignIn assertValidUIDelegate] (GIDSignIn.m:512)
4  Hello English                  0x1005861dc -[GIDSignIn signInWithOptions:] (GIDSignIn.m:961)
5  Hello English                  0x10058a0e4 -[GIDSignInButton pressed] (GIDSignInButton.m:385)
6  UIKit                          0x188988be8 -[UIApplication sendAction:to:from:forEvent:]
7  UIKit                          0x188988b64 -[UIControl sendAction:to:forEvent:]
8  UIKit                          0x188970870 -[UIControl _sendActionsForEvents:withEvent:]
9  UIKit                          0x188988454 -[UIControl touchesEnded:withEvent:]
10 UIKit                          0x188988084 -[UIWindow _sendTouchesForEvent:]
11 UIKit                          0x188980c20 -[UIWindow sendEvent:]
12 UIKit                          0x18895104c -[UIApplication sendEvent:]
13 UIKit                          0x18894f628 _UIApplicationHandleEventQueue
14 CoreFoundation                 0x1837a909c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
15 CoreFoundation                 0x1837a8b30 __CFRunLoopDoSources0
16 CoreFoundation                 0x1837a6830 __CFRunLoopRun
17 CoreFoundation                 0x1836d0c50 CFRunLoopRunSpecific
18 GraphicsServices               0x184fb8088 GSEventRunModal
19 UIKit                          0x1889ba088 UIApplicationMain
20 Hello English                  0x10029b2cc main (AppDelegate.swift:26)
21 libdispatch.dylib              0x18326e8b8 (Missing)
到目前为止,我们已经尝试:

  • GIDSignIn.sharedInstance().uiDelegate=self
    viewDidLoad()
    移动到
    viewdidDisplay(动画:Bool)
  • 我们还实现了
    GIDSignInUIDelegate
    的所有三种方法,但都没有成功
  • 我们的视图控制器不是
    UIViewController
    的直接子对象,但现在它是,崩溃仍在统计中
  • 我们使用的是简单的
    UIButton
    ,所以我们认为这可能是崩溃的原因(虽然不合逻辑,但我们尝试过);我们将其更改为
    GIDSignInButton
    ,但没有减少崩溃计数
  • 目前,我们已经删除了
    谷歌登录
    过程,但这不是解决方案。奇怪的是,我们无法在我们的终点重现这场崩溃;我们只在
    Crashlytics
    上得到这些崩溃


    有人有什么解决办法吗?

    经过努力终于找到了解决办法。 我的环境是

    Swift 3

    iOS 10.3.x

    代码8.3.2

    在此发布完整的解决方案

    import UIKit
    import GoogleSignIn
    
    class ViewController: UIViewController, GIDSignInUIDelegate, GIDSignInDelegate {
    
        @IBOutlet weak var signInButton: UIButton!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // set delegates
            GIDSignIn.sharedInstance().delegate = self
            GIDSignIn.sharedInstance().uiDelegate = self
    
            let buttonFrame : CGRect = CGRect.init(x: 0, y: 0, width: 100, height: 50)
            let gdSignInButton = GIDSignInButton.init(frame:buttonFrame)
            gdSignInButton.center = view.center
            view.addSubview(gdSignInButton)
    
            //  gdSignInButton.addTarget(self, action: #selector(self.didTapSignOut(sender: self.gdSignInButton)), for: UIControlEvents.touchUpInside)
        }
    
        @IBAction func didTapSignOut(sender: GIDSignInButton) {
            GIDSignIn.sharedInstance().signOut()
        }
    
        func sign(inWillDispatch signIn: GIDSignIn!, error: Error!) {
    
            guard error == nil else {
    
                print("Error while trying to redirect : \(error)")
                return
            }
    
            print("Successful Redirection")
        }
    
    
        //MARK: GIDSignIn Delegate
    
        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!
                print("User id is \(userId)")
    
                let idToken = user.authentication.idToken // Safe to send to the server
                print("Authentication idToken is \(idToken)")
                let fullName = user.profile.name
                print("User full name is \(fullName)")
                let givenName = user.profile.givenName
                print("User given profile name is \(givenName)")
                let familyName = user.profile.familyName
                print("User family name is \(familyName)")
                let email = user.profile.email
                print("User email address is \(email)")
                // ...
            } else {
                print("ERROR ::\(error.localizedDescription)")
            }
        }
    
        // Finished disconnecting |user| from the app successfully if |error| is |nil|.
        public func sign(_ signIn: GIDSignIn!, didDisconnectWith user: GIDGoogleUser!, withError error: Error!)
        {
    
        }
    
    
    
    }
    
    我的AppDelegate看起来像这样

    import UIKit
    import Google
    import GoogleSignIn
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
    
        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
            // Initialize sign-in
            var configureError: NSError?
            GGLContext.sharedInstance().configureWithError(&configureError)
            assert(configureError == nil, "Error configuring Google services: \(configureError)")
    
    
    
            return true
        }
    
        func applicationWillResignActive(_ application: UIApplication) {
            // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
            // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
        }
    
        func applicationDidEnterBackground(_ application: UIApplication) {
            // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
            // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
        }
    
        func applicationWillEnterForeground(_ application: UIApplication) {
            // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
        }
    
        func applicationDidBecomeActive(_ application: UIApplication) {
            // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        }
    
        func applicationWillTerminate(_ application: UIApplication) {
            // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        }
    
    
        func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
            return GIDSignIn.sharedInstance().handle(url as URL!,
                                                     sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
        }
    
    
    }
    
    我的解决方案(iOS 10.0)是将我的视图控制器指定为其ViewDidDisplay:method中的ui委托

    -(void)viewDidAppear{
        [super viewDidAppear];
        [GIDSignIn sharedInstance].uiDelegate = self;
    }
    

    您需要正确地实现Google和firebase的委托
    像这样加上

    class ViewController: UIViewController, GIDSignInDelegate,GIDSignInUIDelegate    
    
    在ViewDidLoad中添加这些行

    GIDSignIn.sharedInstance().uiDelegate = self
    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().signIn()
    

    只需包括以下内容

    func sign(_ signIn: GIDSignIn!, dismiss viewController: UIViewController!) {
        print("dismissing Google SignIn")
    }
    
    func sign(_ signIn: GIDSignIn!, present viewController: UIViewController!) {
        print("presenting Google SignIn")
    }
    

    我也遇到了这个错误,我在两个案例中观察到了它:

  • 我为GoogleSignIn按钮创建了一个@iAction,其中包括以下两行:

    @iAction func googleSignInButton(uu发件人:任意) { GIDSignIn.sharedInstance().signIn() GIDSignIn.sharedInstance().uiDelegate=self }

  • 但是uiDelegate的分配需要在viewDidLoad中,而不是在@iAction中:

    GIDSignIn.sharedInstance().uiDelegate = self
    
  • 在我的另一个例子中,uiDelegate是UIViewController,因此我不需要指定其他方法。和这里的其他海报一样,我确信我遵循了所有的方向,但每当我点击谷歌登录按钮时,仍然会看到这个错误
  • 根本原因是我创建了一个新的UIViewController类“DoLoginVC.swift”,但我没有将这个自定义类映射到故事板中的UIViewController!疏忽。在我添加自定义类映射之后,一切都开始工作了

    如果看到此错误,请转到情节提要并转到ViewController,并验证是否已指定自定义类,如所附屏幕截图中所示


    您需要在
    视图中将这些行写入
    UIViewController
    的方法

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(true)
    
        GIDSignIn.sharedInstance().uiDelegate = self
        GIDSignIn.sharedInstance().delegate = self
    }
    

    你需要将这行代码添加到ViewDid中,这对我很有用

    override    func    viewDidAppear(_ animated: Bool) {
            GIDSignIn.sharedInstance().delegate    =    self
            GIDSignIn.sharedInstance().uiDelegate    =    self
        }
    

    这仅仅意味着您必须实现以下委托方法:

    GIDSignIn.sharedInstance().delegate = self
    GIDSignIn.sharedInstance().uiDelegate = self
    
    您可以在
    viewDidLoad()
    中进行设置


    特例
    如果您在多个ViewController中使用Google登录(例如,在注册和登录屏幕中),则必须在屏幕更改时更新代理。为了避免冲突,我建议在
    viewdide()中设置它。如果您前后切换屏幕,这将使用当前ViewController更新代理引用。

    如果您忘记将自定义视图控制器类设置为UIViewController,则也会显示此错误

    要执行此操作->转到脚本->选择视图控制器->转到标识资源管理器 如果为空,则将您的类设置为空

    参考下图


    谷歌登录SDK 5.0及以上版本的
    GIDSignInUIDelegate
    已被撤销

    所以不是

    GIDSignIn.sharedInstance().uiDelegate = self
    
    在下面一行添加此项,以符合显示登录视图的要求

    GIDSignIn.sharedInstance()?.presentingViewController = self
    

    你为什么不定义代理?看看编辑,我们也试过了。看看这个@Anbu.Karthik,我们试过的解决方案来自同一个链接。嘿@Ravi Sisodia,你可以找到解决方案。我面临这个问题。你的解决方案有什么不同?您在哪里调用了
    GIDSignIn.sharedInstance().signIn()