rohittigga

Untitled

Oct 19th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.94 KB | None | 0 0
  1. import UIKit
  2. import FBSDKCoreKit
  3. import FBSDKLoginKit
  4. import Firebase
  5. import FirebaseDatabase
  6.  
  7. class TutorialViewController: UIViewController {
  8.    
  9.     @IBOutlet var containerView: UIView!
  10.     @IBOutlet var pageControl: UIPageControl!
  11.     @IBOutlet var facebookButton: UIButton!
  12.    
  13.     //this dict stores the facebook user object
  14.     var dict : [String : AnyObject]!
  15.    
  16.     //firebase
  17.     var ref: FIRDatabaseReference!
  18.    
  19.     //keys below
  20.     struct defaultsKeys {
  21.         static let facebookID = "facebookID"
  22.     }
  23.  
  24.     override func viewDidLoad() {
  25.         super.viewDidLoad()
  26.         facebookButton.layer.cornerRadius = 18
  27.         pageControl.addTarget(self, action: #selector(TutorialViewController.didChangePageControlValue), for: .valueChanged)
  28.     }
  29.    
  30.     //facebook functions below
  31.     @IBAction func fbLoginButtonPressed(_ sender: AnyObject) {
  32.         let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()
  33.         fbLoginManager.logIn(withReadPermissions: ["email", "public_profile", "user_friends", "user_location"], from: self) { (result, error) in
  34.             print("Here is the result", result)
  35.             print("Here is the error" , error)
  36.            
  37.             // To update constraints programmatically
  38.             //self.view.layoutIfNeeded()
  39.            
  40.             if ( error == nil ){
  41.                 print("the error is nil")
  42.                 let fbloginresult : FBSDKLoginManagerLoginResult = result!
  43.                 //commenting if statement out temporarily for debugging purposes
  44.                
  45.                 if(fbloginresult.grantedPermissions.contains("email"))
  46.                 {
  47.                     print("email is contained and printing result")
  48.                     self.getFBUserData()
  49.                     fbLoginManager.logOut()
  50.                    
  51. //                    func instantiateFromStoryboard()
  52. //                    func instantiateFromStoryboard(storyboardIdentifier: String!)
  53. //                    NSStringFromClass(aClass: AnyClass)
  54.                    
  55.                     //figure out to how to resolve "whose view is not in the window hierarchy!"
  56.                     let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
  57.                     let nextViewController = storyBoard.instantiateViewController(withIdentifier: "VerifyLinkedInViewController") as! UIViewController
  58. //                    self.present(nextViewController, animated:true, completion:nil)
  59.                     UIApplication.shared.keyWindow?.rootViewController?.present(nextViewController, animated: true, completion: nil)
  60.  
  61.  
  62.                 }
  63.             }
  64.         }
  65.     }
  66.    
  67.     func getFBUserData(){
  68.         if((FBSDKAccessToken.current()) != nil){
  69.             FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, picture.type(large), email"]).start(completionHandler: { (connection, result, error) -> Void in
  70.                
  71.                 print("inside get FB user data")
  72.                 if (error == nil){
  73.                     self.dict = result as! [String : AnyObject]
  74.                     print(result!)
  75.                     print(self.dict)
  76.                    
  77.                     //firebase storing user
  78.                     self.ref = FIRDatabase.database().reference()
  79.                    
  80.                     //or
  81.                     if let myDictionary = self.dict as? [String : AnyObject] {
  82.                         let id = myDictionary["id"]
  83.                         let defaults = UserDefaults.standard
  84.                         defaults.setValue(id, forKey: defaultsKeys.facebookID)
  85.                         defaults.synchronize()
  86.                         self.ref.child("users").child(id as! String).setValue(self.dict)
  87.                         self.ref.child("users").child((id as! String)+"/largePictureURL").setValue("http://graph.facebook.com/"+(id as! String)+"/picture?width=1080&height=1080")
  88.                        
  89.                     }
  90.                 }
  91.             })
  92.         }
  93.     }
  94.     var tutorialPageViewController: TutorialPageViewController? {
  95.         didSet {
  96.             tutorialPageViewController?.tutorialDelegate = self
  97.         }
  98.     }
  99.  
  100.     /**
  101.      Fired when the user taps on the pageControl to change its current page.
  102.      */
  103.     func didChangePageControlValue() {
  104.         tutorialPageViewController?.scrollToViewController(index: pageControl.currentPage)
  105.     }
  106. }
  107.  
  108. extension TutorialViewController: TutorialPageViewControllerDelegate {
  109.    
  110.     @objc(tutorialPageViewController:didUpdatePageIndex:) func tutorialPageViewController(_ tutorialPageViewController: TutorialPageViewController,didUpdatePageIndex count: Int) {
  111.         pageControl.numberOfPages = count
  112.     }
  113.    
  114.     @objc(tutorialPageViewController:didUpdatePageCount:) func tutorialPageViewController(_ tutorialPageViewController: TutorialPageViewController,didUpdatePageCount index: Int) {
  115.         pageControl.currentPage = index
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment