Andrea Culot Andrea Culot - 1 month ago 12
iOS Question

How to take cover_photo from Facebook in swift 2.0

I'm trying to get the user's cover photo from Facebook but I have some problems...here is my code

import UIKit
import FBSDKCoreKit
import FBSDKLoginKit

class ViewController: UIViewController, FBSDKLoginButtonDelegate {
@IBOutlet weak var name : UILabel!
@IBOutlet weak var sesso: UILabel!
@IBOutlet weak var immagineProfilo : UIImageView!
@IBOutlet weak var immagineCopertina: UIImageView!

override func viewDidLoad() {
super.viewDidLoad()
if (FBSDKAccessToken.currentAccessToken() == nil)
{
print("Not logged in..")
}
else
{
print("Logged in..")
}

let loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["public_profile", "email"]
loginButton.center = CGPointMake(80, 100)
loginButton.delegate = self
self.view.addSubview(loginButton)

}

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
{
if error == nil
{
print("Login complete.")
/* let protectedPage = self.storyboard?.instantiateViewControllerWithIdentifier("ProfileViewController") as! ProfileViewController
let protectedPageNav = UINavigationController(rootViewController: protectedPage)

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

appDelegate.window?.rootViewController = protectedPageNav*/

if let _ = result.token{
//Get user acces token
let _:FBSDKAccessToken=result.token

print("Token = \(FBSDKAccessToken.currentAccessToken().tokenString)")

print("User ID = \(FBSDKAccessToken.currentAccessToken().userID)")


}

//Show user information
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name,picture, email, gender, pic_cover"])

graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

if ((error) != nil)
{
// Process error
print("Error: \(error)")
}
else
{
let imgURLCoverPhoto = "https://graph.facebook.com/\(FBSDKAccessToken.currentAccessToken().userID)/pic_cover?
let imgURLCover = NSURL(string: imgURLCoverPhoto)
let imageDataCoverPhoto = NSData(contentsOfURL: imgURLCover!)
let imageCoverPhoto = UIImage(data: imageDataCoverPhoto!)
self.immagineCopertina.image = imageCoverPhoto
let imgURLString = "https://graph.facebook.com/\(FBSDKAccessToken.currentAccessToken().userID)/picture?width=10000"
let imgURL = NSURL(string: imgURLString)
let imageData = NSData(contentsOfURL: imgURL!)
let image = UIImage(data: imageData!)
self.immagineProfilo.image = image

print("fetched user: \(result)")
self.name.text = result.valueForKey("name") as! NSString as String
self.name.text = result.valueForKey("gender") as! NSString as String


}
})


}
else
{
print(error.localizedDescription)
}
}

func loginButtonDidLogOut(loginButton: FBSDKLoginButton!)
{
print("User logged out...")
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}


thanks

Answer

A look at the docs helps sometimes:

The correct field is cover, not pic_cover as found in your code.

Comments