解析Facebook登录Swift 3.0

时间:2017-05-09 10:24:25

标签: ios swift facebook parse-platform parse-server

我正在尝试使用Parse登录Facebook并创建一个新的Parse用户作为结果。 Parse没有适当的Facebook登录文档。他们中的一些人已经过时了我已经浏览了Stack Overflow上的每个Parse + Facebook登录帖子,但无法得到任何答案。

PS我无法对现有帖子发表评论,因为我刚刚开始堆栈溢出,而且我的声誉偏低。

我在按钮中有以下代码。当我点击按钮时,我被重定向到Facebook,在那里我给予许可,然后它就停留在那里。同时在控制台“哦哦。用户取消了Facebook登录。”打印当我打印出用户并且错误时(两者都在PFFacebookUtils.logInInBackground中

我已经尝试过很多方法,例如在App Delegate中更改方法以找到解决方法。直到现在我还没有成功。

以下是我的按钮

中的代码
         @IBAction func FacebookLogin(_ sender: Any) {

    //var permissions = [ "public_profile" ]

    PFFacebookUtils.logInInBackground(withReadPermissions: permissions) { (user, error) in            if let user = user {
        if user.isNew {
            print("User signed up and logged in through Facebook!")
        } else {
            print("User logged in through Facebook!")
        }
    } else {
        print("Uh oh. The user cancelled the Facebook login.")
        }
    }    

}

这是我添加的app代理相关FB功能。如果你可以看看它会很棒

func application(application:UIApplication,openURL url: NSURL,sourceApplication:String?,
        annotation:AnyObject) ->Bool{

        return FBSDKApplicationDelegate.sharedInstance().application(application,open: url as URL!, sourceApplication: sourceApplication,annotation: annotation)

    }

  func applicationDidBecomeActive(_ application: UIApplication) {
        FBSDKAppEvents.activateApp()
    }


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Enable storing and querying data from Local Datastore.
        // Remove this line if you don't want to use Local Datastore features or want to use cachePolicy.
        Parse.enableLocalDatastore()

        let parseConfiguration = ParseClientConfiguration(block: { (ParseMutableClientConfiguration) -> Void in
            ParseMutableClientConfiguration.applicationId = “xxxxxx"
            ParseMutableClientConfiguration.clientKey = “xxxxxx"
            ParseMutableClientConfiguration.server = “xxxxxx"
        })

        Parse.initialize(with: parseConfiguration)

        //PFUser.enableAutomaticUser()


        PFFacebookUtils.initializeFacebook(applicationLaunchOptions: launchOptions)

        let defaultACL = PFACL();

        // If you would like all objects to be private by default, remove this line.
        defaultACL.getPublicReadAccess = true

        PFACL.setDefault(defaultACL, withAccessForCurrentUser: true)

        if application.applicationState != UIApplicationState.background {
        return FBSDKApplicationDelegate.sharedInstance().application(application,didFinishLaunchingWithOptions:launchOptions)
    }  

最后在我的Bridging标题

#ifndef ParseStarterProject_Bridging_Header_h
#define ParseStarterProject_Bridging_Header_h
#import <ParseFacebookUtilsV4/PFFacebookUtils.h>
#import <FBSDKCoreKit/FBSDKcoreKit.h>
#endif /* ParseStarterProject_Bridging_Header_h */

1 个答案:

答案 0 :(得分:1)

替换你的方法:

func application(application:UIApplication,openURL url: NSURL,sourceApplication:String?,
        annotation:AnyObject) ->Bool

由此:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}

否则SDK不会调用您的方法

我希望我的回答很有帮助