Skip to main content

Overview

DeviceLoginManager implements Facebook’s Device Login flow, which is designed for devices without a standard browser or limited input capabilities, such as tvOS apps. The flow works as follows:
  1. Call start() to request a short user code and a verification URL from Facebook.
  2. The manager calls your delegate’s deviceLoginManager(_:startedWith:) method with a DeviceLoginCodeInfo object containing the code and URL.
  3. Present the code to the user and direct them to the URL to complete authentication on another device.
  4. The manager polls Facebook periodically. When the user completes (or declines) the flow, your delegate’s deviceLoginManager(_:completedWith:error:) method is called.
Module: FacebookLogin
Declared in: DeviceLoginManager.swift
Objective-C name: FBSDKDeviceLoginManager

Initializer

init(permissions:enableSmartLogin:)

@objc(initWithPermissions:enableSmartLogin:)
public init(permissions: [String], enableSmartLogin: Bool)
permissions
[String]
The permissions to request, e.g. ["public_profile", "email"].
enableSmartLogin
Bool
When true, the manager uses Bonjour to advertise the login code on the local network so nearby devices can complete the flow automatically (Smart Login).

Properties

delegate
DeviceLoginManagerDelegate?
A weak reference to the object receiving login events. Set this before calling start().
permissions
[String]
The permissions that were passed to the initializer. Read-only.
redirectURL
URL?
An optional URL to redirect the user to after login completes. Must be registered in your app settings under Advanced → OAuth Redirect URIs.

Methods

start()

Begins the device login flow. The manager retains itself until the flow finishes or you call cancel().
public func start()

cancel()

Attempts to cancel the device login flow and releases the internal self-reference.
public func cancel()

DeviceLoginManagerDelegate protocol

Implement this protocol in your view controller or coordinator to respond to login events. Objective-C name: FBSDKDeviceLoginManagerDelegate

deviceLoginManager(_:startedWith:)

Called when the manager has received a device login code from Facebook.
func deviceLoginManager(
    _ loginManager: DeviceLoginManager,
    startedWith codeInfo: DeviceLoginCodeInfo
)
loginManager
DeviceLoginManager
The manager that started the flow.
codeInfo
DeviceLoginCodeInfo
The code and URL to present to the user. See DeviceLoginCodeInfo.

deviceLoginManager(_:completedWith:error:)

Called when the device login flow finishes, regardless of the outcome.
func deviceLoginManager(
    _ loginManager: DeviceLoginManager,
    completedWith result: DeviceLoginManagerResult?,
    error: Error?
)
loginManager
DeviceLoginManager
The manager that completed the flow.
result
DeviceLoginManagerResult?
The login result, or nil if an error occurred. See DeviceLoginManagerResult.
error
Error?
An error, or nil if the flow succeeded or was cancelled.

DeviceLoginCodeInfo

Contains the information you need to display to the user at the start of the device login flow. Objective-C name: FBSDKDeviceLoginCodeInfo
identifier
String
A unique identifier for this login flow instance, used internally for polling.
loginCode
String
The short user code to display to the user. The user enters this code at the verificationURL.
verificationURL
URL
The URL where the user completes the flow on another device (e.g. https://facebook.com/device).
expirationDate
Date
The date and time when the code expires.
pollingInterval
UInt
How frequently (in seconds) the manager polls Facebook for a result. Minimum value is 5 seconds.

DeviceLoginManagerResult

Represents the outcome of the device login flow. Objective-C name: FBSDKDeviceLoginManagerResult
accessToken
AccessToken?
The access token granted upon successful login. nil if the login was cancelled or failed.
isCancelled
Bool
true if the user cancelled the flow or if the device login code expired.

Usage example

import FacebookLogin

class DeviceLoginViewController: UIViewController, DeviceLoginManagerDelegate {

    var loginManager: DeviceLoginManager?

    func startLogin() {
        let manager = DeviceLoginManager(
            permissions: ["public_profile", "email"],
            enableSmartLogin: true
        )
        manager.delegate = self
        loginManager = manager
        manager.start()
    }

    // MARK: - DeviceLoginManagerDelegate

    func deviceLoginManager(
        _ loginManager: DeviceLoginManager,
        startedWith codeInfo: DeviceLoginCodeInfo
    ) {
        // Display the code and URL to the user
        codeLabel.text = codeInfo.loginCode
        instructionLabel.text = "Visit \(codeInfo.verificationURL) on your phone or computer."
    }

    func deviceLoginManager(
        _ loginManager: DeviceLoginManager,
        completedWith result: DeviceLoginManagerResult?,
        error: Error?
    ) {
        if let error = error {
            print("Login error:", error)
            return
        }
        guard let result = result else { return }

        if result.isCancelled {
            print("Login was cancelled or the code expired")
        } else if let token = result.accessToken {
            print("Logged in with token:", token.tokenString)
        }
    }
}

Build docs developers (and LLMs) love