0
1
00:00:00,210 --> 00:00:07,260
Now, in the last lesson, we built our Natural Language processing machine learning model that is going
1

2
00:00:07,260 --> 00:00:13,080
to be predicting sentiment or emotion from tweets that we give it.
2

3
00:00:13,230 --> 00:00:19,380
Now, in this lesson, we're going to get it set up and incorporate it into our Twittermenti app.
3

4
00:00:19,380 --> 00:00:25,710
So if you haven't already done so, make sure you head over to the GitHub page for Twittermenti-iOS12
4

5
00:00:25,800 --> 00:00:33,690
and go ahead and git clone or download, and unzip the initial skeleton project. And once you've done so,
5

6
00:00:33,690 --> 00:00:37,420
you should end up with this Xcode project.
6

7
00:00:37,620 --> 00:00:43,920
And just as we've done before millions of times now for all of our mlmodels, we're simply going to
7

8
00:00:43,980 --> 00:00:47,370
drag and drop our brand-new model,
8

9
00:00:47,400 --> 00:00:54,990
the TweetSentimentClassifier model into our project. So I'm just gonna drag and drop it into our project
9

10
00:00:55,410 --> 00:01:02,730
and make sure that under Destination, you check that "Copy items if needed" is ticked.
10

11
00:01:02,730 --> 00:01:04,950
So let's go ahead and click Finish.
11

12
00:01:05,010 --> 00:01:14,130
So that is now available inside our project as the class TweetSentimentClassifier.
12

13
00:01:14,140 --> 00:01:18,040
Now, the next thing we need to do is to get ourselves some tweets, right?
13

14
00:01:18,190 --> 00:01:26,620
And in order to pull the live tweet from Twitter, we're going to be using the Twitter API. And in order
14

15
00:01:26,620 --> 00:01:34,250
to do that, we first have to register to be a Twitter developer. So if you head over to 
15

16
00:01:34,250 --> 00:01:43,040
developer.twitter.com, and go ahead and click Apply. And Twitter has a number of tiers for their API. They have
16

17
00:01:43,040 --> 00:01:43,820
premium.
17

18
00:01:43,850 --> 00:01:47,000
They have enterprise and they also have standard.
18

19
00:01:47,030 --> 00:01:50,860
This is the free one that we'll be using for our project.
19

20
00:01:50,870 --> 00:01:55,640
So go ahead and click "Get started with standard access."
20

21
00:01:55,660 --> 00:02:03,010
Now, the first thing you have to do is to create a Twitter app and you have to do that at apps.twitter.com.
21

22
00:02:03,190 --> 00:02:09,170
And here you're going to click Create New App.
22

23
00:02:09,190 --> 00:02:11,880
Now, at this stage, you might be asked to log in.
23

24
00:02:12,310 --> 00:02:15,110
So you'll need a Twitter user account
24

25
00:02:15,160 --> 00:02:20,140
if you don't already have one. If you do have one, then just go ahead and log in to your normal Twitter
25

26
00:02:20,140 --> 00:02:24,520
account and you'll be able to get started creating an application.
26

27
00:02:24,520 --> 00:02:27,640
Now, the next step is you need to give your application a name.
27

28
00:02:27,640 --> 00:02:32,950
Now, I've called mine Twittermenti after the app that I have created,
28

29
00:02:33,250 --> 00:02:35,600
but you might need to give it a different name,
29

30
00:02:35,620 --> 00:02:38,170
so anything that makes sense to you is fine.
30

31
00:02:38,320 --> 00:02:42,850
Give it a brief description and then put in a website.
31

32
00:02:42,850 --> 00:02:48,940
Now, if you don't have a website, then you can go ahead and put our website which is www.appbrewery.co,
32

33
00:02:49,000 --> 00:02:54,520
dot C-O, and leave the Callback URL empty.
33

34
00:02:54,520 --> 00:02:59,550
That's mostly for when you try to use Twitter as a authentication.
34

35
00:02:59,560 --> 00:03:02,390
So, for example, log in with Twitter for your apps.
35

36
00:03:02,440 --> 00:03:03,550
We're not gonna be doing that.
36

37
00:03:04,480 --> 00:03:08,170
And finally, just check the developer agreement and click
37

38
00:03:08,170 --> 00:03:09,100
Yes.
38

