2014-09-18 13 views
7

Ho faticato a creare una funzione di login/registrazione di Facebook in modo rapido. stavo cercando un tutorial, ma non sono stato in grado di trovare nulla. Quindi ho provato a farlo da solo. Sembra funzionare, ma perché non salva il mio facebookName, Gender e l'immagine nel database? Lo sta salvando nel simulatore ma non quando sto usando il mio dispositivo ios 8?facebook parse iscriviti swift

sto ricevendo questo messaggio di log "firmato utente e login tramite Facebook!", E un nuovo utente viene aggiunto alla classe di analisi, ma non il nome, l'immagine e il sesso ...

@IBAction func login(sender: UIButton!) { 




    var permissionArray = ["user_about_me", "user_relationships", "user_birthday", "user_location"] 

    PFFacebookUtils.initializeFacebook() 

    PFFacebookUtils.logInWithPermissions(permissionArray, block: { (user: PFUser!, error: NSError!) in 
     println(user) 
     if user == nil { 
      println(error.localizedDescription) 


     } else { 



      if user.isNew { 



       var userQuery = PFUser.query() 
       userQuery.getObjectInBackgroundWithId(PFUser.currentUser().objectId) { 
        (userObject: PFObject!, error: NSError!) -> Void in 


        var fbRequest = FBRequest.requestForMe() 
        fbRequest.startWithCompletionHandler { (connection: FBRequestConnection!, result:AnyObject!, error: NSError!) in 


         if error == nil { 

          //FACEBOOK DATA IN DICTIONARY 
          var userData = result as NSDictionary 
          var faceBookId = userData.objectForKey("id") as NSString 
          var faceBookName = userData.objectForKey("first_name") as NSString 
          var faceBookMiddle = userData.objectForKey("middle_name") as NSString 
          var faceBookGender = userData.objectForKey("gender") as NSString 

          var url:NSURL = NSURL.URLWithString(NSString(format:"https://graph.facebook.com/%@/picture?width=320", faceBookId)) 
          var err: NSError? 
          var imageData :NSData = NSData.dataWithContentsOfURL(url, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err) 

          var imageFile = PFFile(name: "image.jpg", data: imageData) as PFFile 

          println(userData) 

          userObject.setObject(faceBookName, forKey: "name") 
          userObject.setObject(imageFile, forKey: "file") 
          userObject.setObject(faceBookGender, forKey: "gender") 

          userObject.saveInBackground() 



          var sharedInstance:userSingleton = userSingleton.sharedInstance 

          sharedInstance.userName = (userObject.objectForKey("name") as NSString) 
          sharedInstance.userGender = (userObject.objectForKey("gender") as NSString) 


          (userObject.objectForKey("file") as PFFile).getDataInBackgroundWithBlock { 
           (theImageData: NSData!, error: NSError!) -> Void in 

           println(error) 
           if error == nil { 

            sharedInstance.userImage = UIImage(data:theImageData) 
           } 
           self.performSegueWithIdentifier("LoginSegue", sender: self) 
          } 


         } 
        } 
       } 





       println("User signed up and logged in through Facebook!") 
      } else { 



       println("User logged in through Facebook!") 
      } 


     } 

    }) 

} 

} 

risposta

4

Supponendo di utilizzare Parse, ecco come lo faccio. Personalmente creare una classe Utils.swift dove ho messo tutte le cose che voglio riutilizzare (o che io non voglio avere tra le ViewControllers):

class Utils { 

    class func notLoggedIn() -> Bool { 
     let user = PFUser.currentUser() 
     // here I assume that a user must be linked to Facebook 
     return user == nil || !PFFacebookUtils.isLinkedWithUser(user) 
    } 
    class func loggedIn() -> Bool { 
     return !notLoggedIn() 
    } 


    class func logInWithFacebook() { 
     PFFacebookUtils.logInWithPermissions(["public_profile", "user_friends"]) { 
      (user: PFUser!, error: NSError!) -> Void in 
      if user == nil { 
       NSLog("The user cancelled the Facebook login (user is nil)") 
      } else { 
       NSLog("The user successfully logged in with Facebook (user is NOT nil)") 
       // HERE I SET A USER POINTER TO THE INSTALLATION 
       // That way we can send push notifications to specific users 
       if let installation = PFInstallation.currentInstallation() { 
        installation["user"] = PFUser.currentUser() 
        installation.saveEventually() 
       } 
       // THEN I GET THE USERNAME AND fbId 
       Utils.obtainUserNameAndFbId() 
      } 
     } 
    } 


    class func obtainUserNameAndFbId() { 
     if notLoggedIn() { 
      return 
     } 
     let user = PFUser.currentUser() // Won't be nil because is logged in 
     // RETURN IF WE ALREADY HAVE A USERNAME AND FBID 
     // Note that we check the fbId because Parse automatically fills in the username with random numbers 
     if let fbId = user["fbId"] as? String { 
      if !fbId.isEmpty { 
       println("we already have a username and fbId -> return") 
       return 
      } 
     } 
     // REQUEST TO FACEBOOK 
     println("performing request to FB for username and IDF...") 
     if let session = PFFacebookUtils.session() { 
      if session.isOpen { 
       println("session is open") 
       FBRequestConnection.startForMeWithCompletionHandler({ (connection: FBRequestConnection!, result: AnyObject!, error: NSError!) -> Void in 
        println("done me request") 
        if error != nil { 
         println("facebook me request - error is not nil :(") 
        } else { 
         println("facebook me request - error is nil :)") 
         println(result) 
         // You have 2 ways to access the result: 
         // 1) 
         println(result["name"]) 
         println(result["id"]) 
         // 2) 
         println(result.name) 
         println(result.objectID) 
         // Save to Parse: 
         PFUser.currentUser().username = result.name 
         PFUser.currentUser().setValue(result.objectID, forKey: "fbId") 
         PFUser.currentUser().saveEventually() // Always use saveEventually if you want to be sure that the save will succeed 
        } 
       }) 
      } 
     } 
    } 

} 

allora si può semplicemente chiamare Utils.logInWithFacebook() quando vuoi eseguire il login.

Nota che, poiché la richiesta di Facebook non può riuscire, non è garantito il salvataggio del nome utente e dell'ID di Facebook su Parse. Ecco perché creo il metodo Utils.obtainUserNameAndFbId(), che chiamo in application(_: didFinishLaunchingWithOptions) (può essere chiamato ogni avvio perché eseguirà la richiesta solo su FB finché non avrà esito positivo).