Hey buddy what's going on says Caleb with Debs Lopes dot com.
And in this video we're going to actually write the code that we need to do to get accelerometer updates
using see a motion manager so go ahead and pull open your project here and what we're going to do is
we're first woman clothes the assistant editor and then just simply go into our view controller here.
Now what we need to do before we begin anything is import a library called core motion.
OK.
Because that's the one we just talked about in the previous video.
So go ahead and type import core motion.
OK.
And this is going to give us access to all that cool stuff accelerometer gyroscope magnetometer all
that stuff.
That's all included in core motion.
Now if you've ever used the map kit framework or Core Location you know that there is something called
a location manager and actually now that I think of it that's part of C-L location manager.
Now the location manager is in charge of managing the location.
Same with core motion there's going to be something called a CM motion manager that's in charge of managing
the motion.
OK.
So we're going to actually create and so we're actually going to create a variable called Motian manager
var motion manager and it's going to be of type CM motion manager.
Now go ahead and put an exclamation mark there because it's not yet used but we are going to instantiate
it later in view to load when we do need it.
So when the view loads in view load We're going to go ahead and instantiate it like slow motion manager
equals CM motion manager okay and instantiate it there with some parentheses.
Now all we need to do to start keeping track of accelerometer updates is literally just to go like this
motion manager dot start accelerometer updates that easy I promise.
So I want you to notice though that if I type start you can see start Jairo updates start device Motian
updates Macken magnetometer updates and accelerometer updates so we can get all that good stuff.
But we want to control what happens whenever the accelerometer is updated right.
We want to update our labels with the different access data.
So go ahead and choose the function with the completion handler.
Press enter and the operation queue here is just going to be main.
All right.
So we're just doing this on the main queue.
And for the handler I want to I want you to check out what's inside of it so click on the placeholder
there and press enter.
And as you can see it returns an instance of S.M. accelerometer data that's optional as well as an optional
error.
And before we actually dive into this we're going to create a function that has both of these parameters
and we're going to actually call that where the completion handler is.
And it's just kind of an easier way to compartmentalize it so we don't have to put everything up here
in view to load.
So go ahead and type a function here func and we'll call the update labels because that's what the function
will do.
And just like our completion handler up here we're going to go ahead and write a property here for data
of type S.M. accelerometer data.
It's going to be optional like so then we're going to have an error of type error and it's also going
to be optional.
OK so now that we have the two required properties for the completion handler watch what we can do.
You can actually go ahead and call this again.
We're going to put on the main queue and I can just call update labels okay and I can actually even
get rid of these as well.
So we just have update labels because this function here does or at least has the parameters that get
passed when this accelerometer update is completed.
We passed it into data.
And if there's an error we pass it into the error OK.
So I want to go ahead and create a constant that's going to hold the value for the data that comes in.
And we're just going to print it out to see what raw data we get.
So I'm going to create a constant using guard let Because this comes in as optional.
So guard let Excel or turr data data is going to be equal to data.
Else we're just going to return.
OK.
So if there's an error we're going to go ahead and return or if there is no data if it's nil.
We're also going to return.
So now that we have our accelerometer data let's just go ahead and print it.
So as soon as it updates we're going to just print accelerometer data.
Ok so I don't know about you but I want you to try this out.
I'm pretty excited to see how this works.
We created a motion manager using cmd motion manager we instantiated it in view to load and we started
accelerometer updates on the main queue and we were returning all the values from our accelerometer
through this function called update labels and they were simply printing out the data there.
Now I want you to look at the description here.
It says it starts accelerometer updates on an operation queue and with a specified handler.
Now if you want it to stop you need to call stop accelerometer updates in our app.
We don't necessarily want it to stop.
But in an app where maybe you only need it for a certain period of time.
That would be a good thing to call.
And basically the handler is what is going to pass back that data that's going to print out.
So let's go ahead and I'm going to build and run it on my phone and we'll see what we get.
So build and run and as soon as it's ready we're going to go ahead and pull it up here on the screen
and I'll show you using my device and building it to my actual iPhone because the simulator device doesn't
actually have an accelerometer even a virtual one so you have to build it to an actual device and check
out what's happening right here on the screen do you see this.
We're getting a lot of printouts and as you can see this is a very exact amount of data that goes to
like six or seven decimal points.
So what I can do is as I move it you'll see the values start to change.
So I'm actually moving my device here and the numbers are changing.
Now we're going to set it up later so that we only get a certain number of decimal points at the end
and therefore it will appear to be updating a little less frequently because this is actually creating
a lot of updates and we don't necessarily want to be printing that much that much data even though the
data will be coming in and iOS can handle it.
We're going to actually go ahead and set it so that when we move our device we're just going to reduce
the amount of decimal points so that it changes less sporadically it's kind of hard to look at to be
honest.
So go ahead and kill it and we're going to go ahead and reduce that down to the bottom.
But guys we are getting accelerometer data printing out now literally all we have to do is pull out
the individual data for each individual access X Y and Z and display it when ever the accelerometer
is updated.
So that will happen in the next video.
We're going to handle our results.
We're going to pull it out.
We're going to do some string formatting all kinds of fun stuff so let's head over to the next video
and let's get our accelerometer data showing up in our labels.
I'll see you there.