[英]Facebook Login returns nil user Xcode 7
I have the exact same situation from this topic PFFacebookUtils.logInInBackgroundWithReadPermissions, swift 1.2, user returned is nil 我在这个主题中有完全相同的情况PFFacebookUtils.logInInBackgroundWithReadPermissions,swift 1.2,返回的用户为nil
I get user nil and error nil, the author said code started working but didn't post his solution, any ideas? 我得到用户nil和错误nil,作者说代码开始起作用,但是没有发布他的解决方案,有什么想法吗?
(I'm new, so I don't have the reputation to post in that topic) (我是新来的,所以没有这个主题的声誉)
Try this: 尝试这个:
Librarys in podfile: podfile中的库:
pod 'FBSDKCoreKit', '~> 4.2' pod'FBSDKCoreKit','〜> 4.2'
pod 'FBSDKLoginKit', '~> 4.2' pod'FBSDKLoginKit','〜> 4.2'
pod 'FBSDKShareKit', '~> 4.2' pod'FBSDKShareKit','〜> 4.2'
AppDelegate.swif class: AppDelegate.swif类:
import UIKit
import FBSDKCoreKit
import FBSDKShareKit
import FBSDKLoginKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate{
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
// Added to handle the Authorization code returned from sign-in.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
}
MainViewController class or other view controller: MainViewController类或其他视图控制器:
import UIKit
import FBSDKCoreKit
import FBSDKShareKit
import FBSDKLoginKit
class MainViewController: UIViewController, FBSDKLoginButtonDelegate{
override func viewDidLoad() {
super.viewDidLoad()
var fbLoginButton : FBSDKLoginButton = FBSDKLoginButton()
fbLoginButton.readPermissions = ["public_profile", "email", "user_friends"]
fbLoginButton.delegate = self
self.view.addSubview(fbLoginButton)
fbLoginButton.center = self.view.center
}
override func viewDidAppear(animated: Bool) {
if (FBSDKAccessToken.currentAccessToken() != nil){
self.returnUserData()
}
}
func goToTabController(){
Util.showViewController(viewControllerSource: self, viewControllerToShow: "TabBarController")
}
//////////////////////////////////////
// Facebook Methods Delegate ///
//////////////////////////////////////
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
if ((error) != nil){
// Process error
}else if result.isCancelled {
// Handle cancellations
}else {
// If you ask for multiple permissions at once, you should check if specific permissions missing
if result.grantedPermissions.contains("email"){
self.returnUserData()
}
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
//println("User Logged Out")
}
func returnUserData(){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, first_name, last_name, email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
println(result.valueForKey("email"))
println(result.valueForKey("name"))
self.goToTabController()
}
})
}
//////////////////////////////////////
// Facebook Methods Delegate ///
//////////////////////////////////////
}
I hope it helps you 希望对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.