Skip to main content

Overview

Profile represents a cached, immutable snapshot of a Facebook user’s profile. The SDK fetches profile data from the Graph API /me endpoint and stores the result in UserDefaults. When the profile changes, the SDK posts a .profileDidChange notification so your UI can update. You can configure the class to automatically refresh whenever AccessToken.current changes, which means you typically only need to call loadCurrentProfile(completion:) once, or rely on the auto-update behavior.
ModuleFacebookCore
Objective-C nameFBSDKProfile
AvailabilityiOS 12+

Static properties

current
Profile?
The profile for the currently logged-in user. nil if no user is logged in or the profile has not yet been loaded. Persisted to UserDefaults.standard between app launches.
isUpdatedWithAccessTokenChange
Bool
When true, the SDK automatically fetches a new profile from the Graph API whenever AccessToken.current changes and the new token belongs to a different user.
Profile.isUpdatedWithAccessTokenChange = true

Instance properties

userID
String
The Facebook user identifier.
firstName
String?
The user’s first name.
middleName
String?
The user’s middle name.
lastName
String?
The user’s last name.
name
String?
The user’s complete, formatted name.
A URL to the user’s Facebook profile page.
Only populated when the user has granted the user_link permission.
refreshDate
Date
The date and time when this profile snapshot was last fetched from the Graph API.
imageURL
URL?
A URL suitable for fetching the user’s profile picture. Use imageURL(forMode:size:) to request a specific size and crop mode.
email
String?
The user’s email address.
Only populated when the user has granted the email permission.
friendIDs
[String]?
A list of user IDs for the user’s friends who also use your app.
Only populated when the user has granted the user_friends permission.
birthday
Date?
The user’s birthday.
Only populated when the user has granted the user_birthday permission.
ageRange
UserAgeRange?
The user’s age range (e.g., min: 21). Useful when you need age verification but the user has not granted the user_birthday permission.
Only populated when the user has granted the user_age_range permission.
hometown
Location?
The user’s hometown, as a Location object containing an id and a name.
Only populated when the user has granted the user_hometown permission.
location
Location?
The user’s current location, as a Location object containing an id and a name.
Only populated when the user has granted the user_location permission.
gender
String?
The user’s gender.
Only populated when the user has granted the user_gender permission.
permissions
Set<String>?
The set of permissions the user has granted to your application.

Instance methods

imageURL(forMode:size:)

Returns a URL for fetching the user’s profile picture at a specific size and crop mode.
public func imageURL(forMode pictureMode: Profile.PictureMode, size: CGSize) -> URL?
body.pictureMode
Profile.PictureMode
required
The crop mode for the image. One of:
ValueDescription
.squareCrops the image to a square.
.normalPreserves the original aspect ratio.
.albumPreserves the original aspect ratio (album-style).
.smallPreserves the original aspect ratio (small).
.largePreserves the original aspect ratio (large).
body.size
CGSize
required
The desired width and height in points. Values are rounded to the nearest integer.
Returns a Graph API URL, or nil if the SDK cannot construct one (e.g., no access token or client token is set).

Static methods

loadCurrentProfile(completion:)

Loads the profile for the current access token and updates Profile.current. If a non-expired profile is already cached for the same user, the completion block is called synchronously with the cached value.
public static func loadCurrentProfile(completion: ProfileBlock?)
body.completion
ProfileBlock?
A closure called when loading completes. Receives an optional Profile and an optional Error. Both values can be nil if the profile changed mid-flight.

Notifications

NameDescription
Notification.Name.profileDidChangePosted on the default NotificationCenter when Profile.current changes.

Usage example

import FacebookCore

func setupProfile() {
    // Automatically refresh the profile whenever the logged-in user changes
    Profile.isUpdatedWithAccessTokenChange = true

    // Observe profile changes and update your UI
    NotificationCenter.default.addObserver(
        forName: .profileDidChange,
        object: nil,
        queue: .main
    ) { _ in
        self.updateUI(with: Profile.current)
    }

    // Manually trigger a load
    Profile.loadCurrentProfile { profile, error in
        if let error = error {
            print("Failed to load profile:", error)
            return
        }

        guard let profile = profile else { return }

        print("Name:", profile.name ?? "Unknown")
        print("User ID:", profile.userID)

        // Request a 100×100 square profile picture URL
        if let imageURL = profile.imageURL(forMode: .square, size: CGSize(width: 100, height: 100)) {
            // Use imageURL with URLSession or a third-party image loading library
            print("Profile picture URL:", imageURL)
        }
    }
}

func updateUI(with profile: Profile?) {
    guard let profile = profile else {
        // User logged out
        return
    }
    nameLabel.text = profile.name
    emailLabel.text = profile.email ?? "No email granted"
}

Build docs developers (and LLMs) love