1
00:00:06,980 --> 00:00:10,260
Hey buddy what's going on says Caleb with Debs Lopes dot com.

2
00:00:10,300 --> 00:00:15,580
And in this video we're going to actually write the code that we need to do to get accelerometer updates

3
00:00:15,820 --> 00:00:21,580
using see a motion manager so go ahead and pull open your project here and what we're going to do is

4
00:00:21,580 --> 00:00:27,440
we're first woman clothes the assistant editor and then just simply go into our view controller here.

5
00:00:27,670 --> 00:00:33,740
Now what we need to do before we begin anything is import a library called core motion.

6
00:00:33,740 --> 00:00:34,020
OK.

7
00:00:34,030 --> 00:00:36,130
Because that's the one we just talked about in the previous video.

8
00:00:36,140 --> 00:00:40,690
So go ahead and type import core motion.

9
00:00:40,990 --> 00:00:41,700
OK.

10
00:00:41,800 --> 00:00:48,880
And this is going to give us access to all that cool stuff accelerometer gyroscope magnetometer all

11
00:00:48,880 --> 00:00:49,370
that stuff.

12
00:00:49,540 --> 00:00:52,390
That's all included in core motion.

13
00:00:52,420 --> 00:01:00,370
Now if you've ever used the map kit framework or Core Location you know that there is something called

14
00:01:00,400 --> 00:01:05,490
a location manager and actually now that I think of it that's part of C-L location manager.

15
00:01:05,590 --> 00:01:10,330
Now the location manager is in charge of managing the location.

16
00:01:10,330 --> 00:01:16,450
Same with core motion there's going to be something called a CM motion manager that's in charge of managing

17
00:01:16,450 --> 00:01:17,080
the motion.

18
00:01:17,080 --> 00:01:17,670
OK.

19
00:01:17,920 --> 00:01:24,490
So we're going to actually create and so we're actually going to create a variable called Motian manager

20
00:01:25,360 --> 00:01:32,930
var motion manager and it's going to be of type CM motion manager.

21
00:01:32,950 --> 00:01:39,430
Now go ahead and put an exclamation mark there because it's not yet used but we are going to instantiate

22
00:01:39,430 --> 00:01:41,910
it later in view to load when we do need it.

23
00:01:41,950 --> 00:01:48,820
So when the view loads in view load We're going to go ahead and instantiate it like slow motion manager

24
00:01:48,970 --> 00:01:55,260
equals CM motion manager okay and instantiate it there with some parentheses.

25
00:01:55,270 --> 00:02:02,590
Now all we need to do to start keeping track of accelerometer updates is literally just to go like this

26
00:02:02,890 --> 00:02:08,830
motion manager dot start accelerometer updates that easy I promise.

27
00:02:08,830 --> 00:02:15,940
So I want you to notice though that if I type start you can see start Jairo updates start device Motian

28
00:02:15,940 --> 00:02:21,650
updates Macken magnetometer updates and accelerometer updates so we can get all that good stuff.

29
00:02:21,730 --> 00:02:26,100
But we want to control what happens whenever the accelerometer is updated right.

30
00:02:26,160 --> 00:02:30,190
We want to update our labels with the different access data.

31
00:02:30,250 --> 00:02:34,400
So go ahead and choose the function with the completion handler.

32
00:02:34,600 --> 00:02:39,080
Press enter and the operation queue here is just going to be main.

33
00:02:39,100 --> 00:02:39,590
All right.

34
00:02:39,790 --> 00:02:41,630
So we're just doing this on the main queue.

35
00:02:42,010 --> 00:02:48,400
And for the handler I want to I want you to check out what's inside of it so click on the placeholder

36
00:02:48,400 --> 00:02:49,880
there and press enter.

37
00:02:49,960 --> 00:02:56,140
And as you can see it returns an instance of S.M. accelerometer data that's optional as well as an optional

38
00:02:56,350 --> 00:02:56,680
error.

39
00:02:56,680 --> 00:03:02,860
And before we actually dive into this we're going to create a function that has both of these parameters

40
00:03:03,130 --> 00:03:07,210
and we're going to actually call that where the completion handler is.

41
00:03:07,210 --> 00:03:11,830
And it's just kind of an easier way to compartmentalize it so we don't have to put everything up here

42
00:03:11,830 --> 00:03:12,840
in view to load.

43
00:03:13,120 --> 00:03:18,730
So go ahead and type a function here func and we'll call the update labels because that's what the function

44
00:03:18,730 --> 00:03:19,810
will do.

45
00:03:20,050 --> 00:03:26,440
And just like our completion handler up here we're going to go ahead and write a property here for data

46
00:03:26,830 --> 00:03:29,310
of type S.M. accelerometer data.

47
00:03:29,390 --> 00:03:36,460
It's going to be optional like so then we're going to have an error of type error and it's also going

48
00:03:36,460 --> 00:03:37,410
to be optional.

49
00:03:37,690 --> 00:03:43,000
OK so now that we have the two required properties for the completion handler watch what we can do.

50
00:03:43,040 --> 00:03:45,120
You can actually go ahead and call this again.

51
00:03:45,280 --> 00:03:51,040
We're going to put on the main queue and I can just call update labels okay and I can actually even

52
00:03:51,040 --> 00:03:53,200
get rid of these as well.

