ios - 解析 : Facebook login not creating new PFUser

标签 ios facebook swift parse-platform facebook-login

我一直在尝试集成 Facebook 登录和 Parse,以便在登录时创建一个新的 PFUser。我创建了一个 Facebook 登录按钮,当我按下它时,会出现标准的 Facebook 登录/请求权限屏幕。当我接受登录时,我被重定向回初始屏幕,日志显示用户已取消 Facebook 登录。当我在 Parse 网站上查看我的数据时,没有 PFUser。

据我所知,我已经按照 Parse ( https://parse.com/docs/ios/guide#users ) 和 Facebook ( https://developers.facebook.com/docs/ios/getting-started ) 制定的确切说明进行操作。我正在使用 XCode 6.3.2。我在 Cocoapods 中添加了集成的 Parse (ParseFacebookUtilsV4 v1.7 & ParseUI v1.1) 和 Facebook (FBSDKCoreKit v4.2 &FBSDKLoginKit v4.2),并链接了以下库:

libsqlite3.dylib
libz.dylib
Accounts.framework
AudioToolbox.framework
CFNetwork.framework
CoreGraphics.framework
CoreLocation.framework
CoreLocation.framework
MobileCoreService.framework
QuartzCore.framework
Security.framework
Social.framework
StoreKit.framework
SystemConfiguration.framework
libPods-xxx.a (being the name of my project)

我的代码如下:

//Appdelegate.swift
import UIKit


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

//--------------------------------------
// MARK: - UIApplicationDelegate
//--------------------------------------

func application(application: UIApplication, didFinishLaunchingWithOptions   launchOptions: [NSObject: AnyObject]?) -> Bool {


    Parse.setApplicationId("xxxxxxxx",
        clientKey: "xxxxxxxxxx")
               PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions)

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


}

}


func application(   application: UIApplication,
                openURL url: NSURL,
                sourceApplication: String,
                annotation: AnyObject?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application,
                                                            openURL: url,
                                                            sourceApplication: sourceApplication,
                                                            annotation: annotation)
}

//Facebook analytics
func applicationDidBecomeActive(application: UIApplication!) {
FBSDKAppEvents.activateApp()
} 

以及放置登录名的 View Controller :

//OnboardingRootViewController.swift

import Foundation
import UIKit




class OnboardingRootViewController: UIViewController {


override func viewDidLoad() {
    super.viewDidLoad()
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}



@IBAction func fbLoginClick(sender: AnyObject) {

var permissions = ["public_profile", "email", "user_likes"]


PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
    (user: PFUser?, error: NSError?) -> Void in
    if let user = user {
        if user.isNew {
            println("User signed up and logged in through Facebook!")
        } else {
            println("User logged in through Facebook!")
        }
    } else {
        println("Uh oh. The user cancelled the Facebook login.")
    }
}
}


}

那么,有人能帮忙吗?同样,当调用 IBAction fbLoginClick 时,Facebook 登录至少看起来有效,但是“呃哦。用户取消了 Facebook 登录。”消息显示在日志中,并且没有在 Parse 中创建新的 PFUser。我已经看到多个其他类似的问题,但没有一个有任何真正的答案,所以我相信这个问题的答案会受到很多人的欢迎,特别是因为这是使用由 Parse 和 Facebook 创建的登录教程中显示的确切代码!

编辑:如果没有人能够提供帮助,我仍然希望得到以下方面的帮助:有人告诉我应该检查 loginInBackgroundWithReadPermissions 返回的错误消息,遗憾的是没有进一步的解释。这个人只是指“呃哦。用户取消了 Facebook 登录”。消息,或者是否可以通过 NSError 检索任何信息?

编辑#2: 我添加了以下代码行以查看 NSError 返回的内容: println("写入失败: (error?.localizedDescription)")

并在日志中得到如下信息: 呃哦。用户取消了 Facebook 登录。 写入失败:无

所以我想这并没有真正让我更进一步......

编辑 #3:说真的,我会支付 10 美元给任何能帮助我的人

最佳答案

如果您正确设置框架并遵循 Parse 的 Facebook 设置指南 (https://parse.com/docs/ios/guide#users-facebook-users) 的步骤,此代码将起作用。确保您的语法与我的函数相同,并尝试删除未包含在下面的任何不必要的代码。

AppDelegate.swift

import UIKit
import CoreData
import Parse

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    //X's replace my actual Parse information

    Parse.setApplicationId("XXXXXXXXXXXXXXXXXXXXXX", clientKey: "XXXXXXXXXXXXXXXXXXXXXX")
    PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)

    PFFacebookUtils.initializeFacebookWithApplicationLaunchOptions(launchOptions)

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

}

func applicationWillResignActive(application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {

    FBSDKAppEvents.activateApp()
}

/*
func application(application: UIApplication, url: NSURL, sourceApplication: NSString, annotation: AnyObject) -> Bool {

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

}*/

func application(application: UIApplication,
    openURL url: NSURL,
    sourceApplication: String?,
    annotation: AnyObject?) -> Bool {
        return FBSDKApplicationDelegate.sharedInstance().application(application,
            openURL: url,
            sourceApplication: sourceApplication,
            annotation: annotation)
}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    self.saveContext()
}

ViewController.swift

import UIKit

class ViewController: UIViewController {

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

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

let permissions = [ "email","user_birthday", "public_profile", "user_friends"]

@IBAction func loginButtonPressed(sender: AnyObject) {
    PFFacebookUtils.logInInBackgroundWithReadPermissions(permissions) {
        (user: PFUser?, error: NSError?) -> Void in
        if let user = user {
            if user.isNew {
                println("User signed up and logged in through Facebook!")
            } else {
                println("User logged in through Facebook!")
            }
        } else {
            println("Uh oh. The user cancelled the Facebook login.")
        }
    }
}

}

@IBAction 用于我放置在 Main.storyboard 空白 View Controller 中心的简单 UIButton

Objective-C 桥接头文件:

#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <ParseFacebookUtilsV4/PFFacebookUtils.h>
#import <Parse/Parse.h>

希望这能解决您的问题!

关于ios - 解析 : Facebook login not creating new PFUser,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30830855/

相关文章:

ios - 在 SwiftUI 中创建具有特定日期的 DatePicker

ios - 生成新的 UITableViewController 并显示它

ios - 应用程序不在后台运行

ios - Firebase 消息传递中的 "Error : Use of undeclared type MessagingDelegate"

ios - 如何让我的 Facebook GraphAPI 数据在 UITableView 中不重复?

facebook - 使用 facebook javascript sdk 发布到 friend 墙

swift - 访问所有数组元素的子节点

facebook - 获取 "secret"Facebook 群组的 access_token

ios - Swift - 获取 ipv6 链接本地地址?

swift - NSOutlineView 列不排序