将委托方法提取到单独的类中?

时间:2018-01-15 20:29:16

标签: swift

目前正在使用facebook登录我的应用程序实施firebase。在我的LoginViewController中我很容易添加了facebook和firebase,但是我想知道是否可以将委托方法放到另一个类中?我已经看到了表格视图dataSource的教程。

如何将这三种方法转移到自己的班级?借口命名惯例,我刚刚意识到它听起来有多愚蠢

import UIKit
import FBSDKLoginKit
import Firebase

class LoginViewController: UIViewController, FBSDKLoginButtonDelegate {

@IBOutlet weak var fbLogin: FBSDKLoginButton!



override func viewDidLoad() {
    super.viewDidLoad()
    fbLogin.delegate = self
}

public func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
    if error != nil {
        print(error)
        return
    }

    let fbloginresult : FBSDKLoginManagerLoginResult = result

    if result.isCancelled {
        print("User clicked cancel")
        return
    }

    let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
    loginFirebase(with: credential);
}




public func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
    print("did log out of facebook")

    let firebaseAuth = Auth.auth()
    do {
        try firebaseAuth.signOut()
    } catch let signOutError as NSError {
        print ("Error signing out: %@", signOutError)
    }

}

func loginFirebase(with credential: AuthCredential) {
    print(credential)

    Auth.auth().signIn(with: credential) { (user, error) in
        if error != nil {
            if let errCode = AuthErrorCode(rawValue: error!._code) {
                switch errCode {
                    case .userDisabled:
                        print("User appears to be disabled, contact administrator")
                    case .networkError:
                        print("There appears to be an network error, try signing in again")
                    default:
                        print("Error \(errCode)")
                }
            }
        }
        print("Successfully logged in with facebook.")
    }
}
}

更新代码

Login View Controller

import UIKit
import FBSDKLoginKit
import Firebase

class LoginViewController: UIViewController {


    let facebookLogin = fbController()

    override func viewDidLoad() {
        super.viewDidLoad()
        facebookLogin.buildFacebookButton(with: view)
    }
}

fbLoginController

import FBSDKLoginKit
import Firebase


class fbController: NSObject, FBSDKLoginButtonDelegate {

    let facebookLogin = FBSDKLoginButton()

    override init() {
        super.init()
        facebookLogin.delegate = self
    }


    public func buildFacebookButton(with view: UIView ) {
        view.addSubview(facebookLogin)
        facebookLogin.frame = CGRect(x: 16 , y: 50, width: view.frame.width - 32, height: 50)
    }


    internal func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        if error != nil {
            print(error)
            return
        }
        let _ : FBSDKLoginManagerLoginResult = result

        if result.isCancelled {
            print("User clicked cancel")
            return
        }
        let credential = FacebookAuthProvider.credential(withAccessToken: FBSDKAccessToken.current().tokenString)
        loginFirebase(with: credential);
    }



    internal func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {
        print("did log out of facebook")
        let firebaseAuth = Auth.auth()
        do {
            try firebaseAuth.signOut()
        } catch let signOutError as NSError {
            print ("Error signing out: %@", signOutError)
        }

    }


    internal func loginFirebase(with credential: AuthCredential) {
        print(credential)
        Auth.auth().signIn(with: credential) { (user, error) in
            if error != nil {
                if let errCode = AuthErrorCode(rawValue: error!._code) {
                    switch errCode {
                    case .userDisabled:
                        print("User appears to be disabled, contact administrator")
                    case .networkError:
                        print("There appears to be an network error, try signing in again")
                    default:
                        print("Error \(errCode)")
                    }
                }
            }
            print("Successfully logged in with facebook.")
            print("made firebase account too?")
        }
    }
}

0 个答案:

没有答案