53
00:03:53,200 --> 00:03:59,770
So we just have update labels because this function here does or at least has the parameters that get

54
00:03:59,770 --> 00:04:03,250
passed when this accelerometer update is completed.

55
00:04:03,400 --> 00:04:04,440
We passed it into data.

56
00:04:04,450 --> 00:04:07,740
And if there's an error we pass it into the error OK.

57
00:04:08,170 --> 00:04:14,020
So I want to go ahead and create a constant that's going to hold the value for the data that comes in.

58
00:04:14,260 --> 00:04:17,190
And we're just going to print it out to see what raw data we get.

59
00:04:17,230 --> 00:04:23,300
So I'm going to create a constant using guard let Because this comes in as optional.

60
00:04:23,410 --> 00:04:33,140
So guard let Excel or turr data data is going to be equal to data.

61
00:04:33,220 --> 00:04:35,280
Else we're just going to return.

62
00:04:35,480 --> 00:04:36,270
OK.

63
00:04:36,280 --> 00:04:42,010
So if there's an error we're going to go ahead and return or if there is no data if it's nil.

64
00:04:42,040 --> 00:04:43,090
We're also going to return.

65
00:04:43,270 --> 00:04:47,630
So now that we have our accelerometer data let's just go ahead and print it.

66
00:04:47,650 --> 00:04:51,640
So as soon as it updates we're going to just print accelerometer data.

67
00:04:51,860 --> 00:04:54,970
Ok so I don't know about you but I want you to try this out.

68
00:04:55,000 --> 00:04:57,150
I'm pretty excited to see how this works.

69
00:04:57,160 --> 00:05:04,690
We created a motion manager using cmd motion manager we instantiated it in view to load and we started

70
00:05:05,510 --> 00:05:12,590
accelerometer updates on the main queue and we were returning all the values from our accelerometer

71
00:05:12,890 --> 00:05:17,290
through this function called update labels and they were simply printing out the data there.

72
00:05:17,300 --> 00:05:19,080
Now I want you to look at the description here.

73
00:05:19,280 --> 00:05:25,430
It says it starts accelerometer updates on an operation queue and with a specified handler.

74
00:05:25,430 --> 00:05:30,200
Now if you want it to stop you need to call stop accelerometer updates in our app.

75
00:05:30,200 --> 00:05:32,360
We don't necessarily want it to stop.

76
00:05:32,360 --> 00:05:36,040
But in an app where maybe you only need it for a certain period of time.

77
00:05:36,050 --> 00:05:37,750
That would be a good thing to call.

78
00:05:37,910 --> 00:05:43,370
And basically the handler is what is going to pass back that data that's going to print out.

79
00:05:43,370 --> 00:05:47,060
So let's go ahead and I'm going to build and run it on my phone and we'll see what we get.

80
00:05:47,060 --> 00:05:51,530
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

81
00:05:51,530 --> 00:05:58,130
and I'll show you using my device and building it to my actual iPhone because the simulator device doesn't

82
00:05:58,130 --> 00:06:04,010
actually have an accelerometer even a virtual one so you have to build it to an actual device and check

83
00:06:04,010 --> 00:06:07,070
out what's happening right here on the screen do you see this.

84
00:06:07,100 --> 00:06:13,010
We're getting a lot of printouts and as you can see this is a very exact amount of data that goes to

85
00:06:13,010 --> 00:06:15,620
like six or seven decimal points.

86
00:06:15,830 --> 00:06:19,970
So what I can do is as I move it you'll see the values start to change.

87
00:06:19,970 --> 00:06:24,190
So I'm actually moving my device here and the numbers are changing.

88
00:06:24,190 --> 00:06:29,680
Now we're going to set it up later so that we only get a certain number of decimal points at the end

89
00:06:29,680 --> 00:06:34,970
and therefore it will appear to be updating a little less frequently because this is actually creating

90
00:06:34,970 --> 00:06:40,850
a lot of updates and we don't necessarily want to be printing that much that much data even though the

91
00:06:40,850 --> 00:06:44,120
data will be coming in and iOS can handle it.

92
00:06:44,120 --> 00:06:49,280
We're going to actually go ahead and set it so that when we move our device we're just going to reduce

93
00:06:49,280 --> 00:06:54,890
the amount of decimal points so that it changes less sporadically it's kind of hard to look at to be

94
00:06:54,890 --> 00:06:55,400
honest.

95
00:06:55,400 --> 00:07:00,530
So go ahead and kill it and we're going to go ahead and reduce that down to the bottom.

96
00:07:00,630 --> 00:07:06,320
But guys we are getting accelerometer data printing out now literally all we have to do is pull out

97
00:07:06,320 --> 00:07:13,820
the individual data for each individual access X Y and Z and display it when ever the accelerometer

98
00:07:13,820 --> 00:07:14,660
is updated.

99
00:07:14,660 --> 00:07:17,230
So that will happen in the next video.

100
00:07:17,270 --> 00:07:18,640
We're going to handle our results.

101
00:07:18,650 --> 00:07:19,690
We're going to pull it out.

102
00:07:19,690 --> 00:07:24,500
We're going to do some string formatting all kinds of fun stuff so let's head over to the next video

103
00:07:24,770 --> 00:07:27,910
and let's get our accelerometer data showing up in our labels.

104
00:07:27,910 --> 00:07:28,400
I'll see you there.
