首页 文章

解析:Facebook登录时没有创建新的PFUser

提问于
浏览
3

我一直在努力整合Facebook登录和Parse,以便在登录时创建新的PFUser . 我创建了一个Facebook登录按钮,当我按下它时,会出现标准的Facebook登录/询问权限屏幕 . 当我接受登录时,我被重定向回初始屏幕,并且日志显示用户已取消Facebook登录 . 当我在Parse网站上查看我的数据时,没有PFUsers .

据我所知,我已经遵循了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控制器:

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


}

那么,任何人都可以得到任何帮助吗?再次,Facebook登录至少似乎工作,当调用IBAction fbLoginClick时,但“哦哦 . 用户取消了Facebook登录 . ”消息显示在日志中,并且在Parse中不会创建新的PFUser . 我已经看到了其他多个类似的问题,但没有任何真正的答案,所以我肯定很多人都会理解这个问题的答案,特别是因为这是使用Parse和Facebook创建的登录教程中显示的确切代码!

编辑:如果没有人能够提供帮助,我仍然感谢以下方面的帮助:我被告知我应该检查loginInBackgroundWithReadPermissions返回的错误消息,遗憾的是没有进一步的解释 . 这个人只是简单地提到“呃哦 . 用户取消了Facebook登录 . ”消息,还是可以通过NSError检索任何信息?

编辑#2:我添加了以下代码行以查看NSError返回的内容:println(“write failure:(error?.localizedDescription)”)

并在日志中收到以下消息:哦,哦 . 用户取消了Facebook登录 . 写失败:没有

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

编辑#3:同样认真的,我会向任何可以帮助我的人支付10美元

1 回答

  • 1

    如果您正确设置框架并遵循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是一个简单的UIButton我放在我的Main.storyboard中的空白视图控制器的中心

    Objective-C桥接 Headers :

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

    希望这能解决你的问题!

相关问题