aws-amplify / aws-amplify/docs

Amplify Libraries Swift - Authentication - Sign In with Web UI - Looking for SwiftUI/UIKit example

Open
#5,791 1 comment 1 reaction 0 assignees View on GitHub
amplify/ios
Dominant language
MDX
Stars
506
Forks
1.1k
Avg merge
3h 14m
Merged PRs (30d)
1

Description

**Describe the content issue**:

image

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:
```swift
// Use `@MainActor` since the window will be accessed from the UI, via a button tap for example.
@MainActor
private var window: UIWindow {
guard
let scene = UIApplication.shared.connectedScenes.first,
let windowSceneDelegate = scene.delegate as? UIWindowSceneDelegate,
let window = windowSceneDelegate.window as? UIWindow
else { return UIWindow() }

return window
}
```

```swift
func signInWithWebUI() async {
do {
let signInResult = try await Amplify.Auth.signInWithWebUI(presentationAnchor: window)
if signInResult.isSignedIn {
print("Sign in succeeded")
}
} catch let error as AuthError {
print("Sign in failed \(error)")
} catch {
print("Unexpected error: \(error)")
}
}
```

Then provide a simple SwiftUI example
```swift
struct ContentView: View {
var body: some View {
VStack {
Button("Sign In with HostedUI") {
Task {
await signInWithWebUI()
}
}
}
}
}
```

**URL page where content issue is**:

https://docs.amplify.aws/lib/auth/signin_web_ui/q/platform/ios/#update-infoplist

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.