2015-06-14 24 views
3

我一直在嘗試整合Facebook登錄和解析,以便在登錄時創建一個新的PFUser。我創建了一個Facebook登錄按鈕,當我按下標準Facebook出現登錄/詢問權限屏幕。當我接受登錄時,我被重定向回初始屏幕,並且日誌顯示用戶已取消Facebook登錄。當我在Parse網站上查看我的數據時,沒有PFUser。解析:Facebook登錄不創建新的PFUser

據我所知,我遵循了Parse(https://parse.com/docs/ios/guide#users)和Facebook(https://developers.facebook.com/docs/ios/getting-started)的詳細說明。我正在使用XCode 6.3.2。我已經加入綜合解析(ParseFacebookUtilsV4 V1.7 & ParseUI V1.1)和Facebook(FBSDKCoreKit V4.2 & FBSDKLoginKit 4.2版)中的CocoaPods,並已鏈接下列庫:

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

而且其中在登錄放在視圖控制器:

//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(「寫入失敗:(錯誤.localizedDescription)?」)

並在日誌中收到以下消息: 呃哦。用戶取消了Facebook登錄。 寫入失敗:無

所以我想這並沒有真正得到我的任何進一步...

編輯#3:就像嚴重的是,我將貝寶$ 10到任何人可以幫助我

回答

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我把空白的ViewController在我的主要中心。故事板

Objective-C的橋接報:

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

希望這能解決您的問題!

+0

嗚呼!有效!我只更新了應用程序委託,所以我的錯誤肯定在某處。非常感謝Armin! – clfougner