我正在尝试创建一个类来处理我项目中的所有登录服务。除了谷歌登录外,一切都运行良好。
我希望在Google文档中提到的不是在UIVIewController中实现Google登录,您可以使用GIDSignInDelegate
执行此操作,并在您的类不是UIViewController时使用它。
我的课程已从NSObject
延长,并希望在该服务上处理Google SignIn,之后我只想调用一种方法来执行此操作。
var ls: LoginService = LoginService.instance
ls.doGoogleLogin()
这是我的LoginService:
struct LOGIN_URL {
static let LOGIN: String = "login"
}
protocol LoginServiceDelegate: class {
func loginSuccess(loginResponse: User)
func loginFailed(message: String)
}
class LoginService: NSObject, GIDSignInDelegate{
static let instance = LoginService()
weak var delegate: LoginServiceDelegate?
var user: User?
func doGoogleLogin(){
var configureError: NSError?
GGLContext.sharedInstance().configureWithError(&configureError)
assert(configureError == nil, "Error configuring Google services: \(configureError)")
GIDSignIn.sharedInstance().delegate = self
// GIDSignIn.sharedInstance().uiDelegate = self
GIDSignIn.sharedInstance().signIn()
}
public func sign(_ signIn: GIDSignIn!, didSignInFor user: GIDGoogleUser!, withError error: Error!) {
print(user.profile.email)
}
func signIn(signIn: GIDSignIn!,
presentViewController viewController: UIViewController!) {
self.presentViewController(viewController, animated: true, completion: nil)
}
func signIn(signIn: GIDSignIn!,
dismissViewController viewController: UIViewController!) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
当我使用此代码时出现错误
value of type LoginService has no member presentViewController
这里应该做什么不会出现此错误?
答案 0 :(得分:1)
通过键入self.presentViewController
,您说self
引用的类是可以使用方法presentViewController
的对象类型。在这种情况下,当您说self
时,您正在引用LoginService
实例。 LoginService
继承自NSObject
,这意味着它不是可以调用该方法的View Controller
或Navigation Controller
对象。
我认为您需要在LoginService类的顶部添加委托方法。
protocol LoginServiceDelegate: class {
func loginSuccess(loginResponse: User)
func loginFailed(message: String)
func googleSignInLaunch(_ viewController: UIViewController)
func googleSignInDismiss(_ viewController: UIViewController)
}
然后当你打电话:
func signIn(signIn: GIDSignIn!, presentViewController viewController: UIViewController!) {
//call delegate method here
//which will trigger view controller calling this to launch something
delegate?.googleSignInLaunch(_ viewController: UIViewController)
}
func signIn(signIn: GIDSignIn!,
dismissViewController viewController: UIViewController!) {
delegate?.googleSignInDismiss(_ viewController: UIViewController)
}
然后在View Controller中调用您的登录服务:
class MyViewController: UIViewController {
let loginService = LoginService()
override func viewDidLoad() {
loginService.delegate = self
loginService.doGoogleLogin()
}
func googleSignInLaunch(_ viewController: UIViewController) {
self.presentViewController(viewController, animated: true, completion: nil)
}
func googleSignInDismiss(_ viewController: UIViewController) {
self.dismissViewControllerAnimated(true, completion: nil)
}
//the methods below are the other two methods that were in your LoginService protocol
func loginSuccess(loginResponse: User) {
//whatever you need to do
}
func loginFailed(message: String) {
//whatever you need to do
}
}
答案 1 :(得分:0)
您的LoginService
课程不是UIViewController
,因此正在运行presentViewController
无效。尝试使用以下代码获取最顶层的ViewController,并在该ViewController上调用presentViewController
。
if var topController = UIApplication.shared().keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
// Call presentViewController on topController
}