39
00:03:09,280 --> 00:03:13,480
And then you can go ahead and create your Twitter application.
39

40
00:03:13,510 --> 00:03:18,850
Now, once you've created your application, you can see that you have a number of things here.
40

41
00:03:19,390 --> 00:03:22,400
And these are your API keys.
41

42
00:03:22,450 --> 00:03:29,890
And keep this page open because we're going to be using those API keys very shortly in order to authenticate
42

43
00:03:30,130 --> 00:03:33,310
our API requests to Twitter.
43

44
00:03:33,310 --> 00:03:38,500
Now, if we head back to the Twitter Documentation where it says "Get started,"
44

45
00:03:38,500 --> 00:03:43,300
you can see that we've already done the first step which is creating an app.twitter. 
45

46
00:03:43,300 --> 00:03:44,040
com.
46

47
00:03:44,050 --> 00:03:46,870
The next step is to start using the endpoints.
47

48
00:03:46,900 --> 00:03:54,520
So start making requests to the Twitter servers. And we can check out the documentation and API references
48

49
00:03:55,090 --> 00:03:57,510
and how to use the Twitter API.
49

50
00:03:57,580 --> 00:04:03,550
But there's also a lot of libraries and utilities that they point us towards which will make it much,
50

51
00:04:03,550 --> 00:04:07,150
much easier to work with the Twitter API.
51

52
00:04:07,150 --> 00:04:12,700
You can see that there are libraries built by Twitter and there are libraries built for the Twitter
52

53
00:04:12,700 --> 00:04:13,860
platform.
53

54
00:04:13,900 --> 00:04:20,800
Now, one of the most useful and simplest to use is something called a Swifter which is a Twitter framework
54

55
00:04:20,800 --> 00:04:23,690
for iOS and macOS written in Swift.
55

56
00:04:24,190 --> 00:04:29,800
And this is the library that we're going to be using in our project to make our lives really easy when we're
56

57
00:04:29,800 --> 00:04:32,350
trying to work with the Twitter API.
57

58
00:04:32,380 --> 00:04:38,620
Now, I've known many developers senior and junior like getting bogged down in the Twitter API Docs and
58

59
00:04:38,620 --> 00:04:45,940
getting stuck. And Swifter is just simply a much more pain-free way of working with the Twitter framework
59

60
00:04:46,300 --> 00:04:53,500
and it allows us to do everything that we can do with Twitch API, but provide, essentially, a wrapper around
60

61
00:04:53,500 --> 00:05:00,030
it that makes it much easier and much smoother to work with Twitter's API.
61

62
00:05:00,100 --> 00:05:07,920
Now, in order to use a Swifter, as they say here under "Getting Started," then we have to drag the Swifter
62

63
00:05:07,960 --> 00:05:16,810
Xcode project into our own project, and add either the Swifter iOS or Swifter Mac framework as an embedded
63

64
00:05:16,810 --> 00:05:25,460
framework. So I'll walk you through step by step what all of that means and how we can do it. But, firstly,
64

65
00:05:25,490 --> 00:05:31,840
we need to git clone this project. So, I'm going to copy the HTTPS address.
65

66
00:05:31,850 --> 00:05:41,360
I'm gonna head over to Terminal and I'm going to cd over to my desktop. And here, I'm going to git clone
66

67
00:05:41,500 --> 00:05:51,070
this particular address for Swifter, and then let's open it up. And now we have a swifter here.
67

68
00:05:51,070 --> 00:05:56,830
So if you look inside here, there's a whole bunch of things such as the Swifter demo for Mac., Swifter demo
68

69
00:05:56,830 --> 00:06:02,320
for iOS, the tests, and also the Swifter Xcode project.
69

70
00:06:02,320 --> 00:06:04,900
So we're going to follow these instructions to the latter.
70

71
00:06:04,990 --> 00:06:09,460
We're going to drag the Swifte Xcode project into our own project.
71

72
00:06:09,460 --> 00:06:16,180
Now, it's kind of important where you drag it into because it's very easy to say drag it into a project.
72

73
00:06:16,390 --> 00:06:23,300
But does that mean drag it into the folder, or drag it into the project, or drag it down here,
73

74
00:06:23,320 --> 00:06:24,480
what do you exactly do?
74

