Group Group Group Group Group Group Group Group Group

Sign in with Apple using SwiftUI | raywenderlich.com

Great tutorial. For testing purposes I was testing it without asking for FullName and logged in a couple of times with my Apple ID. Now I want to change it to ask for full name but the request won’t kick in. Already deleted the App and also deleted the access to it from my Apple Id on iCloud. Still it won’t prompt. Is there anything I can do to get the pop up back up?

The way to do this is go to setting, tap on your apple id at the top, choose Password & Security, then Apps Using Apple ID. From there you should be able to choose your app and “Stop Using Apple ID” for it. After you do that it’ll prompt you like it’s the first time.

Thanks. This was buggy two days ago, in the sense I removed and iOS wouldn’t still ask but seem to be fine now. The webcredentials for the keychain is off now. Not working at all. Plus, the face id pop up request for the Apple Sign In does not pop sometimes (you have to slightly force it by doing a swipe up).
Its kinda weird right now. Your tutorial is helping me understand it better.

By the way, the keychain wrapper you use in the example. Is it a cocoapod? Is it fine if I use?
It seems to be one the bests I saw around cause it allows for try/catches, the other from Ray just returns a Bool.

Nope, it’s just a file that I wrote. You can freely copy it as long as you keep the copyright header in tact.

@gargoyle, thanks for the tutorial, it’s really useful!
You mentioned OAuth setup with Sign In with Apple. Any plans for tutorial about it?

@max.vyshnevskyi Glad to hear you found it useful! I do plan on writing a tutorial around using this server side with OAuth. However, there’s a bug right now with Apple’s REST API where they are ignoring the expiration date passed and the returned token is only good for 9 minutes. I have an open bug with them, but they just keep asking for a ‘sysdiagnose’ even though this has nothing to do with a Mac. I’ve sent them explicit ‘curl’ commands now showing how this is done from a non-mac windows machine, but I haven’t heard back from them.

@gargoyle, I see… So good luck with that and I’m looking forward to the new tutorial :slight_smile:

I ran the app on a device running iOS 13.3 and even though the modal for logging in with my Apple ID shows, the app does not progress past the sign in screen once I successfully log in with my Apple ID. I compiled the app using XCode 11.2.1

Hi @jtremain. The app isn’t designed to do anything after you’ve successfully authenticated. It’s just showing you how to authenticate. You’ll just do normal SwiftUI type stuff to handle your navigation after you’ve attempted the authentication.

I have a problem with storing data to the keychain. I always get the following error:

Error Domain=NSCocoaErrorDomain Code=4866 “The data couldn’t be written because it isn’t in the correct format.” UserInfo={NSUnderlyingError=0x2815af720 {Error Domain=NSCocoaErrorDomain Code=4864 "This decoder will only decode classes that adopt NSSecureCoding. Class ‘__SwiftValue’ does not adopt it."

Once the user signs in, how do you get it to go to the main section of the app and get off the login view? Sorry, I’m new to this and I can’t seem to figure it out.

Hi Cory! Scott/@gargoyle asked me to answer this.

This tutorial is set up with the login as the only screen. In your app, you’d probably want it to appear as a modal sheet. I wrote a tutorial about SwiftUI navigation, and there’s a section about showing and dismissing a modal. Have a look?

I am curious as to why you pass window as environment(\.window, window). As I understand, @Environment is used in cases where it’s needed to automatically update the view state if the environment object changes. In the current use case it doesn’t look to be the case. Why not just pass UIWindow as a constructor parameter to ContentView? It would be easier (no need to create a custom EnvironmentKey, for instance).

@neverwintermoon I suppose in this case you could do that as it’s a simple example. However, in a real app you’re probably not going to be pushing the sign in window as the very first window you have. It’ll be pushed as part of some type of navigation in the app, based on actions taken. So, in this case, you’d want to have it be in the environment instead of having to pass it around via all the views you might go through to get there.

@gargoyle Can you please help with this when you get a chance? Thank you - much appreciated! :]

@steve46 are you still facing this issue? Can you show your exact code please?

@gargoyle is it safe using directly credentials.user to identify a user on app server without doing any validation with Apple servers? I’m using native AuthenticationServices and I don’t understand how to use identityToken and authorizationCode. Thanks!

credentials.user is the ID that you store for the user. It’s where you’d historically have had them use an email address to create an ID in the past. There is no security around that user though. It’s a string of characters that anybody could spoof if they knew what it was.

The identityToken and authorizationCode are for the OAuth type flows that you’d handle server side. There a potential bug right now with the server side expiration dates that I’m working with Apple, and once I have that resolved I’m planning to post a tutorial on handling server side via Vapor.

If you are only on the device then you can trust the user provided. If you’re making a REST call to your backend server, then you need to implement the authorization via Apple’s REST API.

1 Like

Yes, I’m making REST call to my backend server… so I need to implement OAuth flow. Thanks for your answer, I’ll wait for your Vapor tutorial and then I’ll try to make it work in PHP :smiley: