Hi, Alex here from rebeloper.com. Welcome back to the channel and welcome back to
this tutorial where you will learn how to use Apple Sign In with UIKit.
So if you want to build out at this project make sure you hit that subscribe button,
open up Xcode and let's just drive it. So let's dive into creating our UIKit
based project with Signing with Apple. So go ahead and click the link down below
to grab this starter project and of course the resources that are needed to
make our lives a little bit easier. Now in the previous episode I have covered
how to do all of this in SwiftUI. It has been quite a long video so I will
try to make it as short as possible here. So maybe there will be some copying and
pasting but I will guide you through what the code is basically. Now if you
have downloaded this project then you will see that we have basically our
scene delegate with tab bar controller and we have a new feed view controller and
a profile view controller and basically that is it. If we are not logged in then
we are presented with our login a view controller and I try to do my best
do you have the same layout as I did in the previous episode for the SwiftUI
project, okay. So first things first what we need to do here is add our helper
files so go ahead and download the resources and you will see our Sign In
with Apple UIKit helpers and this is auth stated Sign In with Apple
Manager. So let me just drag and drop that these are just a few of an auth
state and that is undefined sign out and sign in and a Sign In with Apple
Manager that is basically holding a key for our user identifier and we have a
check user auth and this is a pretty straight forward, okay. Now let's just go
into our login view controller because that
is where we are going to do most of our setup, okay. First things first you need
to be importing authentication services, Okay and then we have our login view
controller delegate, okay. So that's for another time. So we are first going to
add here under a login view controller we are going to create our Sign In with
Apple button and that is straightforward because we are going to use the
authentication services to do that. So this will be a lazy variable and I will
call this Apple login button and it will be of type Apple and just type in
button here and you will see so it is ASAuthorization Apple ID and button.
Okay! So let's create our button here and let
me just copy that out, there we go. And let's return this button but before we
do that we want to add target. So button dot add target,
self selector, #selector and let's say Apple.
I will just copy this out. Apple login button tapped, okay. And for the
event touch up inside, there we go. So let's create this function here and it
will be an objective-c function
because we are in a selector, okay. This could be also private if you want it to
and here we can basically call on the controller that will show our face ID
recognition and so on. So let's initialize this and let's create our
controller. So controller and that will be an ASAuthorizationController.
There we go. And we are going to use some
authorization requests and those requests will be full name and email.
Now you want to choose full name or email or none according to your app
needs. So let request equals ASAuthorizationApple ID provider.
So there are, these are so mouthful of functions but here we go
create request and on this request we are just setting up the scope. So scope
requested scopes equals and full name and email. Now we can add this request here.
So this needs to be an array.
And next thing we want to do is set the controllers. So controller dot delegate
equals self and then controller dot presentation context provider equals
self again. And let's hit that and we want to
present it. So controller perform requests, there we go.
So what we need here is basically just maybe comment these out hit command B
and see that everything builds. We want to return the button here sorry for that
many of you most probably already recognize that issue here. So now we have
our Apple login button tapped, okay. We will take care of the
delegates too but for now let's just add this here and I'm just copy and paste
this because this is standard code. So let's paste all of this here, basically
what we did is added the Apple Login Button in center and we are pushing it
down and up of the UIView and we are constraining it a lot of UIKit stuff I
know but yeah that's that's what you have to do. So this is this. This is the
login view controller but if we would have run this on our device and again
you really need a device for this. You will see that we are presented with our
tab view. No login view whatsoever. So we need to check if the user is logged in.
So to do that let's go back and we are doing this under the profile view
controller in the viewdidload. So let's just scroll all the way down here and
basically what we need here is called Sign In with Apple Manager dot and the
check user auth and we will get back on auth state. So auth state, there we go and
now we want to go through that auth state with a switch
statement and let's just wait for the auto completion to fix this. Let's add
our were missing cases and of course it is undefined, signed out and signed in.
So I would just delete the default value. So what do we have here?
If this is undefined then we want to present the login view controller.
If this is sign out we want to present a login view controller. If it is signed in
then we just want to skip all together. So let's take a look at this and print
out let's say signed in, okay. We are already signed in. Now for the undefined
and sign out version let's create our controller.
And that will be our login view controller. Now in is 13 you have
something called a modal and this modal presentation style is kind of popping up
a sheet and then you can dismiss it. Now we don't want that with the login view
controller. So we want to set this to fullscreen. So what you have to do is set
the controller's model presentation style to be fullscreen, okay. And while
we are here let's set the delegate to be self because we want to check if we
really did finish the authentication and finally let's go and self dot present
view controller, our controller animated true
with completion nil, okay. And let me just copy this out and paste
it under the signed out as well, okay. Now as you can see I have already adhered to
the login view controller delegate and here under user identifier we are just
going to set and then that is pretty straightforward our user defaults dot
standard dot a string four key and this key is Sign In with Apple Manager dot
user identifier key. Now we will save this user identifier in just a moment
and yeah we need to bang that out so yeah we really know that it happens, it
exists, okay. So now we should see a pop-up of our view controller if we
build and run. So let's see that, there we go. We have our login and we do
have our Sign In with Apple but if we would tap on it we will have an error
but because we haven't enabled Apple Sign In in the capabilities. So let's just do
that yeah it is an error here under the logs, okay.
Authorization failed error and so on. Now this means that we need to set our
capabilities. So let's go into the signing in capabilities. So it's under
the target and click on plus capabilities and type in Sign In with
Apple, there we go. We just added, wait for Xcode to do its stuff for creating
provisioning profiles and so on and once we are ready let's just do a build and
run again and I believe we need to again delete our app from the device and just
build and run again and see how that looks, yeah it's working.
So let's tap on Sign In with Apple. Here we have hide my email. Let's continue there.
It's verifying, sign in and done. Yeah, but basically nothing happens.
We still own the Sign In with Apple page. So what we need to do is go back into our
login view controller and for that we will need the delegates and the
presentation context provider. So let's just comment these in and let's just
take care of our delegates and I will just create extensions here. So extension
and that is our log in and view controller and we are going to set the AS
auth, it's a mouthful. So Authorization Controller Delegate, okay.
So that's the first one and the other one again is extension login view
controller ASAuthorization Context Present... Now! So it's ASAuthorization
Controller Presentation Context Providing. So it is a long long one.
So now let's add and the required functions here. So let's click on fix here and what
we need here is just return self dot view dot window and with an exclamation
mark there. So under the authorization controller delegate we will choose let's
say authorization controller and here we have the first one did complete with
authorization and the second one authorization controller did complete
with error, okay. So what do we want to do when we do get back an error? Well we
don't want to navigate away from this login view controller, we just want to
show an alert. So I would just simply copy and paste this out it's much faster.
So there we go. I'm just going to add that in. So we
have our alert controller and we are just going to present the errors
localized description with an okay action. So that is that. Now what do we
want to do in the complete with authorization. So let me just copy this
out because it's quite a chunk of code and it's easier once you see the code.
There we go. So we have coming back an authorisation and
then we check our credential and if the credential is an Apple ID credential
then we do know that this isn't coming from,
keychain iCloud keychain. It does have a user and we can safely go and register
our new account or sign in with an existing account. Now if we don't have an email
then we want to register a new account, if we do then we sign in with an
existing account. Otherwise we get back a password credential and that is coming
from iCloud keychain and then we just want to sign in with user and password.
Now what are these Sign In with existing account and so on? Well this is where you
have to take it and put it onto your database and of course you have to check
if the user is being saved and so on so you come back and I have these private
functions already here that you have to basically implement later on and connect
this authorization with your database. Basically what I do here is just go
ahead and print out the potential of our user and I just say that okay
I'm finished delegate did finish old and dismiss our login view controller.
Of course you will need to save your data to your database and then we'll turn
some results and so on and handle those. Okay and that is what you have to do if
you have on database, okay. So I believe we are quite ready here, let's just a
build and run and see how all of this looks.
Okay, let's Sign In with Apple. Let's hit continue here.
Let's just wait for that and indeed we have dismissed our login view controller
and we do have our user identifier and that is because we are signed up to
receive delegate methods from our login view controller delegate and it's this
finished and our let me just show you again our profile view controller has
its delegate set to self for our login view controller and here we handle that
we are just setting the labels text to our user identifier from our user and
defaults, okay. Now that was that. Now if you want to test this out what do you
have to do? Well it's pretty straightforward you just go into the
scene delegate and your before you do anything all you have to do is add in
here user default standard set and an empty string for our Sign In with Apple
Manager user identifiers key and that will clear everything. So let's just
build and run and test this out if this really worked and I just see, indeed the
Sign In with Apple popped up and we can just sign in again, there we go.
Let's just wait for that, finished and we are on, great, awesome work. Now that is it for
learning how to add Apple Sign In into a UIKit project. Hope you enjoyed this
video if you did go ahead and hit that notification bell because in my next
video I will be talking about transforming views in SwiftUI with the
help of modifiers. So if you are interested make sure to subscribe to the
channel and while you wait for that video make also sure that you check out
these two videos that I have over here. I talked a lot about iOS development, SwiftUI
and coding practices in general. So as usual I will see you in the next one.