我正在使用ParseFacebookUtilsV4,并且fb idk似乎在检索用户信息方面遇到问题,只是为了现在打印到控制台。问题是,似乎启动请求的块没有被执行,因为无论何时我调试它似乎都跳过了启动完成处理程序。
// View controller code
PFFacebookUtils.facebookLoginManager().loginBehavior = .web
var loginTask = PFFacebookUtils.logInInBackground(withReadPermissions: [])
loginTask.continue( { (bfTask) -> Any? in
print("I'm here")
let request = FBSDKGraphRequest(graphPath:"me", parameters: ["fields":"id,email,name,first_name,last_name,picture"] )
print(request)
request?.start {
(connection, result, error) in
print(result)
}
return ""
})
// App delegate config
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let configuration = ParseClientConfiguration {
$0.applicationId = "xxxx"
$0.clientKey = "xxxxx"
$0.server = "https://parseapi.back4app.com"
}
Parse.initialize(with: configuration)
PFFacebookUtils.initializeFacebook(applicationLaunchOptions: launchOptions)
PFTwitterUtils.initialize(withConsumerKey: "xxxxx", consumerSecret: "xxxxx")
// Override point for customization after application launch.
return true
}
答案 0 :(得分:1)
这是我用于Facebook请求的功能。我正在使用阻止请求:
func loadFacebookUserDetails() {
// Define fields we would like to read from Facebook User object
let requestParameters = ["fields": "id, email, first_name, last_name, name"]
// Send Facebook Graph API Request for /me
let userDetails = FBSDKGraphRequest(graphPath: "me", parameters: requestParameters)
userDetails.startWithCompletionHandler({
(connection, result, error: NSError!) -> Void in
if error != nil {
let userMessage = error!.localizedDescription
let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
PFUser.logOut()
return
}
// Extract user fields
let userId:String = result.objectForKey("id") as! String
let userEmail:String? = result.objectForKey("email") as? String
let userFirstName:String? = result.objectForKey("first_name") as? String
let userLastName:String? = result.objectForKey("last_name") as? String
// Get Facebook profile picture
let userProfile = "https://graph.facebook.com/" + userId + "/picture?type=large"
let profilePictureUrl = NSURL(string: userProfile)
let profilePictureData = NSData(contentsOfURL: profilePictureUrl!)
// Prepare PFUser object
if(profilePictureData != nil)
{
let profileFileObject = PFFile(name:"profilePic.jpeg",data:profilePictureData!)
PFUser.currentUser()?.setObject(profileFileObject!, forKey: "ProfilePic")
}
PFUser.currentUser()?.setObject(userFirstName!, forKey: "Name")
PFUser.currentUser()?.setObject(userLastName!, forKey: "last_name")
if let userEmail = userEmail
{
PFUser.currentUser()?.email = userEmail
PFUser.currentUser()?.username = userEmail
}
PFUser.currentUser()?.saveInBackgroundWithBlock({ (success, error) -> Void in
if(error != nil)
{
let userMessage = error!.localizedDescription
let myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
PFUser.logOut()
return
}
if(success)
{
if !userId.isEmpty
{
NSUserDefaults.standardUserDefaults().setObject(userId, forKey: "user_name")
NSUserDefaults.standardUserDefaults().synchronize()
dispatch_async(dispatch_get_main_queue()) {
self.dismissViewControllerAnimated(true, completion: nil)
}
}
}
})
})
}