我使用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/