Facebook FBSDKLoginManager/logInWithReadPermissions Swift示例不使用Parse [英] Facebook FBSDKLoginManager/logInWithReadPermissions Swift Example not using Parse

查看:257
本文介绍了Facebook FBSDKLoginManager/logInWithReadPermissions Swift示例不使用Parse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最新的Facebook Swift文档在哪里.我无法显示"FB登录"对话框.对loginWithReadPermissions的调用永不返回?

Where is the latest Facebook Swift Documentation. I can't get the FB Login Dialog to show up. The call to loginWithReadPermissions never returns?

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit


class ViewController: UIViewController {override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.

            let loginManager = FBSDKLoginManager()
            loginManager.logInWithReadPermissions(["basic_info", "email", "user_likes"], fromViewController: self.parentViewController, handler: { (result, error) -> Void in
                if error != nil {
                    print(FBSDKAccessToken.currentAccessToken())
                } else if result.isCancelled {
                    print("Cancelled")
                } else {
                    print("LoggedIn")
                }
            })
        }

推荐答案

所以答案是将FBSDKLoginButton与以下内容一起使用

So the answer is to use FBSDKLoginButton with the following

在viewController的类声明中

In the class declaration of the viewController

import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate

然后使用

        // Setup the FB Login/Logout Button  FB will take care of the
        // verbiage based on the current access token

        let loginView : FBSDKLoginButton = FBSDKLoginButton()
        self.view.addSubview(loginView)
        loginView.center = self.view.center
        loginView.readPermissions = ["public_profile", "email", "user_friends"]
        loginView.delegate = self

        // If we have an access token, then let's display some info

        if (FBSDKAccessToken.currentAccessToken() != nil)
        {
            // Display current FB premissions
            print (FBSDKAccessToken.currentAccessToken().permissions)

            // Since we already logged in we can display the user datea and taggable friend data.
            self.showUserData()
            self.showFriendData()
        }

通过显示用户信息

 func showUserData()
    {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "id, name, gender, first_name, last_name, locale, email"])
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

            if ((error) != nil)
            {
                // Process error
                print("Error: \(error)")
            }
            else
            {
                let userName : NSString = result.valueForKey("name") as! NSString
                print("User Name is: \(userName)")

                if let userEmail : NSString = result.valueForKey("email") as? NSString {
                    print("User Email is: \(userEmail)")
                }
            }
        })
    }

并显示可标记的朋友

func showFriendData()
    {
        let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/taggable_friends?limit=999", parameters: ["fields" : "name"])
        graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

            if ((error) != nil)
            {
                // Process error
                print("Error: \(error)")
            }
            else
            {
                if let friends : NSArray = result.valueForKey("data") as? NSArray{
                    var i = 1
                    for obj in friends {
                        if let name = obj["name"] as? String {
                            print("\(i) " + name)
                            i++
                        }
                    }
                }
            }
        })
    }

这篇关于Facebook FBSDKLoginManager/logInWithReadPermissions Swift示例不使用Parse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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