Open lawmicha opened 1 year ago
Another option I tend to use when invoking signInWithWebUI(...)
from SwiftUI.
// ContentView.swift
struct ContentView: View {
@EnvironmentObject var sceneDelegate: SceneDelegate
var body: some View {
// ...
}
func signInWithWebUI() async {
do {
let signInResult = try await Amplify.Auth.signInWithWebUI(
presentationAnchor: sceneDelegate.window!, // probably best to gracefully unwrap in the example :smile:
)
if signInResult.isSignedIn {
print("Sign in succeeded")
}
} catch let error as AuthError {
print("Sign in failed \(error)")
} catch {
print("Unexpected error: \(error)")
}
}
}
// MyApp.swift
@main
struct MyApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
init() {
// configure Amplify
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(
_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions
) -> UISceneConfiguration {
let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
if connectingSceneSession.role == .windowApplication {
configuration.delegateClass = SceneDelegate.self
}
return configuration
}
}
class SceneDelegate: NSObject, ObservableObject, UIWindowSceneDelegate {
var window: UIWindow?
func scene(
_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions
) {
if #available(iOS 15.0, *) {
self.window = (scene as? UIWindowScene)?.keyWindow
} else {
self.window = (scene as? UIWindowScene)?.windows
.first(where: \.isKeyWindow)
}
}
}
Describe the content issue:
I feel like there's some missing content here. I wasn't working from a UIViewController and was building a SwiftUI app, I wasn't sure If i wanted to go down the route of creating a UIViewRepresentable, and then get the UIWindow? I had no idea how to get this working, and went searching else where. I feel we should improve this documentation to include the entire UIViewController example and the SwiftUI example. In the end, I went with this, but not sure if this should be the recommended approach
Globally define:
Then provide a simple SwiftUI example
URL page where content issue is:
https://docs.amplify.aws/lib/auth/signin_web_ui/q/platform/ios/#update-infoplist