由于未捕获的异常'NSInvalidArgumentException'而终止应用程序-iOS Google登录 [英] Terminating app due to uncaught exception 'NSInvalidArgumentException' - ios google sign in
问题描述
我正在实施本教程据我所见,作者写道:
在这些示例中,视图控制器是的子类. UIViewController.如果在您的项目中,实现的类 GIDSignInUIDelegate不是UIViewController的子类,实现 signInWillDispatch:error :、 signIn:presentViewController:和 signIn:dismissViewController:GIDSignInUIDelegate的方法 协议.
In these examples, the view controller is a subclass of UIViewController. If, in your project, the class that implements GIDSignInUIDelegate is not a subclass of UIViewController, implement the signInWillDispatch:error:, signIn:presentViewController:, and signIn:dismissViewController: methods of the GIDSignInUIDelegate protocol.
因此,按照他的建议,我写道:
so following his advices I wrote as follows:
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, GIDSignInDelegate {
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Initialize sign-in
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
GIDSignIn.sharedInstance().delegate = self
return true
}
// [START openurl]
func application(application: UIApplication,
openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: sourceApplication,
annotation: annotation)
}
// [END openurl]
@available(iOS 9.0, *)
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
return GIDSignIn.sharedInstance().handleURL(url,
sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String?,
annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}
// [START signin_handler]
func signIn(signIn: GIDSignIn!, didSignInForUser user: GIDGoogleUser!,
withError error: NSError!) {
if (error == nil) {
print("Signed in!")
} else {
print("\(error.localizedDescription)")
}
}
// [END signin_handler]
// [START disconnect_handler]
func signIn(signIn: GIDSignIn!, didDisconnectWithUser user:GIDGoogleUser!,
withError error: NSError!) {
// Perform any operations when the user disconnects from app here.
// [START_EXCLUDE]
NSNotificationCenter.defaultCenter().postNotificationName(
"ToggleAuthUINotification",
object: nil,
userInfo: ["statusText": "User has disconnected."])
// [END_EXCLUDE]
}
我还有一个名为LoginScreen.swift
的类,其中包含:
I also have a class called LoginScreen.swift
that contains:
import UIKit
class LoginScreen: UIViewController, GIDSignInUIDelegate {
override func viewDidLoad() {
super.viewDidLoad()
GIDSignIn.sharedInstance().uiDelegate = self
// Uncomment to automatically sign in the user.
GIDSignIn.sharedInstance().signInSilently()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().signInSilently()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func signInWillDispatch(signIn: GIDSignIn!, error: NSError!) {
print("Nothing!")
}
// Present a view that prompts the user to sign in with Google
func signIn(signIn: GIDSignIn!,
presentViewController viewController: UIViewController!) {
self.presentViewController(viewController, animated: true, completion: nil)
}
// Dismiss the "Sign in with Google" view
func signIn(signIn: GIDSignIn!,
dismissViewController viewController: UIViewController!) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
,当我运行该应用程序时,我看到按钮sign in with google
.但是当我单击它时,我看到了错误:
and when I run the app I see the button sign in with google
. But when I click it, I see the error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'When |allowsSignInWithWebView| is enabled, uiDelegate must
either be a |UIViewController| or implement the
|signIn:presentViewController:| and |signIn:dismissViewController:|
methods from |GIDSignInUIDelegate|.'
这是什么问题?我以为我包括了必要的方法...
What is the problem here? I thought I included the necessary methods...
============编辑-作为@ emrys57问题的后续解答:
============ EDIT - as a follow up to @emrys57 questions:
我在项目中附加了GoogleService-Info.plist
文件:
I have the GoogleService-Info.plist
file attached in my project:
当我注释掉3种方法(presentViewController
,dismissViewController
和signInWillDispatch
)时,没有任何变化-我仍然遇到相同的错误.
When I comment out the 3 methods (presentViewController
, dismissViewController
and signInWillDispatch
) nothing changes - I'm still getting the same error..
关于屏幕-这是我的故事板的外观:
About the screens - this is how my storyboard looks like:
并且我想在第三个屏幕上显示google登录按钮.
and I want to display the google log in button on the 3rd screen.
此处的第二个屏幕包含Tutorial
,其代码如下:
The second screen here contains a Tutorial
and its code is so far as follows:
import UIKit
class Tutorial: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let background = CAGradientLayer().greenBlue()
background.frame = self.view.bounds
self.view.layer.insertSublayer(background, atIndex: 0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
还有一件事情-当我打开应用程序时,我现在看到了google登录按钮:
And one more thing - when I turn on the app I see now the google login button:
当我单击它时-我遇到了提到的错误:
And when I click it - I'm getting the mentioned error:
2016-02-13 09:47:49.578 myapp[20870:800499] *** Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason: 'When
|allowsSignInWithWebView| is enabled, uiDelegate must either be a
|UIViewController| or implement the |signIn:presentViewController:| and
|signIn:dismissViewController:| methods from |GIDSignInUIDelegate|.'
*** First throw call stack:
(...)
所以我想当我按下按钮时LoginScreen正在运行...
so I guess the LoginScreen is running when I hit the button...
===========另一个
=========== another
当我将代码从LoginScreen
复制到类ViewController
并在其中添加按钮时-一切正常,我看到一个google登录按钮,可以登录!但是-该按钮出现在应用程序的第一个屏幕上,这不是我想要的.我希望它出现在第三个屏幕上(在教程之后).有人知道这是怎么回事,为什么我不能在第三个屏幕上放置登录按钮吗?
When I copied the code from my LoginScreen
to the class ViewController
and added button there - everything worked fine, I see a google sign in button and I can log in! BUT - the button appears on the first screen of the app and that's not what I want. I want it to appear on the 3rd screen (after the tutorial). Does anybody know what is going on here and why I can't put the login button on the 3rd screen?
推荐答案
我知道为时已晚,但是如果您将GIDSignIn.sharedInstance().uiDelegate = self
放入viewDidAppear而不是viewDidLoad,它将对其他人有所帮助!
I know it's too late but will help for others if you put the GIDSignIn.sharedInstance().uiDelegate = self
into viewDidAppear instead viewDidLoad, that's worked for me!
这篇关于由于未捕获的异常'NSInvalidArgumentException'而终止应用程序-iOS Google登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!