-
Notifications
You must be signed in to change notification settings - Fork 145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SwiftUI support for fetching tokens interactively without using ViewController #1437
Comments
hi @dpaulino, we don't have SwiftUI sample at the moment. I recommend you to take a look at this tutorial: "Interfacing with UIKit". |
Is there any plan to support SwiftUI? Apple seems heavily invested in this moving forward, and new devs like me only or mostly know SwiftUI. Without support, my colleagues and I may not be able to add Microsoft logins to our products |
I posted this last night for authenticating against a B2C tenant. The code snippets show everything that you need: https://medium.com/neudesic-innovation/using-azure-ad-b2c-to-authenticate-ios-app-users-ef3f82435f7d |
This issue has been automatically marked as stale because it has not had recent activity. Please provide additional information if requested. Thank you for your contributions. |
Full SwiftUI support would be awesome. Apple is really pushing SwiftUI. |
@kaisong1990 and @antrix1989 and word on this feature? Do we have a target date for this? |
We don't have the ETA at the moment, but we are always encouraging contributions. |
Hi y'all, I'm trying to integrate MSAL with SwiftUI app as well. I'm acquiring the token interactively from one of my SwiftUI Views upon appearing, see code below:
In my view model I've this code:
the
Unfortunately, I'm getting the following error: |
This is Working for fresh installation of the App, but not running afterward without signing in on 1st launch of the app. So basically the error is thrown from 2nd launch of the app and up. |
a simple workaround is to create a struct MSALAuthPresentationView: UIViewControllerRepresentable {
@Binding var showingMSALAuthPresentaion: Bool
func makeUIViewController(context: Context) -> UIMSALAuthPresentationViewController {
return UIMSALAuthPresentationViewController(showingMSALAuthPresentaion: $showingMSALAuthPresentaion)
}
func updateUIViewController(_ uiViewController: UIMSALAuthPresentationViewController, context: Context) {
}
}
class UIMSALAuthPresentationViewController: UIViewController {
var showingMSALAuthPresentaion: Binding<Bool>
init(showingMSALAuthPresentaion: Binding<Bool>, nibName nibNameOrNil: String? = nil,
bundle nibBundleOrNil: Bundle? = nil) {
self.showingMSALAuthPresentaion = showingMSALAuthPresentaion
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
doAuth()
}
private func doAuth() {
let config = MSALPublicClientApplicationConfig(clientId: "")
let scopes = ["user.read"]
let application = try? MSALPublicClientApplication(configuration: config)
guard let application = application else {
print("doAuth: load application failed")
showingMSALAuthPresentaion.wrappedValue = false
return
}
#if os(iOS)
let viewController = self // Pass a reference to the view controller that should be used when getting a token interactively
let webviewParameters = MSALWebviewParameters(authPresentationViewController: viewController)
#else
let webviewParameters = MSALWebviewParameters()
#endif
let interactiveParameters = MSALInteractiveTokenParameters(scopes: scopes, webviewParameters: webviewParameters)
application.acquireToken(with: interactiveParameters, completionBlock: { (result, error) in
defer {
self.showingMSALAuthPresentaion.wrappedValue = false
}
guard let authResult = result, error == nil else {
print("doAuth acquireToken error: \(error!)")
return
}
// Get access token from result
let accessToken = authResult.accessToken
// You'll want to get the account identifier to retrieve and reuse the account for later acquireToken calls
let accountIdentifier = authResult.account.identifier
})
}
} then put the |
The article by Michael Collins does work, but it really seems like getting it set up and configured is challenging. One of the advantages of other solutions is the simplicity. Is this on the roadmap? It seems like it would be a high priority given iOS mobile development is a pretty significant audience and SwiftUI is the future. This should be a few lines of code and a few configuration settings in an xcode project. |
any news? would be awesome if the sdk provide the implementation |
Not sure if this would help anyone but I made a repo where I use MSAL with @Environment and @observable class here: The UIViewController is placed at the root of the application and all logic is extracted to an Observable object in the Environment. This allows you to login and logout from anywhere within your SwiftUI Application |
Simple WorkaroundCreate a shared static Utilities class for returning the top view controller
Can be implemented like so:
|
This is a nice workaround, though in iOS16+ it provokes the following warning: 'keyWindow' was deprecated in iOS 13.0: Should not be used for applications that support multiple scenes as it returns a key window across all connected scenes |
I believe I feel the need to propose another workaround, that works flawlessly as of iOS 16.2+: In your AppDelegate, setup your UISceneConfiguration with a delegateClass of type sceneDelegate implementing UIWindowSceneDelegate. In your sceneDelegate cast your UIScene as a UIWindowScene -> keyWindow and from that get the rootViewController. Please note that this doesent woth in Preview, but works fine running normally.
|
Elegant solution! Implemented it today and it works great! |
The issue we're having is that in |
@legoesbenr Can you clarify the type of |
Hi there,
I want to integrate MSAL into my new SwiftUI app, but based on the sample code, it seems that I need to work with UIKit. I'm not familiar with UIKit at all. How do I get a token interactively using just SwiftUI paradigms?
Here's the sample code that I'm confused about, since SwiftUI doesn't use controllers:
The text was updated successfully, but these errors were encountered: