Hi, Alex here from rebeloper.com and welcome back to this last episode of our
extension series where we unveil some fun and cool extensions and today we are
going to get the topmost view controller with the help of a UI application
extension. So without further ado let's jump right into it.
So le me just jump into the testing extensions fund where I built all of this up and show you what
we are going to build out today.Again go ahead and click the link down below to
get the resources and the starter project. So here we have a simple press
me button and let's just press it and success. You tapped me from inside the UI view.
Now, what? Well I am going to show you what we did in this episode of our
extensions. Okay, so let's just stop that, minimize it
and take a look at our extensions fun project. So what we want to do here is
type in application and if you are not familiar what I'm doing here go ahead
and take a look at the first video in this episode series where I explain
everything about this starter project. Now we are going to build this out and
we are going to present the application view controller, okay. It's press me and
it does nothing quite frankly right now. Let's take a look at the application
view controller, okay. Here we have an application customer view and
we are going to take a look at it in just a moment we are simply adding it to
the super view and we are setting its height to 40 pixels and with the help of
tiny constraints, okay. What is this application custom view. Let's take a
look at that. Well it's basically a UI view where we added our a button
and that button has a handle button tapped and of course we add that to our
as a sub view to our view and we Center it in the super view. Now this handle
button tapped basically creates an alert controller it has an okay action added
to this alert controller but how do we actually present it because now if we
would do this on view controller we could just simply grab the view controller
itself and present this alert controller on that view controller but we are in a
UI view. So where is the new controller well there is no view controller but we
want to grab the topmost view controller. So let's go to our extensions and scroll
all the way down to the UI application extension and here we have function
called get topmost view controller base and you will understand just a moment
what why do we need this base. For now let's just know that it returns an
optional UI view controller. I'm just going to paste the code in here and go
through it line by line just so I don't get any typos. So as you can see we
have three if let statements, one is for the UI navigation controller the second
one is for the UI tap bar controller and the third one is for the basic UI
view controller like the presented view controller. So we are iterating through
the nav that is the base that we added here and if we didn't add anything there
then it is the UI application shared key windows root view controller, again
that's mouthful but we are checking if the base is a navigation controller,
a tap bar controller or a simple UI view controller and if it is a navigation
controller then we set the base to be our navigation controller to visible
view controller. The same goes for the tab bar controller
but here we grab the selected view controller and add that selected to our
base and again we call again on this. Now if it is a simple view controller that
we just set the base to be the base of our presented view controller and then
of course we return the base. Now it sounds kind of complicated but really it
isn't and frankly you don't really need to understand all of this and you don't
really need to care what you do know is that now you have an extension on UI
application that is called get the most, topmost you control. So let's go back to
our application view controller or rather yet the custom view because yeah
we want to grab the topmost view controller in a custom view and let's
grab the topmost view controller. So let topmost view controller and that is UI
application and get topmost view controller. As you can see we can add a base here if
we wanted to but this is just fine, okay. And I believe we need to guard let
unwrap it but we will see and now we just need to grab yeah it is an optional
so let's guard let unwrap it. Guard let, else
and return, okay. Now that we do have our topmost
viewcontroller. TopMost view controller we can present our alert controller. So present
our view controller and that is alert controller animated true and with
the completion of a nil, Okay and that is it.
Now you can present an alert controller from your view, okay. Let's build and run
and see how that looks like and again you don't really need to but if you are
required to do so then you can do with the help of this extension. S o let's
click on press me. There we go, success you tapped me from inside a UI view.
Let's tap on cool, okay. And indeed that is really cool I hope you enjoyed this
video and I hope you enjoyed the whole series again take a look at the first
video and if you look at all of the videos in the series there are 10 in
total and this was the last video about extensions. Well maybe I come around
extensions later time but this is the last end and this tutorial series
about awesome extensions. I hope you enjoyed this one if you did go ahead and
give it a thumbs up, subscribe to the channel and hit that notification bell to
get notified of new videos when I put them up on the channel and as usual I
will see you in the next one.