Importance of Natural Resources

Manage Different Environments in Xcode (NEW)


Hi Alex here from rebeloper.com and
welcome back to the channel! Today we are going to talk about how you can manage
different environments in Swift with ease. So without further ado let’s get
started! (MUSIC PLAYING) Now what do I mean about how to manage different environments? Let me
tell you a use case what if your app uses let’s say Firebase for storing data
and of course retrieving them. Once you create your app and you have coded
along the way and you release your app then you just think about it
you want to create another feature. Now if you try to test your app against
their database well it kind of… could have kind of a messy. Because you are
actually fetching adding and retrieving objects and data with let’s say the
production database. And that is why it is a good idea to create a separate
branch on the database that is the development database. Now that is what we
are going to talk about today and how to add it into your own apps. We are going
to start off with the most easy ones and the most fast ones to implement and then
we are going to take it from there. And we are going to talk about some advanced
techniques that I surely recommend that you used these last ones. So what does
this look like well first of all let’s take a look at an example database. So
here we have a production branch of our database and here we have a development.
And this is what we are trying to achieve now. For the development let’s
say we have API a URL and same URL but…. well not the same URL… a
different URL for the production and this is where the production URLs
go to and this is where the development ones go. So this is kind of what we
are trying to achieve. Now let’s create a new Xcode project. So open up your Xcode
and let me just minimize this. This is what we are going to achieve. Let’s go
to Find & New. And let’s create a new project and it will be a simple single
view application. Let’s click on next project name… Let’s click on next. And
let’s go and put it… where is on my desktop… there it is… Okay and I will create a Git
repository. So you may just check it out on a GitHub. Ok, let’s create that and
here we go! Let me just manage this a little bit so you may see. Okay, now if we
want our app on the iPhone X simulator and let’s do that now we will just
simply be in let’s say production environment. But we want to create a
development and a production one. And for that we could use some tricks and the
first one and let’s just close this… And let’s go into our app delegate and let’s
say just… close this… Let’s say we want to print out the environment and that is
all we want to do. So let’s: print(“Environment is:”). The environment let’s add it to a
variable.Okay so… var environment=”Development” Okay and now let’s print this out. Okay,
let’s build and run and see what happens. Rather yet this could be a ‘let’.
I will change that later on. Okay and let me just show you in the console. So
environment is a development. I will just changes this to a ‘let’. Okay now what do
we want to do. The other thing that we could do is… let me just close this. Now
currently we are in environment. Now we want to let’s say at a later time be
in production. So we just copy this out paste it in there and comment out. And
instead of development we just type in “Production”. Okay, build and run again and
this time the environment is… Let’s wait for it… Production! Great. Now this is a
pretty dirty way to do this and of course after that we may check up on
this environment variable, if we set it to a global variable. But this is a plain
old simple not that safe but a simple way to just simply comment out the
environment. Okay now next up is a rather more challenging one and that is with
global variables and an enum. And for that I will just simply create a global
variable. Let me just comment this out. And go up here and let me just create
an enum first. Okay and let’s call this environment. And
the first case is of course development. And that’s make this… not a capital D. And
this will be of course development. Let’s make this string enum. Okay, and the
other case would be of course production. And finally we want to add a non
environment just for checking purposes. Okay, and let’s copy this again. Okay,
now let’s declare a global variable and that is… ‘var environment’ and this will be
of type ‘Environment’ the enum that we have just declared. And for now let’s set
it to none. Okay, now this is loaded once the app is loaded. And what we wanted to
do is go ahead in here and when the application launches we want to check
for the environment global variable. So switch and we check for the our
environment variable and if it is none then let us just do some things and just
delete this. Let’s print out… Oh we should do the same. Okay, let’s go here and copy
this out and paste it in there. Okay so
‘environment is’ and now we can know definitely that what the environment is.
So I’ll just type in here ‘none’. Okay, now we definitely know that this is it. And
let’s do the same for the other two cases. Okay now one last thing that we
want to add is actually the one thing that really sticks this off into
development or productions. So let’s just call our environment and set it to
let’s say first of all development. Okay let’s a build and run and now you should
see this printed out “Environment is: development”. Okay, and that is it. And if
we change. Now this is the counterpart of the commenting out things. All we have to
do is go into this environment statement and let’s say we are in production. And
at the time you want to check whether we want to cool this API in development or
in production you just need to uncheck this one. Otherwise you have this code
inside your project so you don’t have to go into every single file and let’s say
uncomment like with the previous example. Now this is kind of good but, if you
forget to set the environment this one lied to Production before you release
your app it will not tell you that you have an error and you could get into
trouble with your database. Basically all of your users will be uploading their
data to your development branch in your database and that
is not a good thing. Okay, so we should avoid this too. Now the last one that I
will show you now is a much safer way and for that we are going to use schemes.
So what you need to do is let’s say go to your target and duplicate it. Okay, and
let’s rename this environment and depth. Okay,
now you have two targets and this is a safe one because you will select your
Production environment when you just archive and upload your app to the App
Store. Now let’s go into our AppDelegate.Swift file and let’s see our pane
here. And as you can see the target membership is set to both the
environment and the dev target too. Now this is pretty good because we only have
to use one code base and that one code base will be used in the different
targets. Both or even more targets when you check. Now when you create a new
file make sure that you check both of that. So let me just go here and let’s
say… Let’s create a new file. It will be a Swift file next, and as you can see we
have a multiple target so let’s check this and let’s create it. And
there we go we have a new file and target membership is set to both. If you
forget just simply go here under… what is this… file inspector and just select the target membership to be both. Great! Now let’s go back and now select
your… well here… up here manage schemes and here you can see that the name of
the scheme is Environment Copy. Now you want to change this to Dev or
whatever you have set it. Okay, and now let’s close it. And one final thing
before we just simply code is just go into your Environment Dev target go
to a build settings and start typing in…. Other Swift Flags, so if you just type in
Swift Flags that will be okay. And we just go here and type in other Swift
flags. And as you can see we don’t have any flags just yet. So what you want to
do is double click here and type in and this is important -D and what
you really want to name your development let’s say in branch. And I will just call
this development. Okay and hit return and make sure in Debug and Release is the
same. Now if you just switch targets you will see that there aren’t any Swift
flags for the basic production target. Okay, now let’s go back to AppDelegate
and finally make some codes so I will just comment this out. And let’s do this
the new way. Okay, we will still use the environment a global variable, if we want
to check somewhere else. But this time around we can let’s say… set it up in a
different way and a more safer way. So let’s type in #if and not with
capital letters with small letters. And DEVELOPMENT sorry for that. And then type in: #else and than #endif. Okay, now what you want to do here is simple. Set the
environment to production. And here set the environment to production here. I’m
sorry and the development for the development brunch. Okay, now if we want
to run this environment switch again let’s comment this out and copy it and
let’s paste it here rather yet I will just delete it from here. Okay, now we
know exactly what the environment is depending on the target that we have
selected. So let’s test this out first of all let’s select our production
target and this is it. Let’s build and run and let’s see the console and it
says environment is production. And indeed we are set in production. Next up
is to test this out in the Dev target. Okay, same code let’s see what the
console prints out. Here it is environment is development. Now let’s
close this and let’s take a look at the simulator. As you can see the icon and
well… we don’t have an icon yet but it stays the same
and of course the environment that bundle display name stays the same. So I
want to make a little change. So when users or developers test this out
they know that we are in development or
in production mode. So for that first of all we are going to create two icons. I’m just simply… well first of all let’s delete the current one and I
have created two icon sets and let’s just copy and paste
them in here. One is for the original app icon and this is for the dev one. Now we
want to set them in the correct targets so let’s go to environment, then select
our target, go to general and make sure that under app icon source you have the
app icon. Now select your environment dev and just scroll down and change it to
the AppIcon-dev. Now let’s build and run again, and let’s see that in
action. Now click… build has succeeded, let’s stop
this and let’s take a look at the simulator. And as we can see the icon has
been changed and environment dev bundle display name has been set okay. And here is your environment dev copy
info.plist. Now what I want to do is just simply drag it in here and rename it.
Okay not copy but environment dev or maybe just Dev-Info.plist. Okay.
Let’s go again and build and run and as you can see the Info.plist file is
missing. Now let’s fix that because we have moved it and renamed it. Let’s go to
general and let’s choose Info.plist file and I will choose this step. I have
chosen it… let’s select another tab and we are ready. Okay, now what I want to
change here is let’s say… so for the display name and here… and let
us just select our environment, our production environment and let’s see the
icon and the bundle display name one last time. Okay, we are in production. That
is good. Let’s see our icon. We have our simple icon and it says production. Okay,
next up let’s create our a dev environment. Build has succeeded. Environment is development, pretty good.
And now let’s take a look… let’s stop this and take a look at the simulator
and as you can see it says dev and the icon has changed. Now this is what you
should do when you try to create different environments in your Xcode
project. Hope you enjoyed this video. What do you think? Is it necessary to
create these kind of environments? Most importantly the development environment
for your app… is it a waste of time or is it good for your overall project
management? Go ahead and comment down below! Share your thoughts and of course
if you liked this video go ahead give it a thumbs up and subscribe if you already
haven’t done so! And I will see you in the next one!


Reader Comments

  1. Nice video. This article mentioned the same solutions https://medium.com/flawless-app-stories/manage-different-environments-in-your-swift-project-with-ease-659f7f3fb1a6

  2. Hello Rebeloper, I could configure the way you told me. and it worked but i am having one issue. When i switch to Dev environment "Pods" doesn't work. Xcode start complaining about Pod not found. Please

Leave a Reply

Your email address will not be published. Required fields are marked *