2016-12-07 169 views
1

我想創建一個類來處理我的項目上的所有登錄服務。除谷歌登錄之外,一切工作都正常。谷歌登錄不在UIVIewController

我想要在Google文檔中提到的Google登錄不在UIVIewController中,您可以使用GIDSignInDelegate來完成此操作,並在您的類不是UIViewController時使用它。

我的課程從NSObject延伸,並希望在該服務上處理Google SignIn,之後我只需調用一種方法即可完成此操作。

var ls: LoginService = LoginService.instance 
ls.doGoogleLogin() 

這裏是我的login服務:

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

我應該在這裏做的沒有得到這個錯誤?

回答

1

通過鍵入self.presentViewController,你說的是self被引用的類是可以使用的方法presentViewController的對象類型。在這種情況下,當您說self時,您引用了LoginService實例。 LoginService繼承自NSObject,這意味着它不是可以調用該方法的對象,如View ControllerNavigation 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) 
} 

然後在視圖控制器調用您的登錄服務:

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 
    } 

} 
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 
}