简体   繁体   中英

Parse Facebook logInInBackgroundWithReadPermissions (Swift)

I have set both Parse (1.7.1) SDKs and Facebook(v4) SDKs successfully, set bridging header files and AppDelegate.swift. Now in my ViewController, I am trying to create a Facebook Login and I am trying to use the code given in 'Parse iOS Documentation - Facebook SignUp & Login' .

  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.")
        }
    }) 

However when I paste it in my ViewController.swift > ViewDidLoad, I am receiving this error:

- Extra argument in call      // for { at the first line

Can anyone please help me sort this out?

Edit: The script given has worked for me in terms of syntax, however, now I keep getting "Uh no. The user cancelled the Facebook login." even before it asks for permissions; while the facebook page is still loading.. And the user I am trying is already accepted for this particular app. Take a look: http://imgur.com/5yDs1s1

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

我遇到了同样的问题,但是将代码更改为以下代码后对我有用:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM