我试图将函数设置为私有,试图在viewcontroller外部扩展AFViewControllerDelgate
,但没有任何效果。
只有didCancel在工作。看来他们更改了委托方法或其他方法。而且我不知道为什么没有Swift编写的AccountKit官方文档
下面是代码:
import UIKit
import AccountKit
import FBSDKCoreKit
// MARK: - LoginViewController: UIViewController
final class LoginViewController: UIViewController,
AKFViewControllerDelegate {
// MARK: Properties
fileprivate var accountKit = AKFAccountKit(responseType: .accessToken)
fileprivate var dataEntryViewController: AKFViewController? = nil
fileprivate var showAccountOnAppear = false
// MARK: View Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
showAccountOnAppear = accountKit.currentAccessToken != nil
dataEntryViewController = accountKit.viewControllerForLoginResume() as? AKFViewController
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if showAccountOnAppear {
showAccountOnAppear = false
presentWithSegueIdentifier(LoggedInViewController(), animated: animated)
} else if let viewController = dataEntryViewController {
if let viewController = viewController as? UIViewController {
present(viewController, animated: animated, completion: nil)
dataEntryViewController = nil
}
}
self.navigationController?.isNavigationBarHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.isNavigationBarHidden = false
}
// MARK: Actions
@IBAction func loginWithPhone(_ sender: AnyObject) {
FBSDKAppEvents.logEvent("loginWithPhone clicked")
if let viewController = accountKit.viewControllerForPhoneLogin() as? AKFViewController {
prepareDataEntryViewController(viewController)
if let viewController = viewController as? UIViewController {
present(viewController, animated: true, completion: nil)
}
}
}
@IBAction func loginWithEmail(_ sender: AnyObject) {
FBSDKAppEvents.logEvent("loginWithEmail clicked")
if let viewController = accountKit.viewControllerForEmailLogin() as? AKFViewController {
prepareDataEntryViewController(viewController)
if let viewController = viewController as? UIViewController {
present(viewController, animated: true, completion: nil)
}
}
}
// MARK: Helper Functions
func prepareDataEntryViewController(_ viewController: AKFViewController){
viewController.delegate = self
}
fileprivate func presentWithSegueIdentifier(_ segueIdentifier: UIViewController, animated: Bool) {
if animated {
present(segueIdentifier, animated: false)
} else {
UIView.performWithoutAnimation {
self.present(segueIdentifier, animated: true)
}
}
}
func viewController(viewController: UIViewController!, didCompleteLoginWithAccessToken accessToken: AKFAccessToken!, state: String!) {
print("did complete login with access token \(accessToken.tokenString) state \(state)")
//...
}
func viewController(_ viewController: (UIViewController & AKFViewController)!, didCompleteLoginWithAuthorizationCode code: String!, state: String!) {
print("Nem sei")
}
func viewController(_ viewController: (UIViewController & AKFViewController)!, didFailWithError error: Error!) {
// ... implement appropriate error handling ...
print("\(viewController) did fail with error: \(error.localizedDescription)")
}
func viewControllerDidCancel(_ viewController: (UIViewController & AKFViewController)!) {
print("Cancel")
}
}