0

I am new in iOS app development and I'm having a hard time trying to pass facebook user information between views. My app has 2 views. I was able to implement facebook login using this tutorial because as you might know they still dont give oficial support to swift.

This is my view controller:

//  ViewController.swift


import UIKit

class ViewController: UIViewController, FBLoginViewDelegate{

    @IBOutlet var fbLoginView : FBLoginView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.fbLoginView.delegate = self
        self.fbLoginView.readPermissions = ["public_profile", "email", "user_friends"]
    }

    // FACEBOOK DELEGATE METHODS
    func loginViewShowingLoggedInUser(loginView : FBLoginView!) {
        println("User Logged In")
        println("This is where you perform a segue.")
        self.performSegueWithIdentifier("gotoMenu", sender: self)

    }

    func loginViewFetchedUserInfo(loginView : FBLoginView!, user: FBGraphUser){
        println(user.name)
    }

    func loginViewShowingLoggedOutUser(loginView : FBLoginView!) {
        println("User Logged Out")
    }

    func loginView(loginView : FBLoginView!, handleError:NSError) {
        println("Error: \(handleError.localizedDescription)")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

This is the second view controller:

//  SecondViewController.swift

import UIKit

class SecondViewController: UIViewController {

    @IBOutlet weak var greeting: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

I need to show the user name in the second view and if possible his profile picture. How can I do this?

Thanks you very much Greeting nick

NMO
  • 293
  • 1
  • 10
  • There are various ways to achieve this. See the following thread which contains different ways to solve your problem. All in ObjectiveC, but most of them will work in Swift too. https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – Apfelsaft Aug 29 '14 at 06:33

1 Answers1

0

In your LoginViewController simply override prepareForSegue: like so...

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"gotoMenu"]) {
         SecondViewController *vc = segue.destinationViewController;
         vc.username = self.username; // This is where you pass the data!!
    }
}

I know this is on objective-C but all the method names are exactly the same in Swift.

DBoyer
  • 3,062
  • 4
  • 22
  • 32