5

我已将最新的 Facebook SDK 添加到XCode 7.1iOS 9.1. 不幸的是,我只知道Swift,而不是Objective CFacebook 的开发者站点文档只有Objective C中的文档。在 Google 上的每次搜索都会显示太旧的文档,因为在过去的 6 个月里事情发生了很大的变化。所以我有点迷路了。

我用 plist 文件做了所有简单的事情。

我能够使用:

import FBSDKCoreKit
import FBSDKLoginKit

我还补充说:

return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)

return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)

到我的AppDelegate.swift档案。这一切都有效并成功构建。显然,我也添加了正确的框架。除此之外,我处于停滞状态,因为我不知道添加登录按钮的语法,用于捕获我认为将返回的带有令牌的 json 字符串和其他可用于存储在用户帐户中的配置文件信息等.

4

2 回答 2

8

你在正确的轨道上。

你设置好你的 pList 了吗?

您必须添加到您的 VC,添加登录按钮,处理委托,然后处理 FB 信息。像(但不完全是)这样的东西:

class yourVC: UIViewController, FBSDKLoginButtonDelegate, UITextFieldDelegate 

{
var loginView : FBSDKLoginButton = FBSDKLoginButton()

viewDidLoad() {
            loginView.frame = CGRectMake(20, 20, theWidth-40, 40)
            self.view.addSubview(loginView)
            loginView.readPermissions = ["public_profile", "email", "user_friends","user_birthday"]
            loginView.delegate = self

}

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
        if ((error) != nil)
        {
        //handle error
        } else {
                returnUserData()
        }
    }

func returnUserData()
    {


  let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"id,interested_in,gender,birthday,email,age_range,name,picture.width(480).height(480)"])
    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            // Process error
            print("Error: \(error)")
        }
        else
        {
            print("fetched user: \(result)")
            let id : NSString = result.valueForKey("id") as! String
            print("User ID is: \(id)")
            //etc...
         }
      })
}

你将不得不玩弄这个returnData()部分才能让它正确,但这段代码应该能让你完成 90% 的工作。

我已经包含了广泛的权限(用户年龄、感兴趣等),但您必须自己配置它们。老实说,这部分(困难的部分)与它的对象 C 对应部分非常相似,它的文档记录要好得多。只需下载一些可以运行的 get 项目,然后尝试将它们一起解析成适合您的东西。

让这一切正常工作可能很困难,但试一试,并坚持下去!

于 2015-11-02T16:25:51.980 回答
6

如果您想通过单击自定义按钮从 Facebook 执行登录,这将有所帮助:

@IBAction func onClickFacebookButton(sender: UIButton){
    let login = FBSDKLoginManager()
    login.loginBehavior = FBSDKLoginBehavior.SystemAccount
    login.logInWithReadPermissions(["public_profile", "email"], fromViewController: self, handler: {(result, error) in
        if error != nil {
            print("Error :  \(error.description)")
        }
        else if result.isCancelled {

        }
        else {
            FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "first_name, last_name, picture.type(large), email, name, id, gender"]).startWithCompletionHandler({(connection, result, error) -> Void in
                if error != nil{
                    print("Error : \(error.description)")
                }else{

                    print("userInfo is \(result))")

                }
            })
        }

    })
}
于 2016-08-10T09:44:00.697 回答