Andrea Culot Andrea Culot - 1 year ago 67
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 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() {
if (FBSDKAccessToken.currentAccessToken() == nil)
print("Not logged in..")
print("Logged in..")

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


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)")
let imgURLCoverPhoto = "\(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 = "\(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)") = result.valueForKey("name") as! NSString as String = result.valueForKey("gender") as! NSString as String



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

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


Answer Source

A look at the docs helps sometimes:

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download