75
00:06:24,520 --> 00:06:26,970
So that's the next thing I'm gonna address.
75

76
00:06:27,010 --> 00:06:33,760
So once you've got the Swifter file open, you're going to click and drag it, and you're gonna put it just
76

77
00:06:33,850 --> 00:06:35,830
under where it says Twittermenti,
77

78
00:06:35,830 --> 00:06:45,640
so just above our README. And now we've incorporated the Swifter Xcode project into our Twittermenti
78

79
00:06:45,640 --> 00:06:50,480
project, so that we can tap into all the code that's inside that framework.
79

80
00:06:50,590 --> 00:06:58,840
So that's step 1 completed. Step 2 is add the SwifteriOS framework as an embedded framework.
80

81
00:06:58,840 --> 00:07:04,030
So how do we embed frameworks into our current project?
81

82
00:07:04,030 --> 00:07:11,270
So we need to head over to our General tab under the Settings for our Twittermenti app, and we're gonna
82

83
00:07:11,290 --> 00:07:21,280
go down here to Embedded Binaries, and we're going to add the SwifteriOS framework. And once we've added
83

84
00:07:21,280 --> 00:07:29,260
that, if we head over to our Build Phases, then we can check to make sure that the SwifteriOS framework
84

85
00:07:29,680 --> 00:07:33,340
is included in the Embedded Frameworks.
85

86
00:07:33,340 --> 00:07:40,120
And now let's just hit command B or go to Product and Build just to make sure that we don't have any
86

87
00:07:40,120 --> 00:07:44,810
errors and that everything works.
87

88
00:07:44,870 --> 00:07:53,330
Now, once you've done that, you might see that builder has failed, and you'll get three errors that are originating
88

89
00:07:53,330 --> 00:07:55,850
from the SwifteriOS framework.
89

90
00:07:55,850 --> 00:08:02,870
Now, if you don't see any errors, anything red on screen, but you're still getting build failed,
90

91
00:08:02,870 --> 00:08:09,680
then it's a good idea to just close down the project and just open it again. And then go ahead and try
91

92
00:08:09,680 --> 00:08:11,680
to build your app again,
92

93
00:08:11,840 --> 00:08:14,750
and it should trigger Xcode to show you the errors.
93

94
00:08:14,750 --> 00:08:17,860
This is something that I've seen sometimes with Xcode 10.
94

95
00:08:18,110 --> 00:08:25,520
So the three errors that are preventing our project from building successfully are all related to something
95

96
00:08:25,520 --> 00:08:31,820
called SFSafariViewControllerDelegate. And the error is telling us that this thing that we're trying
96

97
00:08:31,820 --> 00:08:39,770
to use, so you can click on it to see where it's being used and it's, of course, inside the Swifter code,
97

98
00:08:39,890 --> 00:08:48,200
it says that it's only available on iOS 9 or newer, and it's suggesting to us some things that we
98

99
00:08:48,200 --> 00:08:51,110
can do, but don't go with their suggestion.
99

100
00:08:51,110 --> 00:08:57,620
Instead, we're going to go into the Swifter project and we're going to select that framework SwifteriOS
100

101
00:08:57,670 --> 00:09:00,330
which we're trying to depend on.
101

102
00:09:00,650 --> 00:09:06,140
And then we're going to go into the part where it says "Deployment Target" and you can see it's got
102

103
00:09:06,140 --> 00:09:09,980
8.0 selected by default from the beginning,
103

104
00:09:09,980 --> 00:09:14,900
whereas the code that it's trying to use is only available in 9 or above.
104

105
00:09:14,930 --> 00:09:18,610
So let's go ahead and change this to 10.0.
105

106
00:09:18,860 --> 00:09:25,650
And if we go ahead and Build our app again, then you can see those errors will disappear.
106

107
00:09:29,160 --> 00:09:33,360
Now, once those errors have disappeared, we now get a warning,
107

108
00:09:33,360 --> 00:09:39,570
single warning, which says openURL, which is something that we're trying to use here or rather
108

109
00:09:39,570 --> 00:09:48,440
Swifter is trying to use over here, was deprecated, namely, that it was retired, basically, in iOS 10.
109

110
00:09:48,450 --> 00:09:55,830
Please use this other method instead which is URL with options and a completionHandler.
110

