如何使用Swift 2.0和iOS 9.1进行Facebook登录? [英] How can I do a Facebook login using Swift 2.0 and iOS 9.1?

查看:90
本文介绍了如何使用Swift 2.0和iOS 9.1进行Facebook登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将最新的Facebook SDK添加到为 iOS 9.1 编写的 XCode 7.1 项目中。不幸的是,我知道的是 Swift ,而不是目标C Facebook的开发人员网站文档仅在目标C 即可。 Google上的每一次搜索都会显示出过去6个月内的事情发生了很大变化的文档太旧了。所以我很失落。



我用plist文件做了所有简单的东西。



我可以使用:

  import FBSDKCoreKit 
import FBSDKLoginKit
pre>

我还添加了:

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

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

到我的 AppDelegate.swift 文件。这一切都是成功的。显然,我也添加了正确的框架。除此之外,我还处于停顿状态,因为我不知道添加登录按钮的语法,用于捕获我假设将使用令牌和其他配置文件信息返回的json字符串,我可以用它来存储在用户帐户中

解决方案

你在正确的轨道上。



a href =https://developers.facebook.com/docs/ios/ios9 =noreferrer>您是否设置了您的列表?



你必须添加到你的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,参数:[fields:id,interested _in,gender,birthday,email,age_range,name,picture.width(480).height(480)])
graphRequest.startWithCompletionHandler({(connection,result,error))>

如果((错误)!= nil)
{
//进程错误
打印(错误:\(错误))
$

{
print(fetched user:\(result))
let id:NSString = result.valueForKey(id)as! String
print(User ID is:\(id))
// etc ...
}
})
}

你将要玩$ code> returnData()我们已经包含了广泛的权限(用户年龄,感兴趣的),但是这个代码应该可以让你90%的方式。



等等),但是你必须自己配置它们。老实说,这部分(很难的部分)与对方C的对应是非常相似的,这是更好的记录。只需下载一些获取项目的工作,并尝试将它们解析成适合您的想法的东西。



可能很难让它一切正常工作,但一定要继续下去!


I have added the latest Facebook SDK to my XCode 7.1 project written for iOS 9.1. Unfortunately all I know is Swift, not Objective C. Facebook's developer site documentation only has the documentation in Objective C. Every search on Google reveals documentation that is just too old because things have changed so much just in the past 6 months. So I'm kind of lost.

I did all the easy stuff with the plist file.

I was able to use:

import FBSDKCoreKit
import FBSDKLoginKit

I also added:

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

and

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

To my AppDelegate.swift file. This all works and builds successfully. I have the correct frameworks added as well, obviously. Beyond this point I'm at a standstill because I don't know the syntax for adding a login button, for capturing what I assume would be returned json strings with tokens and other profile information I can use to store in the user's account, etc.

解决方案

You're on the right track.

Have you set up your pList yet?

You're gonna have to add to your VC, add a login button, deal with delegate, and then deal with FB info. Something like (but not exactly) this:

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

You're gonna have to play around with the returnData() part to get it right, but this code should get you 90% of the way there.

I've included a wide range of permissions (user age, interested in, etc), but you'll have to configure them yourself. Honestly this part (the hard part) is really similar with it's object C counterpart, which is much better documented. Just download some get projects where it's working, and try to parse them together into something that suits your fancy.

It can be hard to get it all working, but give it a go, and keep at it!

这篇关于如何使用Swift 2.0和iOS 9.1进行Facebook登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