facebook登录后如何去另一个viewcontroller?

时间:2017-03-11 21:10:18

标签: ios swift xcode

登录facebook后如何转到另一个viewcontroller?应该在Main.storyboard中做什么?

这是我的AppDelegate.swift

import UIKit

import FBSDKCoreKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

        return true
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

        return FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as! String!, annotation: options[UIApplicationOpenURLOptionsKey.annotation])
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        FBSDKAppEvents.activateApp()
    }

}

这是ViewController.swift

import UIKit
import FBSDKLoginKit

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        let loginButton = FBSDKLoginButton()
        loginButton.center = self.view.center
        view.addSubview(loginButton)
    }

    func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {

        if error != nil {
         print("Something went wrong... \(error)" )
            return
        }

        print("Successfully logged in!")
    }

    func loginButtonDidLogOut(_ loginButton: FBSDKLoginButton!) {

        print("Successfully logged out!")
    }
}

2 个答案:

答案 0 :(得分:0)

你可能正在寻找这样的东西:

if(FBSDKAccessToken.currentAccessToken() != nil) {
    let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
    let vc = storyboard.instantiateViewController(withIdentifier: "myViewController")
    self.navigationController?.push(vc, animated: true)
}

该片段的作用是检查用户是否通过Facebook登录(我假设您正在使用SDK),如果是,则从故事板中实例化VC,并使用导航控制器推送它。你想把它放在哪里,无论你是想以不同的方式呈现VC等等,都取决于你,因为你的应用程序的流量会大量通知它。

答案 1 :(得分:0)

在ViewController.swift中尝试这个:

func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
        if error == nil && !result.isCancelled{
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            if let vc = storyboard.instantiateInitialViewController(){
                self.present(vc, animated: true, completion: nil)
            }
        }
    }