111
00:09:55,860 --> 00:10:02,580
Now, if you want to leave this, your app will work just fine without it. But if you are OCD like me and
111

112
00:10:02,580 --> 00:10:08,280
you want to get rid of all of your warnings, then you can go ahead and change this method so that it
112

113
00:10:08,340 --> 00:10:11,310
abides by these rules.
113

114
00:10:11,310 --> 00:10:18,600
Now, in order to change it, all we have to do is delete the rest of the call and type openURL
114

115
00:10:18,690 --> 00:10:24,650
again, and you can see this one with the line with the strikethrough is the one that has been deprecated.
115

116
00:10:25,950 --> 00:10:32,710
And instead, we have to use the method that's just called open with options and completionHandler.
116

117
00:10:33,180 --> 00:10:39,690
And so let's go ahead and hit enter so that it types it in. The URL that we need to put in here
117

118
00:10:39,870 --> 00:10:41,630
is the queryURL.
118

119
00:10:45,430 --> 00:10:47,770
The options is going to be empty,
119

120
00:10:47,800 --> 00:10:55,600
so I'm just going to write a square bracket, and inside, there will be a set of colon, and then the 
120

121
00:10:55,600 --> 00:10:57,690
completionHandler is, again, nil.
121

122
00:10:57,730 --> 00:11:04,570
We're basically trying to stay true to what this line of code used to do and what the original developers
122

123
00:11:04,630 --> 00:11:10,150
of Swifter wanted to do. But I'm guessing they didn't have the time to update this just yet.
123

124
00:11:10,150 --> 00:11:16,840
But now, if you hit command B, your warnings should disappear as well and you should have a clean slate
124

125
00:11:17,020 --> 00:11:18,850
in your errors and warnings'
125

126
00:11:18,850 --> 00:11:27,130
sidebar. Now, there's no need to understand what it is that we did over here because it's completely unrelated
126

127
00:11:27,190 --> 00:11:28,650
to our project.
127

128
00:11:28,780 --> 00:11:34,150
It's just fixing the third-party library in order to get rid of all of our warnings.
128

129
00:11:34,360 --> 00:11:39,550
And this is one of the downsides of working with third-party libraries because you're always dependent
129

130
00:11:39,970 --> 00:11:47,350
on the library owner to update their code so that you're not getting any errors or warnings.
130

131
00:11:47,350 --> 00:11:53,050
Now, if you are particularly chargeable and you're feeling like you want to volunteer and do some good
131

132
00:11:53,050 --> 00:12:00,490
in the world, then you can always try to fix this yourself, make a fork on the original GitHub repository
132

133
00:12:00,520 --> 00:12:05,910
for Swifter and contribute to this framework that you're using.
133

134
00:12:05,920 --> 00:12:12,820
Now, if you're somebody who's particularly curious and you want to know why we just did what we did, then
134

135
00:12:12,850 --> 00:12:21,970
you can go ahead to this URL and read about the openURL Deprecation in iOS 10 and this will tell
135

136
00:12:21,970 --> 00:12:27,190
you why it is that it got deprecated and what changed.
136

137
00:12:27,210 --> 00:12:27,560
All right.
137

138
00:12:27,570 --> 00:12:37,770
So at this point, we have incorporated our TweetSentimentClassifier mlmodel, and we have also incorporated
138

139
00:12:37,830 --> 00:12:46,450
the Swifter framework, now as you can see, if we don't have CocoaPods or if the third-party libraries
139

140
00:12:46,750 --> 00:12:52,170
don't put their project on the CocoaPods, then there's actually a number of stuff that we have to do
140

141
00:12:52,210 --> 00:12:58,540
and it's all very, very manual. But we've done all of that, we've incorporated the framework and embedded
141

142
00:12:58,540 --> 00:13:04,780
it into our project s--and we fixed all of the errors and warnings are associated with it, and we're
142

143
00:13:04,780 --> 00:13:12,880
ready to get started completing the next step which is using this Swifter framework to tap into the
143

144
00:13:12,880 --> 00:13:17,770
Twitter API and start searching and fetching some tweets.
144

145
00:13:17,770 --> 00:13:20,840
And that is what we're going to be doing in the next lesson.
145

146
00:13:20,860 --> 00:13:22,300
So I'll see you over there.
