在 swift 中将 Facebook 电子邮件存储在解析用户表中
Posted
技术标签:
【中文标题】在 swift 中将 Facebook 电子邮件存储在解析用户表中【英文标题】:Store Facebook email in parse User table in swift 【发布时间】:2015-03-20 07:49:51 【问题描述】:我有以下几行代码。我应该添加什么以便电子邮件也被存储? 任何东西都适合 swift
@IBAction func fblogin(sender: AnyObject)
var permissions = ["public_profile","email"]
PFFacebookUtils.logInWithPermissions(permissions,
(user: PFUser!, error: NSError!) -> Void in
if let user = user
if user.isNew
println("User signed up and logged in through Facebook!")
var user = PFUser.currentUser()
self.performSegueWithIdentifier("login", sender: self)
//let defaults = NSUserDefaults.standardUserDefaults()
else
println("User logged in through Facebook!")
self.performSegueWithIdentifier("login", sender: self)
else
println("Uh oh. The user cancelled the Facebook login.")
)
【问题讨论】:
您在 API 中查看并尝试了什么? parse.com/questions/… 找不到 swift 的任何内容。不了解 Objective C 逻辑一样,类名一般都一样,试试看 【参考方案1】:考虑到您已经设置了解析和 facebook,点击 Facebook 按钮后,它将如何工作。
@IBAction func fblogin(sender: AnyObject)
var permissions = ["public_profile","email"]
PFFacebookUtils.logInWithPermissions(permissions,
(user: PFUser!, error: NSError!) -> Void in
if let user = user
if user.isNew
println("User signed up and logged in through Facebook!")
var user = PFUser.currentUser()
self.performSegueWithIdentifier("login", sender: self)
let defaults = NSUserDefaults.standardUserDefaults()
FBRequestConnection.startForMeWithCompletionHandler(connection, result, error in
println(result.email);
PFUser.currentUser().setObject(result.email as String, forKey: "email")
PFUser.currentUser().saveInBackground()
)
defaults.setBool(true,forKey:"signedup")
self.performSegueWithIdentifier("login", sender: self)
else
println("User logged in through Facebook!")
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(true,forKey:"signedup")
self.performSegueWithIdentifier("login", sender: self)
else
println("Uh oh. The user cancelled the Facebook login.")
)
【讨论】:
您介意添加几句话来解释您的代码应该如何使用吗? 在 PFUser.currentUser().setObject(result.email as String, forKey: "email") 处出现错误【参考方案2】:在目标c中,您可以通过这种方式解决您的问题
[PFFacebookUtils logInWithPermissions:permissionsArray
block:^(PFUser *user, NSError *error)
if (user)
[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
if (!error)
// Store the current user's Facebook ID on the user
[[PFUser currentUser] setObject:[result objectForKey:@"id"]
forKey:@"fbId"];
[[PFUser currentUser] saveInBackground];
];
];
【讨论】:
以上是关于在 swift 中将 Facebook 电子邮件存储在解析用户表中的主要内容,如果未能解决你的问题,请参考以下文章
保存电子邮件地址 Facebook + Parse (Swift)
在 Swift 中链接/合并 Facebook 用户和 PFUser(解析)
如何在 Swift 3 中获取 Facebook 用户 ID、电子邮件、图片类型(大)