2016-03-27 2 views
0

Parse.comサービスを使用している私のアプリケーションに非常にシンプルな機能を提供しようとしています。私が必要とするのは、ユーザーがFacebook経由でアカウントを作成し、Facebook経由で再度ログインできるようにすることだけです。PFFacebookUtils。ログインとサインアップの違いは?

問題は、PFFacebookUtilsログイン方法はFacebook経由でユーザーにログインするだけでなく、新しいPFUserを作成することです。なぜ私にとっては問題なのですか?まあ、もちろん。私はisNewフィールドでサインアップとサインインを区別することができますが、実際には役に立ちません。

ユーザーがFacebook経由でログインしようとしている(彼はまだPFUserを持っていません)、ログオンして新しいユーザーが作成されます。ユーザーが新規(つまり、以前に登録されていないユーザー)で、このログインを拒否する必要があります。さて、私は彼を拒否する、私は言う "あなたはまだ登録されていない、行くとサインアップ"。ユーザーは(同じログイン方法で)サインアップし、今度はユーザーがログインしようとしたときに作成された同じPFUserが返されます。ユーザーは新規ではなく、すでに登録されているため、同じアカウントを再度作成することは不可能です。

問題を理解していますか? PFFacebookUtilsアカウントの作成とログインに対処する方法がわからない、または愚かなAPIを提供するのはPFFacebookUtilsです。あなたはどうやってそれをしますか?私が説明した問題をどうやって解決しますか?本当に、それはとてもシンプルでなければならないが、良い例はどこにも見つからない。

答えて

1

ログインとサインアップで新しいユーザーであるかどうかを確認するためのログインとサインアップコードがある。ここに私のコードです:

LOGIN

let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true) 
    spinningActivity.label.text = "Just a Moment" 
    spinningActivity.detailsLabel.text = "Logging in" 

    if reachabilityStatus == kNOTREACHABLE { 

     spinningActivity.hideAnimated(true) 

     self.displayError("No Internet Connection", message: "Please connect to the internet before continuing") 

    } else { 

     let permissions = ["public_profile"] 

     PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) { (user:PFUser?, error:NSError?) -> Void in 



      if error != nil { 

       spinningActivity.hideAnimated(true) 

       self.displayError("Error", message: error!.localizedDescription) 

      } else if let user = user { 
       if user.isNew { 

        spinningActivity.hideAnimated(true) 

        PFUser.currentUser()?.deleteInBackground() 

        self.displayNoticeWithTwoActions("Account Not Found", message: "This Facebook account is not in our system. You have to sign up first.", firstButtonTitle: "Sign Up",closeButtonTitle: "Ok", segue: "dontHaveAccountSegue") 

       } else { 

        spinningActivity.hideAnimated(true) 

        self.performSegueWithIdentifier("successfulLoginSegue", sender: self) 



       } 
      } else { 

       PFUser.currentUser()?.deleteInBackground() 

       spinningActivity.hideAnimated(true) 

       self.displayError("Error", message: "Unless you tapped on 'Cancel' or 'Done', something went wrong. Please try again.") 

      } 

     } 

    } 

SIGNUP

私はその後、サインアップボタンと "loadFacebookUserDetails" と呼ばれるログインボタンに実装された機能を持っています

let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true) 
    spinningActivity.label.text = "Just a Moment" 
    spinningActivity.detailsLabel.text = "Loading Details" 


    if reachabilityStatus == kNOTREACHABLE { 

     spinningActivity.hideAnimated(true) 

     self.displayError("No Internet Connection", message: "Please connect to the internet before continuing") 

    } else { 

     let permissions = ["public_profile", "email"] 

     PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) { (user:PFUser?, error:NSError?) -> Void in 

      if let user = user { 

       if !user.isNew { 

        spinningActivity.hideAnimated(true) 

        PFUser.logOut() 


        self.displayNoticeWithTwoActions("Account Found", message: "This Facebook account already in our system. You have to log in first.", firstButtonTitle: "Log In", closeButtonTitle: "Cancel", segue: "haveAccountSegue") 



       } else if error != nil { 

        spinningActivity.hideAnimated(true) 

        self.displayError("Error", message: error!.localizedDescription) 



       } else if error == nil { 

        spinningActivity.hideAnimated(true) 

        self.loadFacebookUserDetails() 

       } 

      } 

     else { 

       spinningActivity.hideAnimated(true) 

       self.displayError("Something Went Wrong", message: "Unless you tapped on 'Cancel' or 'Done', something went wrong. Please try again") 

      } 



     } 


    } 

func loadFacebookUserDetails() { 

    let spinningActivity = MBProgressHUD.showHUDAddedTo(self.view, animated: true) 
    spinningActivity.mode = MBProgressHUDMode.AnnularDeterminate 
    spinningActivity.label.text = "Just a Moment" 
    spinningActivity.detailsLabel.text = "Loading Details" 

    let requestPerameters = ["fields": "id, email, first_name, last_name, name"] 

    let userDetails = FBSDKGraphRequest(graphPath: "me", parameters: requestPerameters) 

    userDetails.startWithCompletionHandler { (connection, result, error:NSError!) -> Void in 

     if error != nil { 

      spinningActivity.hideAnimated(true) 

      self.displayError("Error", message: error!.localizedDescription) 

      PFUser.logOut() 

     } else { 

     let userID:String = result["id"] as! String 
     let userEmail:String = result["email"] as! String 
     let userFirstName:String = result["first_name"] as! String 
     let userLastName:String = result["last_name"] as! String 


     // Get Facebook Profile Picture 

     let userProfile = "https://graph.facebook.com/" + userID + "/picture?type=large" 

     let usernameLink = "https://graph.facebook.com/" + userID 

     let username = usernameLink.stringByReplacingOccurrencesOfString("https://graph.facebook.com/", withString: "") 

     let profilePictureUrl = NSURL(string: userProfile) 

     let profilePictureData = NSData(contentsOfURL: profilePictureUrl!) 

     if profilePictureData != nil { 

      let profilePictureObject = PFFile(data: profilePictureData!) 
      PFUser.currentUser()?.setObject(profilePictureObject!, forKey: "profile_picture") 

     } 

     PFUser.currentUser()?.setObject(userFirstName, forKey: "first_name") 
     PFUser.currentUser()?.setObject(userLastName, forKey: "last_name") 
     PFUser.currentUser()?.setObject(username, forKey: "facebook_link") 

     if userEmail == userEmail { 

      PFUser.currentUser()?.email = userEmail 

     } 

     PFUser.currentUser()?.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in 

      if error != nil { 

       spinningActivity.hideAnimated(true) 

       self.displayError("Error", message: error!.localizedDescription) 

       PFUser.logOut() 

      } else if success == true { 

       if !userID.isEmpty { 

        spinningActivity.hideAnimated(true) 

       NSUserDefaults.standardUserDefaults().setObject("authData", forKey: "facebookAuth") 

        NSUserDefaults.standardUserDefaults().synchronize() 

        self.performSegueWithIdentifier("facebookUserDetailsSegue", sender: self) 



       } 

      } else { 

       spinningActivity.hideAnimated(true) 

       self.displayError("Something Went Wrong", message: "Please try again") 

       PFUser.logOut() 

      } 

     }) 

    } 

} 

} 

あなたはObjective Cのへの変換に問題がある場合は、私はあなたがこれを行う方法でYouTubeの動画を見つけることができます賭けます。

関連する問題