Tutorial: Prepare your iOS/macOS app for native authentication
Applies to: iOS (Swift) macOS (Swift)
This tutorial demonstrates how to add Microsoft Authentication Library (MSAL) native authentication SDK framework to your iOS/macOS Swift app.
In this tutorial, you learn how to:
- Add the MSAL framework to an iOS/macOS app.
- Create SDK instance.
Prerequisites
- Xcode
- If you haven't already, follow the instructions in Sign in users in sample iOS (Swift) mobile app by using native authentication and register an app in your external tenant. Make sure you complete the following steps:
- Register an application.
- Enable public client and native authentication flows.
- Grant API permissions.
- Create a user flow.
- Associate the app with the user flow.
- iOS/macOS project
Add the MSAL framework to an iOS/macOS app
- Open your iOS/macOS project in Xcode.
- Select Add Package Dependencies... from the File menu.
- Enter
https://github.com/AzureAD/microsoft-authentication-library-for-objc
as the Package URL and choose Add Package. - Add a new keychain group to your project Capabilities. Use
com.microsoft.adalcache
on iOS andcom.microsoft.identity.universalstorage
on macOS.
For more information and other mechanisms to add MSAL to your project, see the project Readme file.
Create SDK instance
Import the MSAL library into your view controller by adding
import MSAL
at the top of yourViewController
class.Add a
nativeAuth
member variable to yourViewController
class by adding the following code just before theviewDidLoad()
function:var nativeAuth: MSALNativeAuthPublicClientApplication!
Next, add the following code to the
viewDidLoad()
function:do { nativeAuth = try MSALNativeAuthPublicClientApplication( clientId: "Enter_the_Application_Id_Here", tenantSubdomain: "Enter_the_Tenant_Subdomain_Here", challengeTypes: [.OOB] ) print("Initialized Native Auth successfully.") } catch { print("Unable to initialize MSAL \(error)") }
Replace the following values with the values from the Microsoft Entra admin center:
Find the
Enter_the_Application_Id_Here
value and replace it with the Application (client) ID of the app you registered earlier.Find the
Enter_the_Tenant_Subdomain_Here
and replace it with the Directory (tenant) subdomain. For example, if your tenant primary domain iscontoso.onmicrosoft.com
, usecontoso
. If you don't have your Directory (tenant) subdomain, learn how to read your tenant details.The challenge types are a list of values, which the app uses to notify Microsoft Entra about the authentication method that it supports.
- For sign-up and sign-in flows with email one-time passcode, use
[.OOB]
. - For sign-up and sign-in flows with email and password, use
[.OOB, .password]
. - For self-service password reset (SSPR), use
[.OOB]
.
Learn more challenge types.
- For sign-up and sign-in flows with email one-time passcode, use
To build, select the Product > Build in your project’s toolbar.
Optional: Logging configuration
MSAL provides a logging API that you can use to enable and configure logging. To see all debug output from MSAL add the following code at the start of the viewDidLoad()
function:
MSALGlobalConfig.loggerConfig.logLevel = .verbose
MSALGlobalConfig.loggerConfig.setLogCallback { logLevel, message, containsPII in
if !containsPII {
print("MSAL: \(message ?? "")")
}
}
This outputs all debug logs from MSAL, which can be helpful in diagnosing issues and learning how the native authentication flows work. To learn more about configuring log levels and best practices see Logging in MSAL for iOS/macOS.