Skip to content
Init is coming / May 19 - 23
Back

Sign in with Apple + SwiftUI application

  • 0
  • Apple
Pete
4 Nov, 2024, 04:07

I am trying to add Sign in with Apple to my app. I added the AppID, Service ID, etc in the developer's portal. I also configured the Appwrite to enable the Sign in with Apple service. I used the Service ID (Not the bundle ID) in the AppWrite's Apple OAuth. I write the following code in the SignInWithAppleButton's onCompletion closure.

TypeScript
do {
                    _ = try await account.createOAuth2Session(provider: .apple, scopes: ["email", "fullName"])
                    print("Successfully signed in with Apple!")
                } catch {
                    print("Failed to create session: \(error.localizedDescription)")
                }

The app ends up opening up the sign in sheet in the app -> Pop to website -> Sheet appears again -> go back to the app. Not only this weird problem, here are some more:

  1. I can trap the callback with .onOpenURL in the SwiftUI view. But it is kind of weird and not sure whether the user has been logged in successfully or not. How to do that properly?
  2. I can see the Apple ID is being added to the AppWrite Auth's user list. but it only able to track the email not the name. Would I be able to do that?

I tried to search the internet tutorial but most of them are for SignInWithApple + Web App. I can't find any proper tutorial for native SwiftUI app. If any info you have may help, please feel free to let me know.

TL;DR
To fix the issue of not being redirected after signing in with Apple in a SwiftUI app, you should not use the native SignInWithApple button but utilize OAuth instead. Check Appwrite's documentation on OAuth2, select Apple, and create a session using account.get() after sign-in. In your code, use a snippet like this: ``` _ = try await account.createOAuth2Session(provider: .apple, scopes: ["email", "fullName"]) ``` Additional problems: 1. Properly handle the .onOpenURL callback in SwiftUI to determine successful login. 2. To track the user's name along with email, you may
frankenstein
4 Nov, 2024, 07:19

After sign in a session is created. so you can use account.get() to get current session and userID

darShan
4 Nov, 2024, 07:33

Its weird how this is configured.

  1. You are probably using native sign-in [the first oauth modal]
  2. On completion, you are triggering another OAuth that takes you to Safari.

You don't need #1 here.

See - https://appwrite.io/docs/products/auth/oauth2 Select Apple in the language selector.

Pete
4 Nov, 2024, 08:21

Do you mean I should not use the native siginwithapplebutton?

Pete
10 Nov, 2024, 21:28

I tried to switch to NOT use the native SignInWithApple button. It just switch to the Safari every single time.

anceldev
21 Nov, 2024, 01:39

Have you find the solution? I’m with the same problem and can’t be redirected to my app once the user is signed in, it just remains in the same state

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more