2017-02-14 90 views
0

我使用google signIn Api,当我点​​击我的第一个视图上的登录按钮时,我得到了google webview页面,我可以登录自己并获取我的数据,之后我使用performe for segue访问第二种观点。 当我尝试在第二个视图中注销时,他将我的字符串“deco”打印出来,然后回到第一页。但是,当我再次尝试登录自己,他这个错误崩溃:AppDelegate send reponse swift3

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

我想注销功能没有工作,而且我不认为使用performe在的appDelegate SEGUE是最好的梅索德,你有别的东西吗? (期望通知)

我firstViewController

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()。断开()

我的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 

}

回答

0

看起来像你宣布你的ViewController和你的AppDelegate作为执行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. 
    // ... 
} 
+0

当我不写GIDSignIn.sharedInstance()在我的视图控制器uiDelegate =自我,我崩溃与相同的消息,但我甚至不能登录一次 –