Parse Facebook logInInBackgroundWithReadPermissions (Swift)

我只是一个虾纸丫 提交于 2019-11-30 16:57:43

Same problem for me when I upgraded to swift 1.2. Appears to be related to some kind of more restrictive syntax check with the new compiler. This change works for me:

    PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
        (user: PFUser?, error: NSError?) -> Void in
        if let user = user {
            if user.isNew {
                println("User signed up and logged in through Facebook!")
            } else {
                println("User logged in through Facebook!")
            }
        } else {
            println("Uh oh. The user cancelled the Facebook login.")
        }
    }

I found a work-around by adding this code in ViewDidLoad:

   if PFUser.currentUser() != nil {

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

    }

together as placing in the button:

   @IBAction func Facebook(sender: AnyObject) {

    var permissions = ["public_profile"]

    PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) { (user: PFUser?, error: NSError?) -> Void in
        if let user = user {
            if user.isNew {
                println("User signed up and logged in through Facebook!")

                self.facebookButton.alpha = 0



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

            } else {

               println("User logged in through Facebook!")

                self.facebookButton.alpha = 0

                 let currentUser = PFUser.currentUser()

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

            }

        } else {

            println("Uh oh. The user cancelled the Facebook login.")

            println("User cancelled")

        }


    }

Also in App Delegate:

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    Parse.setApplicationId("###",
        clientKey: "###")

    PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions)

    PFUser.enableRevocableSessionInBackground()


    return true
}

And

      func application(application: UIApplication,
    openURL url: NSURL,
    sourceApplication: String?,
    annotation: AnyObject?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application,
            openURL: url,
            sourceApplication: sourceApplication,
            annotation: annotation)
}

Bridging Header:

  #import <FBSDKCoreKit/FBSDKCoreKit.h>
  #import <ParseFacebookUtilsV4/PFFacebookUtils.h>
  #import <Parse/Parse.h>
  #import <Bolts/Bolts.h>
  #import <FBSDKLoginKit/FBSDKLoginKit.h>

I don't know how far it is true but it solved my problem

brunomcortez

I got the same issue, but the code worked for me after changing it into:

PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) { (user: PFUser?, error: NSError?) -> Void in
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!