This project intends to give an example of how the MSAL iOS library can be used in a SwiftUI App. It uses the same MSAL APIs but separated from the View. This projects reuses the sample code provided by Microsoft within their MSAL iOS Sample Project:
https://github.com/Azure-Samples/ms-identity-mobile-apple-swift-objc
Important: I do not guarantee that this project works 100% the same way as the MS Sample does! But I hope it gives some useful input while trying to rewrite the UIKIt sample in SwiftUI :)
To use this project with your own registered Azure App you need to change the following:
- Project Bundle Identifier (
<app_name>
-><your_target>
-> General -> Bundle Identifier) - Replace the credentials in `MSAuthCredentials.swift
struct MSAuthCredentials { static let applicationId = "66855f8a-60cd-445e-a9bb-8cd8eadbd3fa" // aka clientID static let directoryId = "common" // aka tenantID }
The MSAuthAdapter
contains the business logic for loading the account info silently or with interaction. While the MSAuthProxy
encapsulates all the MSAL APIs, which mainly stayed the same as in the above mentioned UIKit sample.
class MSAuthState: ObservableObject {
@Published var account: Account?
}
struct Account: Equatable {
let email: String?
}
struct AuthView: View {
@EnvironmentObject var msAuthState: MSAuthState
private let msAuthAdapter: MSAuthAdapterProtocol = resolve()
var body: some View {
VStack(spacing: 40) {
Text(msAuthState.account?.email ?? "Signed out")
.frame(maxWidth: .infinity, alignment: .trailing)
.foregroundColor(.gray)
Button("Login with interaction") {
msAuthAdapter.login(withInteraction: true)
}
Button("Logout") {
msAuthAdapter.logout()
}
.disabled(msAuthState.account == nil)
Button("Load device mode") {
msAuthAdapter.loadDeviceMode()
}
Text("See logged console output for more info")
.font(.caption)
Spacer()
}
.padding()
}
}
- Resolver for simple dependency injection
I do not intend to update this project or keep it up to date. Microsoft itself is likely to introduce their own Sample Project for SwiftUI soon (state August 2021). See issue regarding SwiftUI Sample for a similar project Azure-Samples/active-directory-b2c-ios-swift-native-msal#47
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch
- Commit your Changes
- Push to the Branch
- Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Alexander Schmutz - [email protected]