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