我正在尝试使用Facebook个人资料图片和Swift中的用户名创建个人资料。我设法在登录按钮所在的ViewController上获取它们,但它们不会显示在我需要它们的Profile ViewController中。这是我使用的代码:
登录页面:
import UIKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate
{
@IBOutlet var btnFacebook: FBSDKLoginButton!
@IBOutlet var ivUserProfileImage: UIImageView!
@IBOutlet var lblName: UILabel!
override func viewDidLoad()
{
super.viewDidLoad()
configureFacebook()
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"first_name, last_name, picture.type(large)"]).startWithCompletionHandler { (connection, result, error) -> Void in
let strFirstName: String = (result.objectForKey("first_name") as? String)!
let strLastName: String = (result.objectForKey("last_name") as? String)!
let strPictureURL: String = (result.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as? String)!
self.lblName.text = "Welcome, \(strFirstName) \(strLastName)"
self.ivUserProfileImage.image = UIImage(data: NSData(contentsOfURL: NSURL(string: strPictureURL)!)!)
}
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
{
let loginManager: FBSDKLoginManager = FBSDKLoginManager()
loginManager.logOut()
ivUserProfileImage.image = nil
lblName.text = ""
}
func configureFacebook()
{
btnFacebook.readPermissions = ["public_profile", "email", "user_friends"];
btnFacebook.delegate = self
}
}
以下是Profile ViewController的代码:
import UIKit
class Profile: UIViewController {
@IBOutlet weak var profilePicture: UIImageView!
@IBOutlet weak var userName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"first_name, last_name, picture.type(large)"]).startWithCompletionHandler { (connection, result, error) -> Void in
let strFirstName: String = (result.objectForKey("first_name") as? String)!
let strLastName: String = (result.objectForKey("last_name") as? String)!
let strPictureURL: String = (result.objectForKey("picture")?.objectForKey("data")?.objectForKey("url") as? String)!
self.userName.text = "Welcome, \(strFirstName) \(strLastName)"
self.profilePicture.image = UIImage(data: NSData(contentsOfURL: NSURL(string: strPictureURL)!)!)
}
}
}
}
我想我错过了第一个VC和Profile VC之间的链接,但我不确定。
提前感谢您的帮助。
答案 0 :(得分:1)
import UIKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
@IBOutlet var btnFacebook: FBSDKLoginButton!
@IBOutlet var ivUserProfileImage: UIImageView!
@IBOutlet var lblName: UILabel!
var object: AnyObject!
override func viewDidLoad() {
super.viewDidLoad()
configureFacebook()
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
FBSDKGraphRequest.init(graphPath: "me", parameters: ["fields":"first_name, last_name, picture.type(large)"]).startWithCompletionHandler { (connection, result, error) -> Void in
self.object = result
self.performSegueWithIdentifier("YourSegueID", sender: self)
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
{
let loginManager: FBSDKLoginManager = FBSDKLoginManager()
loginManager.logOut()
ivUserProfileImage.image = nil
lblName.text = ""
}
func configureFacebook()
{
btnFacebook.readPermissions = ["public_profile", "email", "user_friends"];
btnFacebook.delegate = self
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourSegueID" {
let dvc = segue.destinationViewController as? ViewController2
dvc!.object = object // sending facebook data from VC to ProfileVC
}
}
}
ProfileVC中的
import UIKit
class Profile: UIViewController {
@IBOutlet weak var profilePicture: UIImageView!
@IBOutlet weak var userName: UILabel!
var object: AnyObject!
override func viewDidLoad() {
super.viewDidLoad()
print(object)// your facebook object
print(object.objectForKey("first_name")as! String)
print(object.objectForKey("id") as! String)
print(object.objectForKey("last_name")as! String)
let image = object.objectForKey("picture") as! NSObject
let data = image.valueForKey("data") as! NSObject
let ProfilepicUrl = data.valueForKey("url") as! String
print(ProfilepicUrl)
}