You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/active-directory/develop/tutorial-v2-ios.md
+27-30Lines changed: 27 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,13 @@
1
1
---
2
-
title: "Tutorial: Create an iOS or macOS app that uses the Microsoft identity platform for authentication | Azure"
3
-
titleSuffix: Microsoft identity platform
4
-
description: In this tutorial, you build an iOS or macOS app that uses the Microsoft identity platform to sign in users and get an access token to call the Microsoft Graph API on their behalf.
5
-
services: active-directory
2
+
title: "Tutorial: Create an iOS or macOS app that uses the Microsoft identity platform for authentication"
3
+
description: Build an iOS or macOS app that uses the Microsoft identity platform to sign in users and get an access token to call the Microsoft Graph API on their behalf.
In this tutorial, you build an iOS or macOS app that integrates with the Microsoft identity platform to sign users and get an access token to call the Microsoft Graph API.
22
19
23
-
When you've completed the guide, your application will accept sign-ins of personal Microsoft accounts (including outlook.com, live.com, and others) and work or school accounts from any company or organization that uses Azure Active Directory. This tutorial is applicable to both iOS and macOS apps. Some steps are different between the two platforms.
20
+
When you've completed the tutorial, your application will accept sign-ins of personal Microsoft accounts (including outlook.com, live.com, and others) and work or school accounts from any company or organization that uses Azure Active Directory. This tutorial is applicable to both iOS and macOS apps. Some steps are different between the two platforms.
24
21
25
22
In this tutorial:
26
23
@@ -75,8 +72,8 @@ If you'd like to download a completed version of the app you build in this tutor
75
72
1. Select **Accounts in any organizational directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)** under **Supported account types**.
76
73
1. Select **Register**.
77
74
1. Under **Manage**, select **Authentication** > **Add a platform** > **iOS/macOS**.
78
-
1. Enter your project's Bundle ID. If you downloaded the code, this is `com.microsoft.identitysample.MSALiOS`. If you're creating your own project, select your project in Xcode and open the **General** tab. The bundle identifier appears in the **Identity** section.
79
-
1. Select **Configure** and save the **MSAL Configuration** that appears in the **MSAL configuration** page so you can enter it when you configure your app later.
75
+
1. Enter your project's Bundle ID. If downloaded the code sample, the Bundle ID is `com.microsoft.identitysample.MSALiOS`. If you're creating your own project, select your project in Xcode and open the **General** tab. The bundle identifier appears in the **Identity** section.
76
+
1. Select **Configure** and save the **MSAL Configuration** that appears in the **MSAL configuration** page so you can enter it when you configure your app later.
80
77
1. Select **Done**.
81
78
82
79
## Add MSAL
@@ -85,7 +82,7 @@ Choose one of the following ways to install the MSAL library in your app:
85
82
86
83
### CocoaPods
87
84
88
-
1. If you're using [CocoaPods](https://cocoapods.org/), install `MSAL` by first creating an empty file called `podfile` in the same folder as your project's `.xcodeproj` file. Add the following to `podfile`:
85
+
1. If you're using [CocoaPods](https://cocoapods.org/), install `MSAL` by first creating an empty file called _podfile_ in the same folder as your project's _.xcodeproj_ file. Add the following to _podfile_:
89
86
90
87
```
91
88
use_frameworks!
@@ -96,18 +93,18 @@ Choose one of the following ways to install the MSAL library in your app:
96
93
```
97
94
98
95
2. Replace `<your-target-here>` with the name of your project.
99
-
3. In a terminal window, navigate to the folder that contains the `podfile` you created and run `pod install` to install the MSAL library.
96
+
3. In a terminal window, navigate to the folder that contains the _podfile_ you created and run `pod install` to install the MSAL library.
100
97
4. Close Xcode and open `<your project name>.xcworkspace` to reload the project in Xcode.
101
98
102
99
### Carthage
103
100
104
-
If you're using [Carthage](https://github.com/Carthage/Carthage), install `MSAL` by adding it to your `Cartfile`:
101
+
If you're using [Carthage](https://github.com/Carthage/Carthage), install `MSAL` by adding it to your _Cartfile_:
From a terminal window, in the same directory as the updated `Cartfile`, run the following command to have Carthage update the dependencies in your project.
107
+
From a terminal window, in the same directory as the updated _Cartfile_, run the following command to have Carthage update the dependencies in your project.
111
108
112
109
iOS:
113
110
@@ -129,13 +126,13 @@ You can also use Git Submodule, or check out the latest release to use as a fram
129
126
130
127
Next, we'll add your app registration to your code.
131
128
132
-
First, add the following import statement to the top of the `ViewController.swift`, as well as `AppDelegate.swift` or `SceneDelegate.swift` files:
129
+
First, add the following import statement to the top of the _ViewController.swift_ file and either _AppDelegate.swift_ or _SceneDelegate.swift_:
133
130
134
131
```swift
135
132
importMSAL
136
133
```
137
134
138
-
Then Add the following code to `ViewController.swift` prior to `viewDidLoad()`:
135
+
Next, add the following code to _ViewController.swift_ before to `viewDidLoad()`:
139
136
140
137
```swift
141
138
// Update the below to your client ID you received in the portal. The below is for running the demo only
@@ -151,7 +148,7 @@ var webViewParameters : MSALWebviewParameters?
151
148
var currentAccount: MSALAccount?
152
149
```
153
150
154
-
The only value you modify above is the value assigned to `kClientID`to be your [Application ID](./developer-glossary.md#application-id-client-id). This value is part of the MSAL Configuration data that you saved during the step at the beginning of this tutorial to register the application in the Azure portal.
151
+
The only value you modify above is the value assigned to `kClientID`to be your [Application ID](./developer-glossary.md#application-client-id). This value is part of the MSAL Configuration data that you saved during the step at the beginning of this tutorial to register the application in the Azure portal.
155
152
156
153
## Configure Xcode project settings
157
154
@@ -161,9 +158,9 @@ Add a new keychain group to your project **Signing & Capabilities**. The keychai
161
158
162
159
## For iOS only, configure URL schemes
163
160
164
-
In this step, you will register `CFBundleURLSchemes` so that the user can be redirected back to the app after sign in. By the way, `LSApplicationQueriesSchemes` also allows your app to make use of Microsoft Authenticator.
161
+
In this step, you'll register `CFBundleURLSchemes` so that the user can be redirected back to the app after sign in. By the way, `LSApplicationQueriesSchemes` also allows your app to make use of Microsoft Authenticator.
165
162
166
-
In Xcode, open `Info.plist` as a source code file, and add the following inside of the `<dict>` section. Replace `[BUNDLE_ID]` with the value you used in the Azure portal. If you downloaded the code, the bundle identifier is `com.microsoft.identitysample.MSALiOS`. If you're creating your own project, select your project in Xcode and open the **General** tab. The bundle identifier appears in the **Identity** section.
163
+
In Xcode, open _Info.plist_ as a source code file, and add the following inside of the `<dict>` section. Replace `[BUNDLE_ID]` with the value you used in the Azure portal. If you downloaded the code, the bundle identifier is `com.microsoft.identitysample.MSALiOS`. If you're creating your own project, select your project in Xcode and open the **General** tab. The bundle identifier appears in the **Identity** section.
167
164
168
165
```xml
169
166
<key>CFBundleURLTypes</key>
@@ -189,7 +186,7 @@ In Xcode, open `Info.plist` as a source code file, and add the following inside
189
186
190
187
## Create your app's UI
191
188
192
-
Now create a UI that includes a button to call the Microsoft Graph API, another to sign out, and a text view to see some output by adding the following code to the `ViewController`class:
189
+
Now create a UI that includes a button to call the Microsoft Graph API, another to sign out, and a text view to see some output by adding the following code to the `ViewController`class:
193
190
194
191
### iOS UI
195
192
@@ -372,7 +369,7 @@ Next, also inside the `ViewController` class, replace the `viewDidLoad()` method
372
369
373
370
### Initialize MSAL
374
371
375
-
Add the following `initMSAL` method to the `ViewController` class:
372
+
To the `ViewController` class, add the `initMSAL` method:
376
373
377
374
```swift
378
375
funcinitMSAL() throws {
@@ -390,7 +387,7 @@ Add the following `initMSAL` method to the `ViewController` class:
390
387
}
391
388
```
392
389
393
-
Add the following after `initMSAL` method to the `ViewController` class.
390
+
Still in the `ViewController` class and after the `initMSAL` method, add the `initWebViewParams` method:
394
391
395
392
### iOS code:
396
393
@@ -408,9 +405,9 @@ func initWebViewParams() {
408
405
}
409
406
```
410
407
411
-
### For iOS only, handle the sign-in callback
408
+
### Handle the sign-in callback (iOS only)
412
409
413
-
Open the `AppDelegate.swift` file. To handle the callback after sign-in, add `MSALPublicClientApplication.handleMSALResponse` to the `appDelegate` class like this:
410
+
Open the _AppDelegate.swift_ file. To handle the callback after sign-in, add `MSALPublicClientApplication.handleMSALResponse` to the `appDelegate` class like this:
Now, we can implement the application's UI processing logic and get tokens interactively through MSAL.
444
441
445
-
MSAL exposes two primary methods for getting tokens: `acquireTokenSilently()` and `acquireTokenInteractively()`:
442
+
MSAL exposes two primary methods for getting tokens: `acquireTokenSilently()` and `acquireTokenInteractively()`.
446
443
447
-
-`acquireTokenSilently()` attempts to sign in a user and get tokens without any user interaction as long as an account is present. `acquireTokenSilently()`requires providing a valid `MSALAccount` which can be retrieved by using one of MSAL account enumeration APIs. This sample uses `applicationContext.getCurrentAccount(with: msalParameters, completionBlock: {})` to retrieve current account.
444
+
-`acquireTokenSilently()` attempts to sign in a user and get tokens without user interaction as long as an account is present. `acquireTokenSilently()`require a valid `MSALAccount` which can be retrieved by using one of MSAL's account enumeration APIs. This tutorial uses `applicationContext.getCurrentAccount(with: msalParameters, completionBlock: {})` to retrieve the current account.
448
445
449
446
-`acquireTokenInteractively()` always shows UI when attempting to sign in the user. It may use session cookies in the browser or an account in the Microsoft authenticator to provide an interactive-SSO experience.
450
447
@@ -513,7 +510,7 @@ Add the following code to the `ViewController` class:
513
510
514
511
#### Get a token interactively
515
512
516
-
The following code snippet gets a token for the first time by creating an `MSALInteractiveTokenParameters` object and calling `acquireToken`. Next you will add code that:
513
+
The following code snippet gets a token for the first time by creating an `MSALInteractiveTokenParameters` object and calling `acquireToken`. Next you'll add code that:
517
514
518
515
1. Creates `MSALInteractiveTokenParameters` with scopes.
519
516
2. Calls `acquireToken()` with the created parameters.
@@ -812,7 +809,7 @@ Add the following helper methods to the `ViewController` class to complete the s
812
809
}
813
810
```
814
811
815
-
### For iOS only, get additional device information
812
+
### iOS only: get additional device information
816
813
817
814
Use following code to read current device configuration, including whether device is configured as shared:
818
815
@@ -839,13 +836,13 @@ Use following code to read current device configuration, including whether devic
839
836
840
837
### Multi-account applications
841
838
842
-
This app is built for a single account scenario. MSAL also supports multi-account scenarios, but it requires some additional work from apps. You will need to create UI to help users select which account they want to use for each action that requires tokens. Alternatively, your app can implement a heuristic to select which account to use by querying all accounts from MSAL. For example, see `accountsFromDeviceForParameters:completionBlock:`[API](https://azuread.github.io/microsoft-authentication-library-for-objc/Classes/MSALPublicClientApplication.html#/c:objc(cs)MSALPublicClientApplication(im)accountsFromDeviceForParameters:completionBlock:)
839
+
This app is built for a single account scenario. MSAL also supports multi-account scenarios, but it requires more application work. You'll need to create UI to help users select which account they want to use for each action that requires tokens. Alternatively, your app can implement a heuristic to select which account to use by querying all accounts from MSAL. For example, see `accountsFromDeviceForParameters:completionBlock:`[API](https://azuread.github.io/microsoft-authentication-library-for-objc/Classes/MSALPublicClientApplication.html#/c:objc(cs)MSALPublicClientApplication(im)accountsFromDeviceForParameters:completionBlock:)
843
840
844
841
## Test your app
845
842
846
843
Build and deploy the app to a test device or simulator. You should be able to sign in and get tokens for Azure AD or personal Microsoft accounts.
847
844
848
-
The first time a user signs into your app, they will be prompted by Microsoft identity to consent to the permissions requested. While most users are capable of consenting, some Azure AD tenants have disabled user consent, which requires admins to consent on behalf of all users. To support this scenario, register your app's scopes in the Azure portal.
845
+
The first time a user signs into your app, they'll be prompted by Microsoft identity to consent to the permissions requested. While most users are capable of consenting, some Azure AD tenants have disabled user consent, which requires admins to consent on behalf of all users. To support this scenario, register your app's scopes in the Azure portal.
849
846
850
847
After you sign in, the app will display the data returned from the Microsoft Graph `/me` endpoint.
0 commit comments