如何在swift中调用类

时间:2015-03-25 02:08:05

标签: ios swift

下面是我的班级。 如何用facebook调用autoLogin? 我试过但没有成功 让登录:LoginHelper = LoginHelper.sharedInstance login.autoLogin({},errorHandler:error:String(){}) 谢谢你的帮助!

class LoginHelper : NSObject, GPPSignInDelegate {

private let googleClientID = "myClientID"
private let userDefaultsLoginMethodKey = "LoginMethod"
private let userDefaultsAppTokenKey = "AppToken"

var googleSignIn: GPPSignIn

// google auto login is async, need to save handlers
var autoLoginCompletionHandler : (() -> Void)?
var autoLoginErrorHandler : ((err: String) -> Void)?

class var sharedInstance : LoginHelper {
    struct Singleton {
        static let instance : LoginHelper = LoginHelper()
    }
    return Singleton.instance
}

enum LoginMethod : String {

    case NotLoggedIn = "NotLoggedIn"
    case Facebook = "facebook"
    case Google = "google"
    case MyApp = "MyApp"

    func token() -> String? {
        switch (self) {
        case .Google:
            if LoginHelper.sharedInstance.googleSignIn.authentication != nil {
                return LoginHelper.sharedInstance.googleSignIn.authentication.accessToken
            }
        case .Facebook:
            if FBSession.activeSession().isOpen {
                if let tokenData = FBSession.activeSession().accessTokenData {
                    return tokenData.accessToken
                }
            }
        default:
            return nil
        }
        return nil
    }
}

var loginMethod : LoginMethod {

    get {
        let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
        if let method = defaults.stringForKey(userDefaultsLoginMethodKey) {
            if let loginMethod = LoginMethod(rawValue: method) {
                return loginMethod
            }
        }
        return LoginMethod.NotLoggedIn
    }
    set {
        let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
        defaults.setValue(newValue.rawValue, forKey: userDefaultsLoginMethodKey)
    }
}

var accessToken : String? {

    get {
        let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
        if let token = defaults.stringForKey(userDefaultsAppTokenKey) {
            return token
        }
        return nil
    }
    set {
        let defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
        defaults.setValue(newValue, forKey: userDefaultsAppTokenKey)
    }
}

private override init() {
    googleSignIn = GPPSignIn.sharedInstance()
    googleSignIn.clientID = googleClientID
    googleSignIn.shouldFetchGooglePlusUser = true
    googleSignIn.shouldFetchGoogleUserEmail = true
    googleSignIn.scopes = [kGTLAuthScopePlusLogin, kGTLAuthScopePlusUserinfoEmail]
}

func loginToServer(method : LoginMethod, completionHandler : (() -> Void)? = nil, errorHandler : ((err : String) -> Void)? = nil) {
    if let token = loginMethod.token() {
        // login to server with token
        println("login to server with token");
    }
}

func autoLogin(completionHandler : () -> Void, errorHandler : (err : String) -> Void) {

    switch (loginMethod) {

    case .Google:

        googleSignIn.delegate = self
        if googleSignIn.trySilentAuthentication() {
            // actual login will occur when finishedWithAuth is triggered,
            // keep completionHandler & errorHandler to be used later
            autoLoginCompletionHandler = completionHandler
            autoLoginErrorHandler = errorHandler
        } else {
            errorHandler(err: "silent authentication failed for Google")
        }

    case .Facebook:
        if FBSession.openActiveSessionWithAllowLoginUI(false) {
            loginToServer(loginMethod, completionHandler: completionHandler, errorHandler: errorHandler)
        } else {
            errorHandler(err: "failed to login automaticlly to Facebook")
        }
    default:
        println("Skip server login for \(loginMethod.rawValue)")
        completionHandler()
    }
}

func finishedWithAuth(auth: GTMOAuth2Authentication,  error: NSError! ) -> Void {
    if let err = error {
        println("Failed to login with Google")
        autoLoginErrorHandler?(err: "failed to login automaticlly to Google")
    } else {
        // login to server
        loginToServer(LoginMethod.Google, completionHandler: autoLoginCompletionHandler, errorHandler: autoLoginErrorHandler)
    }
}

func didDisconnectWithError(error: NSError!) -> Void{
}

}

我尝试过但没有成功 让登录:LoginHelper = LoginHelper.sharedInstance login.autoLogin({},errorHandler:error:String(){})

2 个答案:

答案 0 :(得分:0)

let login:LoginHelper = LoginHelper.sharedInstance 
login.autoLogin({}, errorHandler: { (err: String) -> Void in
    //Your error handling code here.
})

答案 1 :(得分:0)

var square = Rect(origin: Point(x: 0.0, y: 0.0),
size: Size(width: 10.0, height: 10.0))
let initialSquareCenter = square.center
square.center = Point(x: 15.0, y: 15.0)

关注链接:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-ID254