Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import FBSDKCoreKit
- import FBSDKLoginKit
- import Firebase
- import FirebaseDatabase
- class TutorialViewController: UIViewController {
- @IBOutlet var containerView: UIView!
- @IBOutlet var pageControl: UIPageControl!
- @IBOutlet var facebookButton: UIButton!
- //this dict stores the facebook user object
- var dict : [String : AnyObject]!
- //firebase
- var ref: FIRDatabaseReference!
- //keys below
- struct defaultsKeys {
- static let facebookID = "facebookID"
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- facebookButton.layer.cornerRadius = 18
- pageControl.addTarget(self, action: #selector(TutorialViewController.didChangePageControlValue), for: .valueChanged)
- }
- //facebook functions below
- @IBAction func fbLoginButtonPressed(_ sender: AnyObject) {
- let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
- fbLoginManager.logIn(withReadPermissions: ["email", "public_profile", "user_friends", "user_location"], from: self) { (result, error) in
- print("Here is the result", result)
- print("Here is the error" , error)
- // To update constraints programmatically
- //self.view.layoutIfNeeded()
- if ( error == nil ){
- print("the error is nil")
- let fbloginresult : FBSDKLoginManagerLoginResult = result!
- //commenting if statement out temporarily for debugging purposes
- if(fbloginresult.grantedPermissions.contains("email"))
- {
- print("email is contained and printing result")
- self.getFBUserData()
- fbLoginManager.logOut()
- // func instantiateFromStoryboard()
- // func instantiateFromStoryboard(storyboardIdentifier: String!)
- // NSStringFromClass(aClass: AnyClass)
- //figure out to how to resolve "whose view is not in the window hierarchy!"
- let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
- let nextViewController = storyBoard.instantiateViewController(withIdentifier: "VerifyLinkedInViewController") as! UIViewController
- // self.present(nextViewController, animated:true, completion:nil)
- UIApplication.shared.keyWindow?.rootViewController?.present(nextViewController, animated: true, completion: nil)
- }
- }
- }
- }
- func getFBUserData(){
- if((FBSDKAccessToken.current()) != nil){
- FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
- print("inside get FB user data")
- if (error == nil){
- self.dict = result as! [String : AnyObject]
- print(result!)
- print(self.dict)
- //firebase storing user
- self.ref = FIRDatabase.database().reference()
- //or
- if let myDictionary = self.dict as? [String : AnyObject] {
- let id = myDictionary["id"]
- let defaults = UserDefaults.standard
- defaults.setValue(id, forKey: defaultsKeys.facebookID)
- defaults.synchronize()
- self.ref.child("users").child(id as! String).setValue(self.dict)
- self.ref.child("users").child((id as! String)+"/largePictureURL").setValue("http://graph.facebook.com/"+(id as! String)+"/picture?width=1080&height=1080")
- }
- }
- })
- }
- }
- var tutorialPageViewController: TutorialPageViewController? {
- didSet {
- tutorialPageViewController?.tutorialDelegate = self
- }
- }
- /**
- Fired when the user taps on the pageControl to change its current page.
- */
- func didChangePageControlValue() {
- tutorialPageViewController?.scrollToViewController(index: pageControl.currentPage)
- }
- }
- extension TutorialViewController: TutorialPageViewControllerDelegate {
- @objc(tutorialPageViewController:didUpdatePageIndex:) func tutorialPageViewController(_ tutorialPageViewController: TutorialPageViewController,didUpdatePageIndex count: Int) {
- pageControl.numberOfPages = count
- }
- @objc(tutorialPageViewController:didUpdatePageCount:) func tutorialPageViewController(_ tutorialPageViewController: TutorialPageViewController,didUpdatePageCount index: Int) {
- pageControl.currentPage = index
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment