PFUser

Objective-C

@interface PFUser : PFObject <PFSubclassing>

Swift

class PFUser : PFObject, PFSubclassing

The PFUser class is a local representation of a user persisted to the Parse Data. This class is a subclass of a PFObject, and retains the same functionality of a PFObject, but also extends it with various user specific methods, like authentication, signing up, and validation uniqueness.

Accessing the Current User

  • Gets the currently logged in user from disk and returns an instance of it.

    Declaration

    Objective-C

    + (nullable instancetype)currentUser;

    Swift

    class func current() -> Self?

    Return Value

    Returns a PFUser that is the currently logged in user. If there is none, returns nil.

  • Asynchronously loads the currently logged in user from disk and returns a task that encapsulates it.

    Declaration

    Objective-C

    + (id)getCurrentUserInBackground;

    Swift

    class func getCurrentUserInBackground() -> Any!

    Return Value

    The task that encapsulates the work being done.

  • The session token for the PFUser.

    This is set by the server upon successful authentication.

    Declaration

    Objective-C

    @property (nonatomic, copy, readonly, nullable) NSString *sessionToken;

    Swift

    var sessionToken: String? { get }
  • Whether the PFUser was just created from a request.

    Declaration

    Objective-C

    @property (nonatomic, readonly) BOOL isNew;

    Swift

    var isNew: Bool { get }
  • Whether the user is an authenticated object for the device.

    An authenticated PFUser is one that is obtained via a -signUp: or +logInWithUsername:password: method. An authenticated object is required in order to save (with altered values) or delete it.

    Declaration

    Objective-C

    @property (nonatomic, assign, unsafe_unretained, readonly,
              getter=isAuthenticated) BOOL authenticated;

    Swift

    var isAuthenticated: Bool { get }

Creating a New User

  • Creates a new PFUser object.

    Declaration

    Objective-C

    + (nonnull instancetype)user;

    Return Value

    Returns a new PFUser object.

  • Enables automatic creation of anonymous users.

    After calling this method, +currentUser will always have a value. The user will only be created on the server once the user has been saved, or once an object with a relation to that user or an ACL that refers to the user has been saved.

    Warning

    PFObject. will not work on if an item being saved has a relation to an automatic user that has never been saved.

    Declaration

    Objective-C

    + (void)enableAutomaticUser;

    Swift

    class func enableAutomaticUser()
  • The username for the PFUser.

    Declaration

    Objective-C

    @property (nonatomic, strong, nullable) NSString *username;

    Swift

    var username: String? { get set }
  • ! The password for the PFUser.

    This will not be filled in from the server with the password. It is only meant to be set.

    Declaration

    Objective-C

    @property (nonatomic, strong, nullable) NSString *password;

    Swift

    var password: String? { get set }
  • The email for the PFUser.

    Declaration

    Objective-C

    @property (nonatomic, strong, nullable) NSString *email;

    Swift

    var email: String? { get set }
  • Signs up the user asynchronously.

    This will also enforce that the username isn’t already taken.

    Warning

    Make sure that password and username are set before calling this method.

    Declaration

    Objective-C

    - (id)signUpInBackground;

    Swift

    func signUpInBackground() -> Any!

    Return Value

    The task, that encapsulates the work being done.

  • Signs up the user asynchronously.

    This will also enforce that the username isn’t already taken.

    Warning

    Make sure that password and username are set before calling this method.

    Declaration

    Objective-C

    - (void)signUpInBackgroundWithBlock:(nullable PFBooleanResultBlock)block;

    Swift

    func signUpInBackground() async throws -> Bool

    Parameters

    block

    The block to execute. It should have the following argument signature: ^(BOOL succeeded, NSError *error).

Logging In

  • Makes an asynchronous request to login a user with specified credentials.

    Returns an instance of the successfully logged in PFUser. This also caches the user locally so that calls to +currentUser will use the latest logged in user.

    Declaration

    Objective-C

    + (id)logInWithUsernameInBackground:(nonnull NSString *)username
                               password:(nonnull NSString *)password;

    Swift

    class func logInWithUsername(inBackground username: String, password: String) -> Any!

    Parameters

    username

    The username of the user.

    password

    The password of the user.

    Return Value

    The task, that encapsulates the work being done.

  • Makes an asynchronous request to log in a user with specified credentials.

    Returns an instance of the successfully logged in PFUser. This also caches the user locally so that calls to +currentUser will use the latest logged in user.

    Declaration

    Objective-C

    + (void)logInWithUsernameInBackground:(nonnull NSString *)username
                                 password:(nonnull NSString *)password
                                    block:(nullable PFUserResultBlock)block;

    Swift

    class func logInWithUsername(inBackground username: String, password: String, block: PFUserResultBlock? = nil)

    Parameters

    username

    The username of the user.

    password

    The password of the user.

    block

    The block to execute. It should have the following argument signature: ^(PFUser *user, NSError *error).

Becoming a User

  • Makes an asynchronous request to become a user with the given session token.

    Returns an instance of the successfully logged in PFUser. This also caches the user locally so that calls to +currentUser will use the latest logged in user.

    Declaration

    Objective-C

    + (id)becomeInBackground:(nonnull NSString *)sessionToken;

    Swift

    class func become(inBackground sessionToken: String) -> Any!

    Parameters

    sessionToken

    The session token for the user.

    Return Value

    The task, that encapsulates the work being done.

  • Makes an asynchronous request to become a user with the given session token.

    Returns an instance of the successfully logged in PFUser. This also caches the user locally so that calls to +currentUser will use the latest logged in user.

    Declaration

    Objective-C

    + (void)becomeInBackground:(nonnull NSString *)sessionToken
                         block:(nullable PFUserResultBlock)block;

    Swift

    class func become(inBackground sessionToken: String, block: PFUserResultBlock? = nil)

    Parameters

    sessionToken

    The session token for the user.

    block

    The block to execute. The block should have the following argument signature: ^(PFUser *user, NSError *error).

Revocable Session

  • Enables revocable sessions and migrates the currentUser session token to use revocable session if needed.

    This method is required if you want to use PFSession APIs and your application’s ‘Require Revocable Session’ setting is turned off on http://parse.com app settings. After returned BFTask completes - PFSession class and APIs will be available for use.

    Declaration

    Objective-C

    + (id)enableRevocableSessionInBackground;

    Swift

    class func enableRevocableSessionInBackground() -> Any!

    Return Value

    An instance of BFTask that is completed when revocable sessions are enabled and currentUser token is migrated.

  • Enables revocable sessions and upgrades the currentUser session token to use revocable session if needed.

    This method is required if you want to use PFSession APIs and legacy sessions are enabled in your application settings on http://parse.com/. After returned BFTask completes - PFSession class and APIs will be available for use.

    Declaration

    Objective-C

    + (void)enableRevocableSessionInBackgroundWithBlock:
        (nullable PFUserSessionUpgradeResultBlock)block;

    Swift

    class func enableRevocableSessionInBackground() async throws

    Parameters

    block

    Block that will be called when revocable sessions are enabled and currentUser token is migrated.

Logging Out

  • Asynchronously logs out the currently logged in user.

    This will also remove the session from disk, log out of linked services and all future calls to +currentUser will return nil. This is preferrable to using -logOut, unless your code is already running from a background thread.

    Declaration

    Objective-C

    + (id)logOutInBackground;

    Swift

    class func logOutInBackground() -> Any!

    Return Value

    An instance of BFTask, that is resolved with nil result when logging out completes.

  • Asynchronously logs out the currently logged in user.

    This will also remove the session from disk, log out of linked services and all future calls to +currentUser will return nil. This is preferrable to using -logOut, unless your code is already running from a background thread.

    Declaration

    Objective-C

    + (void)logOutInBackgroundWithBlock:(nullable PFUserLogoutResultBlock)block;

    Swift

    class func logOutInBackground() async throws

    Parameters

    block

    A block that will be called when logging out completes or fails.

Requesting a Password Reset

  • Send a password reset request asynchronously for a specified email and sets an error object. If a user account exists with that email, an email will be sent to that address with instructions on how to reset their password.

    Declaration

    Objective-C

    + (id)requestPasswordResetForEmailInBackground:(nonnull NSString *)email;

    Swift

    class func requestPasswordResetForEmail(inBackground email: String) -> Any!

    Parameters

    email

    Email of the account to send a reset password request.

    Return Value

    The task, that encapsulates the work being done.

  • Send a password reset request asynchronously for a specified email.

    If a user account exists with that email, an email will be sent to that address with instructions on how to reset their password.

    Declaration

    Objective-C

    + (void)requestPasswordResetForEmailInBackground:(nonnull NSString *)email
                                               block:(nullable PFBooleanResultBlock)
                                                         block;

    Swift

    class func requestPasswordResetForEmail(inBackground email: String, block: PFBooleanResultBlock? = nil)

    Parameters

    email

    Email of the account to send a reset password request.

    block

    The block to execute. It should have the following argument signature: ^(BOOL succeeded, NSError *error).

Third-party Authentication

  • Registers a third party authentication delegate. If a delegate is already registered for the authType then it is replaced by the new delegate.

    Note

    This method shouldn’t be invoked directly unless developing a third party authentication library.

    See

    PFUserAuthenticationDelegate

    Declaration

    Objective-C

    + (void)registerAuthenticationDelegate:
                (nonnull id<PFUserAuthenticationDelegate>)delegate
                               forAuthType:(nonnull NSString *)authType;

    Parameters

    delegate

    The third party authenticaiton delegate to be registered.

    authType

    The name of the type of third party authentication source.

  • Unregisters a third party authentication delegate. If no delegate is registered, this fails gracefully.

    Note

    This method shouldn’t be invoked directly unless developing a third party authentication library.

    See

    PFUserAuthenticationDelegate

    Declaration

    Objective-C

    + (void)unregisterAuthenticationDelegateForAuthType:
        (nonnull NSString *)authType;

    Swift

    class func unregisterAuthenticationDelegate(forAuthType authType: String)

    Parameters

    authType

    The name of the type of third party authentication source.

  • Logs in a user with third party authentication credentials.

    Note

    This method shouldn’t be invoked directly unless developing a third party authentication library.

    See

    PFUserAuthenticationDelegate

    Declaration

    Objective-C

    + (id)logInWithAuthTypeInBackground:(nonnull NSString *)authType
                               authData:
                                   (nonnull NSDictionary<NSString *, NSString *> *)
                                       authData;

    Swift

    class func logInWithAuthType(inBackground authType: String, authData: [String : String]) -> Any!

    Parameters

    authType

    The name of the type of third party authentication source.

    authData

    The user credentials of the third party authentication source.

    Return Value

    A BFTask that is resolved to PFUser when logging in completes.

  • Links this user to a third party authentication library.

    Note

    This method shouldn’t be invoked directly unless developing a third party authentication library.

    See

    PFUserAuthenticationDelegate

    Declaration

    Objective-C

    - (id)linkWithAuthTypeInBackground:(nonnull NSString *)authType
                              authData:
                                  (nonnull NSDictionary<NSString *, NSString *> *)
                                      authData;

    Swift

    func linkWithAuthType(inBackground authType: String, authData: [String : String]) -> Any!

    Parameters

    authType

    The name of the type of third party authentication source.

    authData

    The user credentials of the third party authentication source.

    Return Value

    A BFTask that is resolved to @YES if linking succeeds.

  • Unlinks this user from a third party authentication library.

    Note

    This method shouldn’t be invoked directly unless developing a third party authentication library.

    See

    PFUserAuthenticationDelegate

    Declaration

    Objective-C

    - (id)unlinkWithAuthTypeInBackground:(nonnull NSString *)authType;

    Swift

    func unlinkWithAuthType(inBackground authType: String) -> Any!

    Parameters

    authType

    The name of the type of third party authentication source.

    Return Value

    A BFTask that is resolved to @YES if unlinking succeeds.

  • Indicates whether this user is linked with a third party authentication library of a specific type.

    Note

    This method shouldn’t be invoked directly unless developing a third party authentication library.

    See

    PFUserAuthenticationDelegate

    Declaration

    Objective-C

    - (BOOL)isLinkedWithAuthType:(nonnull NSString *)authType;

    Swift

    func isLinked(withAuthType authType: String) -> Bool

    Parameters

    authType

    The name of the type of third party authentication source.

    Return Value

    YES if the user is linked with a provider, otherwise NO.

Creating a New User

  • Deprecated

    Please use PFUser.-signUpInBackgroundWithBlock: instead.

    Signs up the user asynchronously.

    This will also enforce that the username isn’t already taken.

    Warning

    Make sure that password and username are set before calling this method.

    @deprecated Please use PFUser.-signUpInBackgroundWithBlock: instead.

    Declaration

    Objective-C

    - (void)signUpInBackgroundWithTarget:(nullable id)target
                                selector:(nullable SEL)selector;

    Swift

    func signUpInBackground(withTarget target: Any?, selector: Selector?)

    Parameters

    target

    Target object for the selector.

    selector

    The selector that will be called when the asynchrounous request is complete. It should have the following signature: (void)callbackWithResult:(NSNumber *)result error:(NSError *)error. error will be nil on success and set if there was an error. [result boolValue] will tell you whether the call succeeded or not.

Logging In

  • Makes an asynchronous request to login a user with specified credentials.

    Returns an instance of the successfully logged in PFUser. This also caches the user locally so that calls to +currentUser will use the latest logged in user.

    @deprecated Please use PFUser.+logInWithUsernameInBackground:password:block: instead.

    Declaration

    Objective-C

    + (void)logInWithUsernameInBackground:(nonnull NSString *)username
                                 password:(nonnull NSString *)password
                                   target:(nullable id)target
                                 selector:(nullable SEL)selector;

    Swift

    class func logInWithUsername(inBackground username: String, password: String, target: Any?, selector: Selector?)

    Parameters

    username

    The username of the user.

    password

    The password of the user.

    target

    Target object for the selector.

    selector

    The selector that will be called when the asynchrounous request is complete. It should have the following signature: (void)callbackWithUser:(PFUser *)user error:(NSError *)error.

Becoming a User

  • Deprecated

    Please use PFUser.+becomeInBackground:block: instead.

    Makes an asynchronous request to become a user with the given session token.

    Returns an instance of the successfully logged in PFUser. This also caches the user locally so that calls to +currentUser will use the latest logged in user.

    @deprecated Please use PFUser.+becomeInBackground:block: instead.

    Declaration

    Objective-C

    + (void)becomeInBackground:(nonnull NSString *)sessionToken
                        target:(nullable id)target
                      selector:(nullable SEL)selector;

    Swift

    class func become(inBackground sessionToken: String, target: Any?, selector: Selector?)

    Parameters

    sessionToken

    The session token for the user.

    target

    Target object for the selector.

    selector

    The selector that will be called when the asynchrounous request is complete. It should have the following signature: (void)callbackWithUser:(PFUser *)user error:(NSError *)error.

Requesting a Password Reset

  • Send a password reset request asynchronously for a specified email and sets an error object.

    If a user account exists with that email, an email will be sent to that address with instructions on how to reset their password.

    @deprecated Please use PFUser.+requestPasswordResetForEmailInBackground:block: instead.

    Declaration

    Objective-C

    + (void)requestPasswordResetForEmailInBackground:(nonnull NSString *)email
                                              target:(nullable id)target
                                            selector:(nullable SEL)selector;

    Swift

    class func requestPasswordResetForEmail(inBackground email: String, target: Any?, selector: Selector?)

    Parameters

    email

    Email of the account to send a reset password request.

    target

    Target object for the selector.

    selector

    The selector that will be called when the asynchronous request is complete. It should have the following signature: (void)callbackWithResult:(NSNumber *)result error:(NSError *)error. error will be nil on success and set if there was an error. [result boolValue] will tell you whether the call succeeded or not.

Creating a New User

  • Signs up the user synchronously.

    This will also enforce that the username isn’t already taken.

    Warning

    Make sure that password and username are set before calling this method.

    Declaration

    Objective-C

    - (BOOL)signUp;

    Return Value

    Returns YES if the sign up was successful, otherwise NO.

  • Signs up the user synchronously.

    This will also enforce that the username isn’t already taken.

    Warning

    Make sure that password and username are set before calling this method.

    Declaration

    Objective-C

    - (BOOL)signUp:(NSError *_Nullable *_Nullable)error;

    Swift

    func signUp() throws

    Parameters

    error

    Error object to set on error.

    Return Value

    Returns whether the sign up was successful.

Logging In

  • Makes a synchronous request to login a user with specified credentials.

    Returns an instance of the successfully logged in PFUser. This also caches the user locally so that calls to +currentUser will use the latest logged in user.

    Declaration

    Objective-C

    + (nullable instancetype)logInWithUsername:(nonnull NSString *)username
                                      password:(nonnull NSString *)password;

    Parameters

    username

    The username of the user.

    password

    The password of the user.

    Return Value

    Returns an instance of the PFUser on success. If login failed for either wrong password or wrong username, returns nil.

  • Makes a synchronous request to login a user with specified credentials.

    Returns an instance of the successfully logged in PFUser. This also caches the user locally so that calls to +currentUser will use the latest logged in user.

    Declaration

    Objective-C

    + (nullable instancetype)logInWithUsername:(nonnull NSString *)username
                                      password:(nonnull NSString *)password
                                         error:(NSError *_Nullable *_Nullable)error;

    Swift

    class func logIn(withUsername username: String, password: String) throws -> Self

    Parameters

    username

    The username of the user.

    password

    The password of the user.

    error

    The error object to set on error.

    Return Value

    Returns an instance of the PFUser on success. If login failed for either wrong password or wrong username, returns nil.

Becoming a User

  • Makes a synchronous request to become a user with the given session token.

    Returns an instance of the successfully logged in PFUser. This also caches the user locally so that calls to +currentUser will use the latest logged in user.

    Declaration

    Objective-C

    + (nullable instancetype)become:(nonnull NSString *)sessionToken;

    Parameters

    sessionToken

    The session token for the user.

    Return Value

    Returns an instance of the PFUser on success. If becoming a user fails due to incorrect token, it returns nil.

  • Makes a synchronous request to become a user with the given session token.

    Returns an instance of the successfully logged in PFUser. This will also cache the user locally so that calls to +currentUser will use the latest logged in user.

    Declaration

    Objective-C

    + (nullable instancetype)become:(nonnull NSString *)sessionToken
                              error:(NSError *_Nullable *_Nullable)error;

    Swift

    class func become(_ sessionToken: String) throws -> Self

    Parameters

    sessionToken

    The session token for the user.

    error

    The error object to set on error.

    Return Value

    Returns an instance of the PFUser on success. If becoming a user fails due to incorrect token, it returns nil.

Logging Out

  • Synchronously* logs out the currently logged in user on disk.

    Declaration

    Objective-C

    + (void)logOut;

    Swift

    class func logOut()

Requesting a Password Reset

  • Synchronously Send a password reset request for a specified email.

    If a user account exists with that email, an email will be sent to that address with instructions on how to reset their password.

    Declaration

    Objective-C

    + (BOOL)requestPasswordResetForEmail:(nonnull NSString *)email;

    Parameters

    email

    Email of the account to send a reset password request.

    Return Value

    Returns YES if the reset email request is successful. NO - if no account was found for the email address.

  • Synchronously send a password reset request for a specified email and sets an error object.

    If a user account exists with that email, an email will be sent to that address with instructions on how to reset their password.

    Declaration

    Objective-C

    + (BOOL)requestPasswordResetForEmail:(nonnull NSString *)email
                                   error:(NSError *_Nullable *_Nullable)error;

    Swift

    class func requestPasswordReset(forEmail email: String) throws

    Parameters

    email

    Email of the account to send a reset password request.

    error

    Error object to set on error.

    Return Value

    Returns YES if the reset email request is successful. NO - if no account was found for the email address.