Hey what's up everyone Mark Price here at the slopes dot com.
Let's go ahead and just get started making in-app purchases so what I want to do is a little bit of
code cleanup as usual I want to create a new group here.
Just call this controller or control or files in there like view control or at Swift and then I'm going
to open the view control or file click the name and rename it to something more useful.
Let's for instance call this a home VC and press enter.
So and now I want to create a services folder because what I want to do is create in that purchase service
that will manage all the in-app purchases for us so services and we're going to say new file swift and
let's go ahead and call this the purchase manager purchase manager.
Yeah that sounds good.
And then class purchase manager like so and we're going to say static let VAR or not are static let
instance what I meant to say equals purchase manager.
So what we're doing here is we're creating a singleton we're creating one instance of this class in
memory that will be used for the lifetime of the application.
So we can always access it easily without having to create new copies of it.
And one more thing we need to do here is import store kit so we can actually use the at Purchase store
get stuff with me so far.
OK.
So the next thing we need to do is forward in with in-app purchases.
Our class is going to have to need to work with this object.
So I want to put an object here as the parent class.
And what we're going to do is we're going implement some protocols we're going to implement S-K products
request delegate like so and S-K payment transaction observer.
OK.
Those are two very common protocols that work with when working with in-app purchases.
OK.
And what do we want to do.
Well we want to work with in-app purchases but we haven't created any yet.
And also by the way we can click this red button here and they'll implement the functions for us here
which is great.
Move these down so they're where they should be.
So we need to create an app purchase so let's go back to our browser and let's go back to our app.
So iTunes Connect my apps
and what we're going to do is go to Daily Dose depth slopes features in-app purchases and we're going
to create the purchase that's going to be used in our app are just going to be a non consumable OK and
non consumable means that you can only purchase it once which is what we want.
Consumable can be purchased many times like coins for a game things like that in our deficit.
We've had consumable coin purchases.
You can use coins to buy courses.
There's many different ways of making purchases.
You can do auto renewable subscriptions or non renewing subscriptions where you just pay it once up
in advance.
And those are few of the ways you can monetize in your apps.
We're going to create a non consumable click Create reference name is just a reference for you and we're
going to call this remove ads and what's the product.
This is important this is the idea you're going to reference in your code and you should use the same
type of bundle that your app is using.
So calm the slopes got a daily dose not remove ads.
That's what I'm going to call it the price.
Let's make it 399 and content hosting content hosting is when Apple will actually host your purchase
you can upload a package file with information about your purchases.
And so they can host it.
And the reason why you might do this is let me give you a perfect example let's think of the app the
soaps app where you can purchase many courses.
We don't want to have 500 in-app purchases one for every course.
That would be too hard to manage and we'd have to keep making updates.
So instead what we do is we would make let's say five in-app purchases we just with different prices
$25 $29 course purchase purchased course purchase of course purchased just with different price values.
Okay.
And when you buy those you buy them great but what about when you like restore your in-app purchases.
How does it know which courses to give back unless you have your own server managing this.
It's impossible.
So what you can do is you can upload metadata about purchases and it's up to Apple and the host for
you.
And then they'll deliver it to you when you need it.
So you can manage things like that.
We're not going to be doing that pretty complex actually.
And then we have localizations of the different languages that you want to display your purchase name
to Apple of course is the more languages that you translate for or localized in the more cells you will
get.
We are only doing English today because that is the only language that I know.
So the display.
This is what the user is going to see when they make the purchase.
Remove ads and the description permanently remove ads is what we're going to make the description say.
Lastly we do need to have a screenshot here so they can look at the app and so they can look at the
app and say hey where's the purchase.
So what we can do is click our simulator here command s to save a screenshot and let's just drag that
screenshot from the desktop over here into the Choose file slot and you want to give the app review
some notes you have to click the green button to remove ads.
Perfect that has loaded.
And when you're all done here go ahead and we're going to copy this idea by the way and we're going
to click safe.
OK your IT purchase is ready to submit.
If if it said something else like missing made it did it you probably missed something here.
But if it's ready to submit what you can do is go back to the App Store now click your first version
here 1.0 or whatever version you want to submit this with.
Scroll down and in-app purchases.
You want to add all of your in-app purchases here and if you created it correctly you'll see here it
did not create correctly it means or if you don't see here it means it was not created correctly and
there's something missing you need to go back and fix it.
Ours is here which is great so I'm going to click done and then I'm going to click save everything that
we've just done.
You have to do even to test in-app purchases.
OK.
If you do not do all of these steps you can even test it and you're going to run into problems so what
did we do.
OK.
Remember we went into the apps or we clicked features we created a new that purchased them we went back
to the apps or we clicked prepare for submission and we added the in-app purchases to this version so
it can be reviewed by the app by the App Store Team.
OK.
So cool.
We're now ready to work with in-app purchases.
So back in the code okay we can start writing code to work with them.
So first things first is I want to actually store the ID of the in-app purchases here so let IAP move
as equals.
You know if you had more than one minute purchase you would store the ID here or if you are building
a more elegant solution you can actually store all these IDs on your server and you can fetch them from
your server and then you can add more in-app purchases dynamically which is really cool without having
to wait for app.
The app store review OK but ours is going to be hard coded because we only have one game.
Are you with me so far.
So here's are in that purchase and I think we're ready to get started on this.
So let's create a few variables.
We're going to create one called Product request products requests type Eskay products request.
This is going to be requests that not with an S zoomie a request that we send up to the app store K
to get products that are actually going to store an array of products like cell escape product
because after all we're seeing you back all of your product IDs and it comes in the form of an escape
product.
Right.
And the reason why we store these IDs here is so we can match them up.
OK so we can match what we get from the App Store to what we have here in our code.
All right.
So those are products requesters our products.
Great.
The very first thing that we want to do is actually fetch the products we cannot make and end up purchase
unless you have the products from the App Store.
Let me say that again you can't make a purchase.
No user can make a purchase unless the app has spoken to the App Store and actually downloaded your
product IDs for your specific app so we have to do that.
So phunk fetch products.
All right we're going to create a set here so product IDs so which product IDs we want to fetch equals
an asset.
We're going to do the one that says object.
We're going to go ahead and pass in that purchase remove ads.
We're going to cast this as the regular set just the way you got to do it.
OK.
So we have a set of product that is that's how Apple wants them.
Is it an asset or a set of some kind.
And then what we're going to do is we're going to save our request so a product request
products request and we're going to say equals Eskay products request and it wants the product identifiers
and wants that set.
So let's go ahead and create the set so product IDs and then products request delegate equals self care
and products requests dot start.
All right so what's going to happen is you're saying hey let's go fetch some products.
Here's the list of products that I have.
Apple can you go get them for me maybe you don't know all of them maybe have some old one deprecated
ones.
You're letting apple know which products that you want.
OK.
And you make the requests you set the delegate and you click start when you do the delegate.
OK.
It's going to call this function here products request function when it's done.
OK so this function here we don't need to do anything else and when it when the request is done after
we started it we'll call this function.
All right which is cool.
So let's go ahead and make sure that when the app first loads that we're starting the fetch on our products
because we want it to be ready by the time the user actually clicks the button.
So we'll go to the app delegate and what we'll do is here we will say purchase manager for instance
that fetch products the moment the app loads.
All right let's just get all this set up and ready to go.
So now that we've started the request how do we actually store the data when it comes down et cetera.
What we'll do is in here and this function did receive response.
Okay.
When you say response products that count was greater than zero meaning did it find products if there's
more than one great let's go ahead and start working with them and then we're just going to store them.
Products equals response products.
We're not doing a whole lot here with this.
And so I do want to show you something which is really interesting.
OK so response that products count.
OK.
So what is a product that it's giving us here.
It's a type Eskay product.
Well if you look at S.K. product OK there's a there's a title a price a price location product identifier
etc. etc. etc. and you're like Mark why is this important.
Well think about this.
You say you have literally used the dove slopes example let's say you're selling 10 courses and they'll
have different prices and different product IDs.
OK.
Where were you getting the pricing information from.
Well the pricing information has to match what's on Apple's server.
And so Apple needs to send you that information right so you can actually display it in your purchased
icons.
Oh OK so you don't manually write the prices yourself in the code.
No.
You get those prices from the App Store so you can display them to the user.
In our case we're not doing that because we're just clicking a button here that says remove ads but
you do have that information available to you here inside your products.
OK.
Every one of these products has a price and title etc. all coming from the apps are all what you created.
All right.
And all we're doing here is actually just just storing the one in that purchase that we have and where
we can make a purchase later on.
No big deal.
OK.
So that looks good to see if it's actually working.
What we can do is in here we can print response products debug description came something very important.
You need to know right now you cannot test in-app purchases on the simulator.
Again you cannot test in-app purchases on the simulator.
Even the request to fetch products won't work.
It will not work in the simulator store.
It does not work in the simulator so if you don't have a device you can't test in-app purchases and
I would never ever recommend submitting an app to the apps or unless you have a device and you've actually
tested it.
So make sure your device is selected and what we're going to do is we're going to look for this print
statement saying which products it was able to pull up from the App Store someone or run it now.
So we can see and again the app delegate is being called to start the fetch products and that should
start this which goes here to the products requests and it will give us a response.
The app is now active and working perfect.
Let's stop it let's look at our prints here.
I saw something that some Google print outs here
and there it is it did return one product for us.
So it's definitely working.
Our one product has been fetched from the iTunes store and we've printed it here.
And so clearly we're receiving an object so let's go ahead and call this video done what we've done
here is we've implemented a connection to the iTunes App Store.
We've successfully fetched one of our advertisements or excuse me one of our in-app purchases by via
via of this idea here and we started the request to fetch the products and we received them right here
and stored them in a variable so we can reference them at a later point in time and make purchase.
So that's it for now.
Marc price Adela's slopes dot com.
Let's move on.
And for.