0

I wanted to known how to pass the values in result to another view controller.

func showEmailAddress() -> Void{
    //Adding user to firebase
    let accessToken = FBSDKAccessToken.current()
    guard let accessTokenString = accessToken?.tokenString else //This makes things a littie safer
    {return}

    let credentials = FacebookAuthProvider.credential(withAccessToken: accessTokenString)

    Auth.auth().signIn(with: credentials) { (user, error) in
        if error != nil {
            print("something went wrong with out FB user: ", error ?? "")
            return
        }
        print("Successfully logged in with our user: ", user ?? "")

        UserDefaults.standard.set(user!.email!, forKey: "usersigned")
        UserDefaults.standard.synchronize()

        let delegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate

        delegate.rememberLogin()
    }
    //print("Succesfully loged in with facebook...")
    FBSDKGraphRequest(graphPath: "/me", parameters: ["fields": "id, name, email"]).start { (connection, result, err) in
        //print(123)
        if err != nil{
            print("Failed to start graph request:",err ?? "")
            return
        }
        print("This is the results")
        print(result ?? "")

     let data:[String:AnyObject] = result as! [String : AnyObject]; print(data["name"]!); print(data["id"]!)

        var username = data["name"]!
        var userID = data["id"]!
        //let number = "1234566"
    }
}

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let DestViewController : menuViewController = segue.destination as! menuViewController
    DestViewController.id = username
}

The values are only existing inside the showEmailAddress(). Is there a way extract them from the function to send username and userID to a another VC?

Abe
  • 23
  • 3

4 Answers4

0

I hope this answer will be helpful as I wasn't sure what specific variables inside the showEmailAddress() function you were wishing to pass to another ViewController. I assume it's the email variable, but I wasn't sure as this variable doesn't need to be passed to the next ViewController as this ViewController can simply retrieve this value from UserDefaults.

Anyway, assuming you wish to pass the email variable (and that this variable cannot be retrieved from UserDefaults), my suggestion is to first make the email variable a property of the source controller class so that it can be accessed outside of the showEmailAddress() function()

The showEmailAddress() will set this property.

You also create a property on your destination controller for the email. In your prepare for segue method you then set this email property in a similar way to how you are assigning the username. eg

DestViewController.id = username
DestViewController.email = self.email!
Tom Wass
  • 149
  • 1
  • 5
0

You can opt to declare your var username ,var userID globally in your class. Once the you get username and userID value in your func showEmailAddress() assign the same to your globally declared username and userID. You can use these values anywhere in your class.

In your DestViewController declare two properties namely id and name. You can access both properties in your prepareForSegue: method and pass your globally defined data as explained by @Tom as

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let DestViewController : menuViewController = segue.destination as! menuViewController
    DestViewController.id = self.userID!
    DestViewController.name= self.username !
}

Hope it helps. Happy Coding!!

luckyShubhra
  • 2,731
  • 1
  • 12
  • 19
0

Make string variable in your AppDelegate Like

var username = String()
var userid = String()

and where you are getting value of username and userid sends its value to AppDelegate. make AppDelgate object like below

let appDelegate = UIApplication.shared.delegate as! AppDelegate

to pass value to app delegate variable

self.appDelegate.username = "your Value"
self.appDelegate.userid = "your Value"

and get your values wherever you want to fetch like below

let  string = appDelegate.username as! String
let  string1 = appDelegate.userid as! String

values passed

iOS Geek
  • 4,825
  • 1
  • 9
  • 30
0

Thank you guys so much! You guys were a lot of help! I ended up iOS geeks method it worked perfectly. I just had to update some of the syntax.

 let delegate : AppDelegate = UIApplication.shared.delegate as! AppDelegate

        delegate.userName = username
        delegate.userid = userID

and to fetch values in desired VC

var myDelegate = UIApplication.shared.delegate as! AppDelegate

And wherever values need I just used

let id = myDelegate.userid

I hope this is able to help anyone else. Thanks again guys!

Abe
  • 23
  • 3