解析Facebook logInInBackgroundWithReadPermissions(Swift)

时间:2015-04-12 05:00:22

标签: ios parse-platform facebook-sdk-4.0 parsefacebookutils

我已成功设置Parse(1.7.1)SDK和Facebook(v4)SDK,设置桥接头文件和AppDelegate.swift。现在在我的ViewController中,我正在尝试创建一个Facebook登录,我正在尝试使用'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.")
        }
    }) 

然而,当我将它粘贴到我的ViewController.swift> ViewDidLoad,我收到此错误:

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

任何人都可以帮我解决这个问题吗?

编辑:给出的脚本在语法方面对我有用,但是,现在我不断得到"呃不。用户取消了Facebook登录。"甚至在它要求权限之前;虽然Facebook页面仍在加载..我正在尝试的用户已经被这个特定的应用程序接受了。 看一看: http://imgur.com/5yDs1s1

3 个答案:

答案 0 :(得分:2)

当我升级到swift 1.2时,同样的问题。似乎与使用新编译器的某种更严格的语法检查有关。这个改变对我有用:

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

答案 1 :(得分:2)

我通过在ViewDidLoad中添加此代码找到了解决办法:

   if PFUser.currentUser() != nil {

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

    }

一起放在按钮中:

   @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")

        }


    }

同样在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
}

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

桥接标题:

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

我不知道它有多远,但它解决了我的问题

答案 2 :(得分:0)

我遇到了同样的问题,但是在将代码更改为:

后,代码对我有用
PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) { (user: PFUser?, error: NSError?) -> Void in