Facebook FBSDKLoginManager/logInWithReadPermissions Swift Example not using Parse

纵然是瞬间 提交于 2019-12-12 03:23:44

问题


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

回答1:


You code should work if you exclude "user_likes" from permissions. From facebook docs: If your app asks for more than public_profile, email and user_friends, Facebook must review it before you release it. Learn more about the review process and what's required to pass review. https://developers.facebook.com/docs/facebook-login/permissions/v2.5#reference-user_likes

Another problem may be that you have not set correctly the FacebookSDK in your project. See this tutorial: https://developers.facebook.com/docs/ios/getting-started/




回答2:


So the answer is to use FBSDKLoginButton with the following

In the class declaration of the viewController

import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate

Then show the FBLogin Button with

        // 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()
        }

Show the users info with

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

And to display the taggable friends

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


来源:https://stackoverflow.com/questions/33131847/facebook-fbsdkloginmanager-loginwithreadpermissions-swift-example-not-using-pars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!