Do you want to learn how to create universal custom frameworks in Swift?
Well, you're in good place because in this tutorial I will teach you how to do
just that. Make sure to check out the chapters so you can jump through the
timeline. Alex here from rebeloper.com and I want
you to hit that SUBSCRIBE button and open up Xcode, let's dive in.
So let's dive into frameworks. So why would you want to create a framework?
Well because frameworks are kind of hiding
everything in the implementation part.
And this is a good use case, secrets, top secret manager
and yeah what we are doing in this is initializing the manager with the key,
of course you want to have a key and if it is authorized
then we can read our secret. Now these are the approved keys
that we can use but of course you might want to
have a fetch request here from a server to verify
that key, okay. So basically that is it. Before we go forward with how to
create this framework, let me just tell you that
if you have a public class or variable or function or initializer
that will be available for the public basically, they can use it
in their projects. But if it's private as you can see here and here
then it will not be available. You can just omit private and
then it will be internal by default also internal
means that it's internal for this framework basically, okay. So what we are
going to build out is a secret reader framework.
So let's do that from scratch. Let's open up Xcode
and inside Xcode let's create file, new and project, okay. So file, new,
project. Let me just resize this so you can
see it a little bit better. Awesome! Now you want to scroll all the way down
and select framework. So that is what we are going
to create next. And I will name these secrets.
Awesome and let's click on the next here and yeah let's just create a new file here.
So let's just drag and drop a new folder and
let's create it right over here and I will
create a git repository on my Mac so you can
later check out the code. Okay! So as you can see all we got here
is a secrets.h file and an info.plist file and of course
the product. Now if you right click on the product and
show it in the Finder you will see that here is actually
our framework and yeah if you just go ahead and yeah this is a mac catalyst
but we want to select. Let's see iPhone. Let's say the Pro Max, okay.
If we build and run. So let's hit command B.
Okay! It has succeeded. Now let's show in finder.
You can see that before the catalyst you have an iPhone
simulator and basically that is what we want. For now we can just select both
of them and delete them. Let's move to the bin.
Okay! So yeah that's that. How to add a new
file here, let's just create a new file and I will call this secrets manager.
So click on next there, there we go. Secrets manager. Let's create that.
Okay and now I will just simply go ahead and copy all of this out
and do the appropriate changes because, yeah
it's straightforward. I don't want to waste time
just typing that out. So secrets manager. There it is and yeah let's just hit
command B right now and build has succeeded, awesome.
and of course if we just go to the Finder the
iPhone simulator framework has been created. How awesome
is that, okay! So what you might want to do is just create a kind of a
demo project where you can easily test this out. So let's just go here
and as you can see we have the target as the framework.
Let's click on this plus button here and let's create
a new Xcode project. So let's go down here single view application will be
fine for us and I will name this secrets
demo or whatever you'd like to do that. So secrets demo.
Let's click on finish here and as you can see the secrets demo app has been
added and of the target basically, okay.
So what we want to do is just under view controller do some coding and
again I will just copy and paste this because it's really straightforward.
Let's just copy this out, move back. First thing first
we want to import secrets, there we go.
And then we just want to paste this here, so there we go.
Let's hit command B of course yeah we are
now, you can't really see that but we are currently in the secrets demo. So let's
go there and here yeah as you can see
this is a secret manager. There we go. If we hit command B,
we should be able to have this basically this use of unresolved
identifier. So what we want to do next is just go
ahead and select that target, there we go.
And just scroll all the way down and as you can see frameworks libraries and
embedded content. We have none. So let's just click on the
plus sign here and just add the secrets of framework.
Okay! So let's hit command B again, as you can see on the demo,
again and let's just write this all over again because it's not secret
manager it's secrets manager
and here we have our key and one, two, three, four, five.
I do know that is a valid key. So what we want to do now
is just build and run. We don't really care
about the simulator. All we want to see is either of these print statements and
now we should be able to print out the secret.
Let's just wait for that, there we go and here it is.
The secret is I love custom frameworks and indeed
that is what we have set right over here. Awesome! Now let's just double check if
we just remove one from the key, one letter there
from the key. Build has succeeded, that's okay but failed to
read secret. Really, really nice, okay.
So what you want to do is just add this in a separate project because yeah this
is just the demo project. So let's just create that. So under Xcode,
file, new let's create a new project again and I will name this
project. Let me just resize this real quick.
I will name this project secrets reader. So not framework, but a single view
application, next, let's click on next there
and create that.
Awesome! Now what we want to do is just add some frameworks that are manual
frameworks, so let's create a group here.
Okay! So how would you go about adding that new framework?
Well let's just move back here and as you can see here
if we just jump to show in Finder for the
secrets dot framework, we see that we have this
debug iPhone simulator and secrets dot framework.
So that is what you want to kind of copy and paste into
that other project. So let's go back to Xcode
and let's just drag and drop this. So secrets dot framework, there it is.
Now make sure copy items if needed is checked, create groups is checked and of
course the target is checked. So let's click on finish here
and now you will see that it has appeared on the
frameworks libraries and embedded content.
And currently it's do not embed and let me just
build and run this project and it has of course failed. All you have
to do is just select embed in sign or embed without signing.
So let's go embed and sign and let's just select
iPhone Pro Max, okay. Let's just build and run and now you won't be
seeing that error again. Build has succeeded.
Let's just wait for the print statement. Yeah, because we haven't added the print
statement, let's just do that. So let's just copy
this out. Let's go back, view controller
and of course we need to import secrets. There we go. Now let's hit command R to
see that build and run again and have. Yes!
Failed to read secret because we don't have
that key ready. Let's just build and run and see if we
do get that secret. The secret is I love custom frameworks. Really, really
nice, okay. So now let's just select my phone. I have disconnected
and let's just build and run on my phone and as you can see build has
failed and yeah that is and you could not find
module secrets and so on. So what this means is that when we are
creating and if you just take a look at the Finder, we have created a secret
framework for the simulator. So we need to create
one for our device too. So let's just
go back to our framework, secrets dot framework
and then select our phone and hit command B and now we should
compile...
Build has succeeded. Awesome, okay. So yes.
Secrets demo. That's one thing but I really meant
this on the framework. So let's hit command B again.
Build has succeeded. Awesome. Now let's take a look at the Finder.
So now we have iPhone OS and again we have a secret framework
and yeah we could just drag and drop that
also into our other project. Let's just see here, but then we would
have two frameworks so yeah that is not quite what we want.
We want universal frameworks. So to do that we
want to create another framework and that we will do
by creating a new target first. So hooray! Let's just go here, secret
reader. Secrets this is the one. Okay! So now we have our secrets
framework. Our secrets demo and let's just click on the plus sign
here and select cross platform and aggregate. So what we are going to do is
kind of take those two. The OS and the simulator
and create a universal one. So the product name let's say
secrets universal. That would be a really nice name for our
product. Let's click finish here and what we want to do here
is basically go to our build phases and click on this
plus button and create a new run script.
Okay! Just open that up. Let me make some room here
and in here we will add a new script. Now what type of script?
Delete everything like that commented line and go ahead and check out the link in
the description where you will have could download this script.
Now it's called universal framework script.
Go ahead and check it out. I will just copy and paste this in here,
yeah I'm not a good scripter myself, well experienced scripter myself. I just
kind of made this through searching from
StackOverflow and on GitHub. Yeah go ahead and check it out.
I have also added some comments there. So now what we want to do, as you can see
I added this script into the secrets universal
target. What we want to do is just select
this target and then here just select
generic iOS device. So the universal target and generic iOS device. Of course
make sure that you have already created
these iPhone OS and iPhone simulator frameworks. Yes. So those two are there,
should be there. Okay! Let's go back and now let's hit
command B and this should build in a moment and
yes, build has succeeded. So now let's take a
look at what we got here. As you can see a new debug iOS universal
has been created. Secrets dot framework and this is what
we are basically going to drag and drop into
our other project secrets reader.
So let's just remove this. So delete,
move to trash, okay and now let's just drag and drop this from the
iOS universal, awesome. Let's click finish.
Make sure all of those are checked. Let's hit command B,
so we can see that it all compiles and build has succeeded.
Awesome and now we can build and run. Build has succeeded.
Yes! Let's unlock our device here.
Yes and this is again image not found. You just need to enable it,
so let's just go all the way down and do not embed,
change that to embed and sign. Yeah! You will get into this error most
probably. So there we go. Build has succeeded.
And let me just unlock that and yeah secret I love
custom frameworks. Awesome! Now let's see if this
also works on our simulator and if it does then yeah build has
succeeded, how awesome is that and the secret is I love custom frameworks.
How awesome is that! Now you know how you can create
universal custom frameworks. So this is how you can hide your
implementation through a custom iOS framework.
Now if you are stuck go ahead and check out the link in the description,
I am available for one-on-one mentoring. Now I want you to hit that notification
bell because in the next video we are going to demystify
UITableViews. That's right UITableViews to the rescue.
So while you wait for that make sure to check out these videos as well.
I talk a lot about UI, Swift SwiftUI in general
and as usual I will see you in the next one.