To configure QR code login for an iOS app, follow these steps.
Create an App by Using iOSNativeSwiftTemplate
The iOSNativeSwiftTemplate includes sample code for setting up QR code login. Find iOSNativeSwiftTemplate in Mobile SDK’s GitHub repository: github.com/forcedotcom/SalesforceMobileSDK-Templates.
To create an app by using the template, see Creating an iOS Project with Forceios.
Configure Your App to Open Links from a QR Code
To configure your app to open the login link when a user scans the QR code in the camera app, see the Apple documentation.
-
Allowing apps and websites to link to your content | Apple Developer Documentation
-
Defining a custom URL scheme for your app | Apple Developer Documentation
Configure Camera Usage for In-App QR Code Scanning
To request camera access and scan QR codes on your app, update the camera app’s usage description according to Apple’s documentation: NSCameraUsageDescription | Apple Developer Documentation
Enable App QR Code Login Features
In iOSNativeSwiftTemplate, QR code login features are labeled by this comment: “Log in via Salesforce UI Bridge API generated QR codes.”
To enable QR code login features in iOSNativeSwiftTemplate, modify these files.
AppDelegate.swift
To configure the QR code login setup to run on app initialization, go to the init method and uncomment the call to setupQrCodeLogin.
override init() {
// Uncomment when enabling log in via Salesforce UI Bridge API generated QR codes.
self.setupQrCodeLogin()
...
}
To customize the QR code login’s URL format, use the sample code provided in SceneDelegate.useQrCodeLoginUrl(_: ).
When using a QR code login URL generated by the UI Bridge API, you can choose between Mobile SDK’s reference format or a custom format.
internal var isQrCodeLoginUsingReferenceUrlFormat = true
If you’re using our reference format, make sure to fill in these variables.
internal var qrCodeLoginUrlScheme = "YOUR_QR_CODE_LOGIN_URL_SCHEME"
internal var qrCodeLoginUrlHost = "YOUR_QR_CODE_LOGIN_URL_HOST"
If you’re using a custom format, set isQrCodeLoginUsingReferenceUrlFormat to false, and then provide the URL handling logic in SceneDelegate.useQrCodeLogInUrl. To modify the QR code login URL format, you can customize the values in setupQrCodeLogin by following the comments provided in the source code.
private func setupQrCodeLogin() {
SalesforceLoginViewController.qrCodeLoginUrlPath =
"YOUR_QR_CODE_LOGIN_URL_PATH"
SalesforceLoginViewController.qrCodeLoginUrlJsonParameterName =
"YOUR_QR_CODE_LOGIN_URL_JSON_PARAMETER_NAME"
SalesforceLoginViewController.qrCodeLoginUrlJsonFrontdoorBridgeUrlKey =
"YOUR_QR_CODE_LOGIN_URL_JSON_FRONTDOOR_BRIDGE_URL_KEY"
SalesforceLoginViewController.qrCodeLoginUrlJsonPkceCodeVerifierKey =
"YOUR_QR_CODE_LOGIN_URL_JSON_PKCE_CODE_VERIFIER_KEY"
...
}
SceneDelegate.swift
To allow your app to receive QR code login links from external QR code readers, uncomment the calls to useQrCodeLogInUrl in the scene(_: openURLContexts:) and scene(_: willConnectTo: options:) methods.
func scene(_ scene: UIScene,
openURLContexts urlContexts: Set<UIOpenURLContext>)
{
// Uncomment when enabling log in via Salesforce UI Bridge API generated QR codes.
if let urlContext = urlContexts.first {
useQrCodeLogInUrl(urlContext.url)
}
...
}
...
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions)
{
// Uncomment when enabling log in via Salesforce UI Bridge API generated QR codes.
if let urlContext = connectionOptions.urlContexts.first {
useQrCodeLogInUrl(urlContext.url)
}
...
}