Hey everybody what's going on this is Caleb with slopes dot com and get excited because we're going
to dive into the world of in-app purchases in this video by creating our IAP service singleton class.
So pull open your X code project and let's go ahead and let's right click on food Zilah.
Let's create a new group called services and we're going to then right click and create a new file.
This is where we're going to create our IP service.
So go ahead and click on swift file.
Go ahead and write.
Service like so and create it.
Now a singleton class is a class that is basically like you instantiated on itself and that allows it
to be used throughout the entire life cycle of the application it's really neat.
It's a really cool way to access any in class and like settle the values in you know access all the
functions and do all kinds of cool stuff from any other class in your app.
But the singleton pattern can be abused.
So we're going to do our best not to do that but go ahead and create a class called IAP service and
go ahead and create a constant called Let instance equals IAP service and we're going to instantiate
it from the get go.
Now in order for a singleton class to work you have to actually declare the instance as static and all
that static means is that the instance of IAP service is going to be accessible throughout the entire
duration of the app lifecycle.
So it's not going to be deallocated or initialized or anything because it is static it means it stays
there it stays in place.
So that's why we're going to use static to create our singleton.
Now when we want to access this we could say you know let blaw equal IAP service instance.
And now inside of instance we can access all the properties that we could for any normal class and you'll
see how that works later on.
But for now before we move on we actually need to go ahead and import a framework here.
So go ahead an import store kit like so and that's going to give us access to all the Inet purchased
goodness.
All right.
And then what we need to do actually is we need to conform to S-K product request delegate.
So this is going to be conforming to S-K product request delegate now S-K stands for store kit and the
S-K product request delegate allows us to request products but it's saying that this does not conform
to the protocol and as object protocol.
And that is because when you're using store kit you also need to conform to can't type and hence object.
So go ahead and type that.
And when you build and run you'll notice that it now only tells us that we don't conform to the S-K
product request delegate and that's because we have not yet basically used our S-K product request delegate
methods that are required.
And the first one is product request.
OK.
Sorry you should read that description here accepts the response from the App Store that contains the
requested product information.
OK.
And go ahead and press enter.
Now.
Product request did receive response.
Now that is the function that we need in order to actually conform to S-K product requests delegate.
But like it said it's going to basically accept the response from the App Store but we need to first
set up you know how are we actually going to do that.
And so let's begin by writing a function here called load products and load products is going to do
two things.
It's basically going to take all of our product IDs and convert them into a set of type string.
Then we're going to go ahead and call request product which we have not written yet.
But what that's going to do is it's going to make an S-K product request and passed that set of strings.
The set of IDS up to iTunes to check hey is it legit.
Is it really connected with this project and it will return to us some information.
Then after we request all those products what we're going to do is we're going to set those products
to live in an array in our IAP service and we're going to pass that array in to our products requester
it will make more sense as we go about it.
So let's go ahead and let's do that now in order to begin we're going to be calling a function here
called Product ID to string set.
Okay that's one function that's going to convert all of our IDs to a string set and you're probably
saying wait what ids are you talking about.
Do you remember when we set up iTunes Connect.
We created a product ID.
OK AP course meal.
Now is the time we're going to use that.
So copy it from iTunes Connect or just remember it.
And we need to set up a constant file and this constant file is basically going to give us access similar
to our food items on a wide scope.
So go ahead and right click Create a new group and call it.
Oh you know what.
This could go on our helpers.
So let's go ahead and get rid of that group.
Bye bye.
And in helper's right click new file swift file and call it Constance OK create it.
And now we're going to go ahead and just call this.
Let IAP underscore meal ID.
OK.
And let's set that to Comcast AP course meal.
Now we can use that.
We don't have to type this as a string ever again which is really nice.
We can't really you know mistyped it.
So back to our IP service.
We are going to use the product ID and convert it into a set of string because that's what that's what
iTunes actually requires us to use is a set of string.
So let's think after we convert all of the IDs into a set of strings we need to go ahead and call request
products which is another function we're going to write but we're going to pass in those product IDs
that we generate.
OK.
Or I guess the set of string from the IDs and request products is going to make that Eskay product request
pass it in send it up to iTunes they'll send back saying yep you're good or no you're not good.
And that's where products requested receive response is going to get called.
So let's go ahead and let's build out these two functions first and then we're going to go ahead and
you know uncommented them and make sure they work.
So go ahead and let's call phunk product ID to string set like so without the capital-T there and now
what we're going to do is we're going to take our identifier.
Right.
IAP M.D. and we're going to need to pass that in to a set of type string.
So let's go ahead and let's create a variable at the top called product IDs and we'll make it of type
set and string.
So var product IDs it's going to be equal to a set K which is a collection type and it's going to be
of type string.
Now instantiate it from the beginning and all we need to do now is we're going to go ahead and call
product IDs insert and look at that we can insert a new member just like that.
So go ahead and call insert and we're going to go ahead and insert IAP Meehl ID.
And now we have a set of type string with our melody good to go up to iTunes.
OK that's easy.
We just converted it into a string set.
Next we're going to go ahead and write our function here called request products.
So go ahead and type phunk request products.
And we're going to need to pass in a set of type string.
So let's go ahead and let's pass in let's see requests products for IDs and we'll just use an internal
parameter there of IDs that's going to be of type set with the internal set or internal type of string.
So there's that function.
Now what are we going to do.
We need an instance of S-K product request so let's go ahead and create that var product request.
It's going to be equal to S.K. product request and go ahead and instantiate it.
So there we go we've got a product request.
Now we need to call some particular functions on it to set it up.
So go ahead and say product request is equal to S-K product request and we're going to instantiate it
fully now.
Now look at that.
It asks for a set of string of product identifiers.
Press enter and pass it on IDS.
Beautiful.
After that we can go ahead and set the product request delegate to be equal to self thanks to us already
conforming to it.
So go ahead and call products request delegate equals self and then beneath that we need to call a function
that's going to start the process like we've loaded up the data.
Now we need to actually start sending it up to iTunes and waiting for a response so go ahead and call
product request that start and it sends the request to the Apple store.
But what if we started this and then got stuck halfway through and tried to request it again that would
not be good.
So in order to safeguard ourselves we were going to call product requests cancel at the beginning so
that if there is a product request we will cancel it first and then start a new ones.
We don't have an issue of multiple requests going on at the same time.
So now that we have this good to go we're going to go ahead and uncomment.
Well uncomment these and just like that is going to let us pass him in although we're missing a parameter
here of IDS or I guess it's for IDs right for IDS build and run make sure that it's good to go.
Or I guess just build K cool.
So what we're doing here load products converts our product ID into a set of type String request products
is basically passing in that set.
Uploading it to iTunes and then as soon as we get a response from the server this function product request
gets called.
So what we need to do is we need to set up an array of type S-K product because inside of an S-K product's
response we get all of those products back from iTunes.
So let's go ahead let's set that up here.
Var products.
And that's going to be equal to an array of S-K product.
Now let's instantiate it from the beginning.
So it at least has an empty value.
And what we're going to do is we're going to set self up products to be equal to response because remember
it's S-K product response and watch this response dot products.
We get an array of S-K product.
Super awesome.
Now let's say that we got zero products what if zero products came back.
So if so if products count is equal to zero meaning of zero products come back we're going to go ahead
and call request request products for IDs and we're going to send in the product IDs.
So this is sort of like a retry.
Right if it comes back empty like if the server doesn't work it's going to retry until we get all of
our items that we need.
OK but if it worked if we get all of our products back we're going to call.
Else.
And this is where we need to basically inform our app that all of our in-app purchases were downloaded.
And so this is where we're going to use a protocol that's going to be a really easy way to help us basically
pass data between two controllers using delegation.
So in order to do this go up to the top of IAP service and type protocol IAP service delegate.
And inside of this we're going to set up a function.
We're not going to give any functionality because that happens wherever this delegate is conformed to.
But for now just go ahead and call phunk IAP products loaded and give it the parentheses needed to declare
it as a function.
And now we have a protocol that's going to allow us to pass information to another view controller when
all the products are loaded.
So go ahead.
And what we're going to do is create a property to hold an instance of delegate and go ahead and do
that by typing var delegate equals are not equal sorry of type IAP service delegate.
And set it to be optional.
Because from the beginning if we were not going to instantiate it we don't actually have it unless it's
being conform to somewhere else.
But assuming that it's being conformed to we can call delegate dot and check it out.
We can say products loaded.
So if we have conformed to this delegate and another view controller we can call this function and then
on that controller that function will load.
That's the cool thing about delegation is that another class can control another class.
Pretty cool.
So let's go ahead and in storefront Visi Let's actually conform to that delegate and then set up a function
to basically tell us hey everything's loaded.
You're good to go.
So to do that let's go ahead and let's create actually an extension beneath our class and let's type
extension
storefront Visi and we're going to go ahead and conform to IP service.
We don't have access to it yet.
Build the project service delegate.
Boom.
Now if I build it it's going to say hey you don't conform to a service delegate.
That's because we have not called IAP products loaded.
OK.
So basically in our IAP service if everything loads the way that it's supposed to we can call delegate
IAP products loaded and it's going to call that function here.
So we're going to just print saying IAP products products loaded.
And that's only going to get called if it actually works which is cool.
So now how do we set.
How do we set this view controller to actually be the the delegate.
How do we set it up so that it knows that this view controller particularly is the delegate super easy.
All we need to do is access our IP service class and set the delegate to be this view controller because
we've already conformed to it.
So go ahead and in your view to load call IAP service instance and now check it out.
We have access to all those functions and all those properties but we just care about the delegate.
So IP service that instance delegate is equal to self or this controller.
Then all we need to do in order to actually load up our products is call IAP service instance load products.
It's that easy when we do that.
It's going to go ahead and convert it request the products and when it's requested it's going to go
ahead and check if 0 came back at all request again if they all came back.
It will call IP products loaded meaning we have all of our products and it worked.
So I don't know about you but I really want to go test this to see if it worked.
Go ahead and click build and run.
And we're going to pull up our simulator here and we're going to see if our labor was worth it if what
we did worked we should get a nice print out in the console if all of our IP products came back the
way they were supposed to so it's going to go ahead and build and run.
Do do do.
And look at that it printed IP products loaded but you know what I want to prove it.
So go ahead and click on IP service and what we're going to do is we're actually going to let's let's
print out let's print Let's go into the products array and let's just print out the first item and we
should be able to access the localized title which I think should be meal.
Let's build and run it again and let's see if it actually does download those pieces of data from iTunes
the way that we expect
boom.
Can you see that it says meal right there.
That's from iTunes.
That's the one that we set.
We have not type that anywhere in this application so we are officially downloading that information
from Apple from IP services.
And this is working guys this is amazing.
So now believe it or not we have access to all of our in-app purchases.
We're going to do some more things to continue building this out and making it even more amazing.
But guys seriously we are so much closer to making real in-app purchases in our in our app.
We can download all of our purchased products and now we can start setting them up to be able to be
purchased.
So let's head over to the next video where we're going to continue building this out and diving into
the world of consumables.