Hey everybody what's going on this is Caleb with slopes dot com and in this video we're going to get
into the code.
We're going to start basically by requesting to purchase our subscription that we just made in iTunes
Connect.
So go ahead and pull open your X code project that you have been working in.
And first we're going to dive into the IAP service and I want to basically just show you everything
that's in there and explain what it's doing.
So select IAP service and basically what's going on and I already touched on this a bit but when the
app loads it's loading all of our IP products from iTunes and you can follow the other course if you
want to see how this is done.
But basically we have a function that we can use to attempt a purchase with a certain product index.
Right.
And when we set up this project we created some constants and these are my two other in-app purchases.
But we need to create a constant that's going to point to our in-app purchases from iTunes Connect.
And if you remember that that ID is Comcast IP course meal time monthly.
So I'm actually going to copy that and then create a new constant here.
So go ahead and type let IAP
mealtime monthly sub and set that to be equal to conduct IAP course meal time monthly.
Of course as a string like so.
And now this constant is what we're going to use as an identifier to load our meal time monthly subscription.
So go ahead and go into your IAP service and this is where we basically take all those product IDs and
we convert them into a set of type string.
And then we pass that into our function to request the products from iTunes Connect.
So we're going to actually go ahead and add that ID into this array of IDS just to begin with.
So go ahead and put a comma and type IAP mealtime monthly sub and now it's going to add that as a product
ID.
Okay that's pretty cool.
So now that we have that as a product ID it's going to go ahead and get passed through when we call
product request to start.
Basically what happens is all of these IDs are shot up to iTunes and if they come back as valid products
they get added to our products array here which up at the top you can see is an array of store kit product
Eskay product.
And basically once that array is full we can attempt to purchase for a particular item.
Okay.
Now we can use that products array and we can pull out the index of a certain item so maybe the first
index is hied adds.
The second is meal ID and the third might be meal time monthly sub.
So the way that we have been doing this is we've been using a custom enumeration called product and
I want to show you that product has two cases.
When you download this project one is zero I guess the zero index is hied adds the other one is meal.
We're going to add a third case here.
So go ahead and call or write case monthly sub and we're going to set that to be equal to 2.
OK.
We're simply following the order of the array as we put them in.
Now when we attempt to make a purchase of an item like hide ads we pass in the index of 0 because this
enumeration has an explicit type of int.
Now when we will let me show you when we do that k we go to our detail Visi And let's see what do we
do.
We tap by button ok.
We call IAP service instance and we call that function attempt purchased for item with product index
and we can call meal or hide ads right we pass in that enumeration value which has an explicit value
of 0 1 or 2.
Now in our IP service we pull out that raw value from the index and then use that to access the item
in the array we want K then we pass it into an S-K payment and pass that into the S-K payment queue
which is like a list of transactions that store kit needs to make.
So then after that the payment is made and this delegate method payment queue updated transactions is
called.
That is where we finish the transactions.
OK.
So there are five different things that can happen.
It is either purchased it's restored like I showed you with that blue button it fails.
Payment is deferred meaning payment was not able to be made or purchasing.
OK.
And that's what happens when you tap on the button but you have not yet purchased it loads into the
purchasing temporary time.
I guess there was like a temporary window of time where it's considered to be purchasing.
So let's say that we bought a meal at consumable purchase it comes in here we tap complete and we pass
it into our complete function.
OK.
And that's just directly beneath us.
Now we do different actions depending on what comes in.
If it's the meal we don't really need to do anything because we just finished the transaction because
it's something that you buy over and over again.
But for hide ads we call a function called Set non-consumer purchase and we set it to be true.
Basically what that does is it sets a value in our user defaults to either be true or false depending
on what we pass in.
And the key is non consumable purchase was made because remember if we buy the hide ads in that purchase
We're going to pass in true because we want to basically say hey a non consumable purchase was made
and we want to save that to user defaults so that that purchase is reflected even if we quit the app
and go back in.
So what we need to do is we need to set up a way to purchase our subscription and to do that.
We're going to use this same attempt to purchase function but we need to add a button to our interface.
So let's go ahead and let's head into Maine that story board and we're going to go ahead and select
this button which is a part of a stacked view.
And to duplicate it all we need to do is press command see Command V and now we have a duplicate button
but B where we've already set this button up with an outlet because this button is already connected.
And if we duplicate it the duplicate button is also connected so watch what happens if I right click
it's already connected to another view controller and we do not want that.
So get rid of that connection and we're going to change this to be a dollar sign.
OK press enter to lock it in place and then change the color to a nice green color and this green if
you want to use it is 6 D-B C6 3.
OK.
So now that that is set in place we're going to go ahead and open up a storefront VC and the assistant
editor.
So click on that button and pull this over.
Then we're going to go ahead and scroll all the way down to the bottom and we're going to go ahead and
find the place where this action can go.
We have one for the restore button.
Let's go ahead and let's put this right above the restore button.
So right click and drag from the button and release then go ahead and select action.
And go ahead and type subscribe button was pressed and press connect.
And now we have a function that we can basically do things when we push this button.
Now let's collapse the assistant editor.
Let's go into storefront Visi and find that IAB action again.
OK here it is.
So what we're going to do is we're going to call our attempt to purchase function.
Now go ahead and call that by typing IAP service for instance because remember it's a singleton class.
If you go into here you can see it's a singleton.
We have a static instance that's available to us throughout the whole lifecycle of the application.
Go back and type IAP service for instance dot attempt purchased for item.
Pretty cool.
Now remember this is for subscription so we can go ahead and type dot and use our enumeration to access
the proper index we need which in our cases monthly sub press enter and check it out guys.
That's all we need to do to attempt to purchase.
We should go try this out we should go see how we did.
So go ahead and build and run.
I'm going to pull open my simulator here so you can see what's going on and basically I'm just using
the movie recording feature on quick time which you can actually set your iPhone as a camera and it
lets you view your screen right on your computer over USP.
It's pretty cool.
So let's go ahead.
We're going to build and run that one more time.
Sometimes quick time likes to hijack my you know my session but looks like we're good.
Very cool.
And if you take a look IAP products are loaded.
So that means we've successfully loaded all of them.
There were no errors.
And watch what happens when I try to tap on my subscribe button.
Check this out.
If I push it it's going to ask for a password.
And this is where you're going to have to set up what is called a sandbox tester.
OK.
So what I'm actually going to do is I'm actually going to push cancel on my phone I'm going to go into
my settings and I'm going to go ahead and go to the app store settings and I'm going to show you that
in just a moment.
OK.
So select app store and you'll see I already have an idea logged in.
You'll have your Apple I.D. logged in in the App Store settings.
You're going to go ahead and push sign out.
And don't worry it's only going to sign you out of the App Store.
Not all of your message not your iCloud just just the App Store.
Leave this as it is and then go back to the app.
K now when you select the subscribe button watch what happens.
It's going to ask you to sign into an Apple ID.
This is where iTunes Connect comes into play.
So go ahead and pull open iTunes Connect again.
And what you're going to do is you're going to click on my apps up at the top.
You're going to click users and rolls and then you're going to click on a button that says send box
testers.
OK.
And this is where you're going to basically create a fake Apple ID account that we can use to test our
subscriptions.
So to do that click the plus and we're going to give it a name a last name an email password.
All that stuff just like you would with a real Apple ID.
So I'm just going to say test account and the e-mail.
This is where we're going to use a cool thing called Nahda.
OK.
And in order to do that go to get not a dot com.
And basically it's a temporary disposable e-mail account that you can use when you need it and delete
it when you don't need it anymore.
Which is perfect for test accounts.
So go ahead and click on get me an inbox and check out what happens here.
I already have one but I'm going to create a new one click add more and you can choose how you want
your e-mail to end.
I'm good with a mail doc club.
And we'll just say meal plan.
Aim at our meal plan at a male duck club.
Click Add.
And check it out.
No messages yet.
Your new messages will appear here.
This is my e-mail meal plan at a male duck club.
Click copy and then we can go back to iTunes Connect and paste in that e-mail.
Now you're going to need to remember that and you're also going to need to choose a password for you
to remember.
So go ahead and type a password.
I'm using test test one two three exclamation mark.
Test test one two three exclamation mark.
And my question is just going to be lame What is a test test test.
One two three exclamation mark.
Now choose a date of birth it doesn't really matter and choose your country.
I'm just going to choose United States now when this is done.
Click save when you have all the information filled out.
Remember your password.
Remember the e-mail and click save as soon as that's done it'll show up in your test account.
OK right.
Their meal plan at a male duck club.
And if you go back to nada you'll see you have an e-mail that says verify your Apple ID.
This is very very important if you do not verify your Apple ID.
You are not able to test subscriptions.
And that's why you need to verify.
That's why we're using disposable e-mail so you don't have to create a bunch of Gmail accounts because
that's not not a really cool thing to do.
So we're just using a disposable e-mail because we're not going to need it forever.
So click on that e-mail click verify now just like so and then enter the password you created earlier.
For me it was test test one two three exclamation mark.
Then click continue.
And as soon as that's done it'll say Hey you're good to go green check is good.
E-mail address varified.
So let's go ahead and we're going to head back into our app here.
We're going to click use existing ID and I'm going to go ahead and enter my e-mail which for myself
was meal plan at a male club my password Whoops I spelled that wrong.
A male user like type it right.
Man all right my password was.
Test test one two three exclamation mark and then click.
By now it's going to ask you for it again.
It's going to say hey enter the password.
Use existing Apple ID and sadly you have to do it one more time.
Let's do it again.
Meal Plan this is basically just setting up the account and verifying everything.
A male club.
All right.
And do not try to sign in on in the appstore settings because it actually will not work.
It will register it as an official Apple ID and for this we're actually registering it as a test sandbox
test account.
So test test one two three press.
OK.
And it's not going to do anything.
That's because we have just successfully logged in.
But watch what happens when I push the dollar sign button again boom it asks for the password for that
particular account so now we know we're logged in.
Test test one two three exclamation mark.
And watch what happens when I push.
Ok
look at that subscribe to meal plan get rid of that not now thing subscribe to meal plan this subscription
will automatically renew every month forty 4:49 ninety nine.
That's exactly right.
That's what we just wanted to set up so with if we push that button we're subscribed but it's not going
to be very good for our users because at the moment that doesn't mean a whole lot.
It doesn't do anything so click Cancel.
And this is amazing guys we have our subscription almost completely set up.
We just have to do a couple of things to basically determine how a subscription is going to modify or
influence the way that our app works.
And we're going to do that in a future video so let's go ahead and let's head over to the next video
where we're going to talk about how how do we handle failure.
How do we handle payment deferral.
How do we handle making the official purchase and setting things in user defaults so that they're locked
let's do it.
Let's head over to the next video and let's talk about that stuff right now.