[英]Facebook profile picture doesn't show up Swift
I'm trying to create a profile with Facebook profile picture's and name of the user in Swift. 我正在尝试使用Facebook个人资料图片和用户名在Swift中创建个人资料。 I manage to get them on the ViewController where the login button is located but they don't show up in the Profile ViewController where I need them.
我设法将它们放在登录按钮所在的ViewController上,但是它们没有显示在我需要它们的Profile ViewController中。 Here is the code I use :
这是我使用的代码:
For the login page : 对于登录页面:
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
}
}
Here is the code for the Profile ViewController : 这是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)!)!)
}
}
}
}
I think I'm missing a link between the first VC and the Profile VC but I'm not sure. 我想我缺少第一个VC和Profile VC之间的链接,但不确定。
Thanks in advance for your help. 在此先感谢您的帮助。
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
}
}
} }
in 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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.