Ios GPPSignInDelegate的实现是否必须是ViewController或AppDelegate?
我正试图按照说明将我正在用Swift编写的iOS应用程序与Google Plus集成。在Ios GPPSignInDelegate的实现是否必须是ViewController或AppDelegate?,ios,google-plus,Ios,Google Plus,我正试图按照说明将我正在用Swift编写的iOS应用程序与Google Plus集成。在ViewController中实现所有功能后,我愚蠢地认为我可以将身份验证行为封装在它自己的类中(如下所示) 问题是两个GPPSignInDelegate协议函数finishedwithAuth和didDisconnectWithError从未被调用。如果我传入一个ViewController或实现GPPSignInDelegate的AppDelegate,并将其分配给googlePlus.delegate调
ViewController
中实现所有功能后,我愚蠢地认为我可以将身份验证行为封装在它自己的类中(如下所示)
问题是两个GPPSignInDelegate
协议函数finishedwithAuth
和didDisconnectWithError
从未被调用。如果我传入一个ViewController
或实现GPPSignInDelegate
的AppDelegate
,并将其分配给googlePlus.delegate
调用成功;当我将self
分配给代理时,永远不会调用回调
import Foundation
class GoogleAuthentication : GPPSignInDelegate {
let kClientId: NSString = ""
let googlePlus: GPPSignIn
var authenticatedSuccessfully: Bool?
// Passing in a ViewController implementing GPPSignInDelegate works
// init(delegate: GPPSignInDelegate) {
init() {
self.delegate = delegate
googlePlus = GPPSignIn.sharedInstance()
googlePlus.shouldFetchGooglePlusUser = true
googlePlus.clientID = kClientId
googlePlus.scopes = [kGTLAuthScopePlusLogin]
googlePlus.delegate = self // works when assignging delegate passed in
authenticatedSuccessfully = googlePlus.trySilentAuthentication()
}
func authenticate() {
googlePlus.authenticate()
}
func finishedWithAuth(auth: GTMOAuth2Authentication!, error: NSError!) {
println("finished with auth!")
}
func didDisconnectWithError(error: NSError!) {
println("disconnected with error!")
}
}
是否缺少某些内容,或者是否必须通过
ViewController
或AppDelegate
来实现gppsignedelegate
?如果是,为什么?委托对象必须从NSObject继承。因此,它变成:
class GoogleAuthentication: NSObject, GPPSignInDelegate {
我的类继承自NSObject,并符合GIDSignInDelegate、GIDSignInUIDelegate协议,但它仍然不起作用