Hi Alex here from rebeloper.com and welcome back to this epic in-app
purchases tutorial series where you can learn how to add in a purchases to your
iOS apps in Xcode 10 swift 4.2 and iOS 12 and in this video I am going to talk
about how you can add non-consumable to your app. So without further ado let's get into it!
Okay in the previous episode I have to show
you how you can add consumables to your iOS apps but I had a little bit of
leftover because I want to show you how you can add the number, well basically the
number of coins that you have purchased. And for that we are going to need
Rebeloper Store Pro. So how do you get Rebeloper Store Pro? It is pretty
easy go ahead and go to our site and go to our: store.rebeloper.com and
there if you just scroll down you will see that I have Rebeloper Store and go
ahead and you can purchase it there. Now once you do that then you will have
a new folder in your Rebeloper Store downloads and that is RebeloperStorePro.
Now that is what we are going to drag and drop into our project now.
Let me just go ahead and add it right over there. Okay and maybe just switch
places here. Now you can see that there is a Rebeloper Store keychain account file
RebeloperStore file, StoreSetup and Models. Okay we are going to go the
mostly on RebeloperStoreSetup but for now do you know
that you need to basically add some new Cocoapods. And that is let me just go
here and we are going to add them here. So let us just go back to our downloaded
resources and open up this Pot file again that you have. As you can see we
have Pots for Rebeloper StorePro. Let us just copy this we are going to
have KeychainSwift and BulletinBoard and you will see why we need these in
the next videos. Okay I'm just paste that in go back to terminal
and again type in pod installed or just I have the up arrow tab and you will get
that command line. As you can see it is installing BulletinBoard and
KeychainSwift and it has been installed. Now let's go back to our project.
Great, let me just open this up again and finally we want to show how many coins
there are in our app. Okay for that first of all make sure that in the
RebeloperStorePro go to RebeloperStoreSetup and resetKeychainToDefaultValues
is set to false. Now as I told you earlier, if you want to delete all of the
current data of your app then you should set this to true. Now it is set to false
that is great. Now we want to show the number of coins that we have acquired or
purchased or we have on upon for first launch. Let's go to our Views and
Headers and let's select our ConsumablesHeader and as you can see here we have a
populate down at the bottom of our file. And we are guard let unwrapping the
getVirtualCurrencyAmount function the return of that function. Basically
what this does a Rebeloper Store Pro function that gets all of our
virtual currency amount. We want to set the countLabel.text to that
amount. And while we are at it let us just go to back to Rebeloper Store
and in RebeloperStoreBasics let's enable some things in the extensions.
First off we want to enable RebeloperStoreKeychainAccount.setup because
once we start the RebeloperStore now we want to use the keychain and for that of
course we have installed the keychainSwift. Let's just scroll down a little
bit and we want to save real purchases into RebeloperStoreKeychain with
our Product ID. I believe yeah... I didn't go down there entirely
so this basically saves the purchase into the keychain. Okay and I just scroll
down if we do have anything left and I believe that's it. Okay now let's go
and launch this on our iPhone and let's see what happens. Okay let me just show
you here .Okay let's unlock it let's build and run again. Okay and there
you go you can see we have our 1000 coins. Of course in the previous
video we did buy but we did not save it. If you want to use
RebeloperStoreBasic then you have the responsibility to save the number of
coins that you have bought, well the user has bought into keychain or
NSUserDefaults or whatever you want. Now let's do that again. Let's do buy a
bag of coins again.
Let's wait for it to be processed there you go! Your purchase was successful
let's click on okay. And it hasn't been updated
it must've mean that we are missing some notifications. Let me just double check.
Okay we don't really have that shown up so I have made a little debugging here
as you can see we only have 1000 coins but we have purchased some. And
that is because we still need to have some more setup. Now first of all let's
set the default dataSource so from now on you can use RebeloperStore.fetchPurchases
and for consumable, non consumable and so on. We should have
this in our notification observer too. Okay now what you want to do now is
basically say how much coins you want to add when one purchases. Okay that can
be done in the RebeloperStoreSetup and if you just scroll down you can see
a section called PrimaryVirtualCurrencyPurchaseID. Now you want to add
your IDs here first of all. Let me just go there RebeloperStore...
Okay let's go and add that too and here we have our virtualCurrencyPurchases
and here we have our first purchase and our second purchase. Now the
interesting part here is our virtualCurrencyPurchaseAmount: 30 and 60.
I will leave that as it is. Okay, now let's try to Build and the Run again
and hopefully this time we will get lucky. That was the missing part of
our setup. Let us just see here, okay! Here we go! Let's purchase. Let's buy and it is
processing it and you are all set your purchase was successful and if you can
notice the 30 coins have been added. Okay let's test our Chest of coins too. Lets buy
and we should get that. Yep we do get it. Okay now that is it for in-app purchases
of consumable. Now one last thing and we will wrap it up for today's lesson and
that is the restore purchases. Now let's just simply let me just double
check if we do have that in there and let's see our ConsumablesController and
let's just scroll down because I remember that I do have it... Yes
RebeloperStore so let's just check if that works. Okay let us just select QuickTime
Player and I am tapping on and the restore purchases button and there you
go. What you want to do is go to settings on your app and then sign out
of your regular account and sign in with the sandbox test user that you have just
created in App Store Connect. And then you can restore the purchases. Okay that
is it for consumables and let's talk about non consumable. It is basically
the same as setting up the consumable. First up let us just go to our App
Store Connect account and let you select our App and let's create a
non consumable and let's just see let's just Build and Run our final project. Let's
stop this and Build and Run and I will create my
first in-app purchase for our consumable. Let's see here here we go it is a No Ads
non consumable. Let's just create that in App Store Connect and it is pretty
simple go ahead and select your app. Then let's go to Features and click on the
plus button here and it will be a non consumable. Let's click on create
Reference Name: No Ads. Okay and the Product ID. Okay now let's go back to Xcode and in
RebeloperStoreConfig if you can find it here we have our first non consumable
but oh... this is the store sorry for that. Let's go and select the
RebeloperStoreStarter and inside here you can see our first non consumable but we
need to edit it. Okay let's copy that out and let's go back to our RebeloperStoreSetup
and scroll all the way up and you can see here you can add your non
consumable IDs. That's that for the first one I will do the second one in my
spare time and you should do that. Let's just do that
here so consumable2. Okay and let's just go and add this here for our
non consumable and remember this is a non consumable. Make sure that under the
kind non consume is set up and that's it for our project.
Let's go back to Google Chrome and have this non consumable product ID there.
Great, let's select our pricing let me just look here it is under tier 1
Display Name: No Ads and description Remove all the ads from the app. Okay here
we go! Let's choose our file here of course make sure that you will replace
this later on. Okay let's click on Save and let's create the second one and that
is for getting access, granting access for the Go to Next Level for the next
level. Okay let's go ahead and click on the plus button another non consumable
it will be real fast. Okay Unlock next level. Okay
let's paste that in with consumable2. Pricing: it is tier 2. Display Name: Unlock Next Level
again and... maybe it's not with capital... Okay
and another in-app purchase template screen and let's Save that great! Now we
have set up our in-app purchases in App Store Connect let's just wait for that
for finishing and we are going to add some code in our Xcode. Without
wasting too much time let us just go into our NonConsumablesController and
just scroll through all of it. First of all let us just... well we will talk about
go to Next Level in just a moment. First of all let's commit our purchase as
usual so RebeloperStore.commitPurchase.
I am grabbing the purchase as usual as we did in our consumablesController.
We are going to create our dataSource as usual this time we are
fetching purchases for our non consumable. Let's just scroll down
again we are fetching our data the same. We are creating a section provider
here then we are adding it to our compose provider then we are creating
our compose provider as a header composed... oh sorry about that...
...let just uncomment that... We are creating our composeHeaderProvider and as you
can see we don't have a populate just yet but we will soon. And we are
commenting out all of these codes too. Okay and we are enabling restore purchases so
let's take a look at this populate. And that is inside our NonConsumablesHeader.
Okay and if you just scroll down you will see that we can comment out
this function. And I just realized that I have set up our in-app purchase for the
No Ads as consumable1 so let me just change these IDs. We are checking for
our first product for our non consumable and that is again in our RebeloperStoreSetup
the non consumable1 and that is set up as the No Ads in App Store
Connect. Let's just go back and we are grabbing our product. Now this product
needs for us to fetch our isProductPurchased inside our RebeloperStore.fetchPurchase
state for our product. Now if the product is
purchased then we set the banner image view to 0 and we set the banner at
status label to purchased. Banner ad is removed and you will see that in just a
moment. And if not then we set the bannerAdImageView
to one so we are showing an ad. Now what do we do when you want to
check for the second product well let's go back to the NonConsumablesController
and if you just scroll all the way up here we have goToNextLevel.
And basically we do the same thing let me just change this to product two
because I have mismanaged that. We are grabbing our product checking if the
product is purchased and if it is then we go to our next level.
Let's Build and Run and see how all of this works.
Okay let's just wait for it and hopefully we will be on time let's see
let's select No Ads. Let's buy and you are all set your purchase was successful
and as you can see the banner has disappeared and a label is appeared.
Now if you want to type in we want to go to the next level. Let's tap on that and it
says: You must purchase the "Unlocked Next Level" to access this content. Okay now you
can see that the in app purchase (No Ads IAP) has disappeared.
Now let's buy our Unlock Next Level. Okay let's buy and once we
buy we should be able to get access to the next level. Great now that is it
for non-consumable hope you enjoyed this video and if you did go ahead and give
it a thumbs up and subscribe to the channel and hit that notification bell.
And as usual I will see you in the next one!