How to implement In-App Purchases using StoreKit in Swift?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello all welcome to arendelle's with me rohan and in today's tutorial we will see how to include in-app purchases in your swift project let's get started [Music] so for this tutorial i have created a project in swift using storyboards and since you know that this tutorial is all about in-app purchases i have set up my catalog in the storyboard itself the first thing that you've got to do is vividly show what you're trying to offer to the customers this is one of the guidelines that apple has provided for the developers to show their catalog very clearly to the users so that is the reason why without wasting much time i have already prepared the catalog for my app in the storyboard itself so here is the catalog so what the users can do is they can buy 500 gems for one dollar and if they want to get rid of the ads it is two dollars and if they want to have a recurring monthly subscription that is for four dollars this is just an example guys so if you want to implement it something differently you can go ahead by the way the in-app purchases there are four types of in-app purchases one is the consumable which says purchase often but can get depleted over time okay the second one is a non-consumable which is a premium content it is purchased once and it never expires the third one is auto renew in which the customers enjoys uh premium content just like the netflix or the amazon prime subscriptions that you have every month or every year that renews automatically and the fourth one is non-auto renew which means uh you are exposing premium content to the users for example if you're trying to show the content just for tokyo 2020 olympics which was just for a limited period of time you can have the non-auto renew and expose the premium content to the customers so this is what uh the four types of in-app purchases are so what i have done right now is i have shown my catalog but this isn't it okay so you have to kind of hook up your catalog to the store kit configuration file which will be adding it in our solution and then from there we will start so as you see now there are three items that the customers or the user can purchase in your app we will have to have this defined in a store kit configuration file okay so you will have to add a new file to your project which is we will be adding it right now and over here just say configuration file for store kit this is the one that you got to add to your project okay and just click next and just leave the name as it is and just click create [Music] great so now it says click the plus button to add an in-app purchase or subscription so once you open up the configuration.storekit file you will be seeing this line over here which says click the plus button to add in the in-app purchase or subscription now where is the plus button it's right here in the below okay just click that and you need to decide the type of in-app purchases over here the first thing that we're going to do is we are going to allow the customer to purchase gems okay so gems for that matter is a consumable and so that is the reason why i'm going to add a consumable over here okay so just click this and a reference name yes i'll just give it gems for now okay this is just identifying your uh your in-app purchases okay so over here the product id very important okay so this is how you're going to identify the product in your code okay so the good practice over here is to uh stick with your butter identifier okay so i'll just copy and the first part of my button bundle identifier and i'm just going to paste it over here and just say gems okay and i believe the amount for purchase of 500 gems was one dollar if i'm not mistaken and i'll just keep it as it is so this is the first set of the configuration done for the gems now second the customer clearly needs to know for what he's purchasing these 500 gems okay so let's specify the description for that purchase and the english localization is already set so just double click that and specify gems okay and say we can specify the description for it saying purchase 500 gems okay for one dollar okay and just click done so in case if your app is used in multiple locations using multiple languages you can specify different localizations over here in these many languages okay so i'll just leave it right there for you to decide for your own app now secondly what i'm going to add is a in-app purchase for a premium content with no ads okay so i'll just click this plus sign over here and it says uh the second one is no ads so in in a sense it is a non-consumable in app purchase isn't it so and just click this and i will give the name as ads premium content okay and again uh the bundle identifier will be here and i will just specify it as premium okay and i will have to recollect the amount so let me just go back to the store report and see what was the about it was two dollars okay so let's go to the configuration.storkitfire and just specify the amount as two dollars over here okay and again just specify the description for it say premium content sorry and say no ads with premium content for two dollars okay and just click done and that's it the second one or rather the third one that we are going to add over here as an in-app purchase is a monthly subscription okay so once this is done the third purchase in our tutorial is a monthly uh renewal or the monthly subscription that is done similar to what you have for netflix or amazon prime uh for the other ott platforms okay so you just click the plus sign over here and specify a auto renewable subscription this is the last item over here from the bunch that you have and just click the add auto renewable subscription and here you will have to specify a group for that matter what i always recommend you is specify a group based on the timeline okay so if your subscription is monthly yearly uh daily weekly for that matter okay just specify this over here and i have added as monthly over here so i just click done so it comes into the left panel over here okay so the first two that we have added as a month as an in-app purchase are over here in the first section and right below that you have the auto renewable subscriptions and which is monthly okay so i have the reference name as monthly over here and the duration is one month okay so again let's add the product id and i will just say monthly and just reference name it as monthly subscription subs monthly subscription okay and over here i guess it is four dollars that we have specified and the subscription duration is one month yes and if you want to enable family sharing it's up to you and just leave it unchecked and again the display uh description let us write it as enjoy [Music] all premium content for monthly subscription oops it's not allowing me to type further so let's say four dollars over here monthly premium monthly premium content for four dollars okay fine it's not allowing me to type further so i'll just leave it right there and i'll just write it as monthly subscription just click done and that's it so you have set up your store kit file now uh just to bear in mind what i'm doing right now okay it is only for test purposes okay so if you want to publish this app to the app store the app store also has a similar configuration that needs to be done in your app store connect website okay so where you are uploading the app in the app store connect you will have to set up the entire configuration setup in the app store connect file so the reason i'm doing it over here is like apple has enabled this toolkit to be used during the testing and development phase that the developers can set up their own configuration while they're developing rather than you know setting up in the app store connect okay so this is a really good thing that apple has done so that is the reason why we are doing it in this tutorial okay so bear in mind this will never work in the production environment okay this is just for the test purposes for you guys so great so now uh what we are going to do is we have configured this toolkit file over here and the second thing that you got to do right now is go back to our viewcontroller.chip class okay and you will have to do a couple of imports over here okay so the first thing that you're going to do is import storekit okay and after you have imported the store kit you will have to add in a delegate for sk product request delegate okay so just write sksk stands for store kit so let's write sk product request delegate which is right here okay and after which you have to also add sk payment transaction observer this is really important this is an observer for it's kind of viewing your transactions whether in what state they are in whether they have completed or not okay so this is really important and it is a best practice for all the developers to implement uh the observer for their in-app purchases okay so let's write sk payment transaction observer okay now a good part over here is we have to specify oh yes we need to add in the stubs so let's add in the the stubs for all this fine so it will not always throw up the errors okay now what we are going to do is we have to specify uh now what we have done is we have specified the uh the product in the configuration file okay so when you're trying to code you will have to add in the same products within your view controller okay so that that is the reason why from the view controller itself it will be going to the sk product request delegate okay okay so let's write in our three products that we have in an enum okay so let's write inno and let's write product okay just specify it as string and case iterable okay and within this let us write in three cases okay the first one is the gems okay so let's write gems so recollect okay we have added uh three products to our uh to our store kit file and which is com.rndlabsme.gems okay i can vividly remember this and uh the next one is a non-uh non-ads premium isn't it okay and i don't remember the uh the name that we have set in for the premium content okay so it's just the premium that we have added so let's copy this and go back to our view controller lots of class and add in the premium and the third one is case monthly subscription isn't it monthly subscription i will keep it as a smaller case s and we will copy this from the configuration.storekit file we have just added monthly that's it all right so this part is ready you have specified your products okay in the viewcontroller.swift class and okay now if you go back to our main.storyboard we will have to hook up these products to our viewcontroller isn't it so let's add in our assistant and create an outlet for all these buttons okay so just make some room here okay and this is by gems okay what name can we give them just say buy gems isn't it it's very simple so let's click buy gems and no ads for that matter okay so let's say by premium okay premium [Music] connect third one is bi-monthly subscription okay so let's rate buy monthly subscription okay so you have all these three buttons hooked up to our viewcontroller.swift class and over here now we have to specify or rather we have to allow the customer to buy gems isn't it so for that we will have to go and write few lines of code so let's go ahead and write them let's say if sk payment queue okay which is the escape event queue type dot can make payment okay this is very important why i will tell you this is if the user is a miner and he is not supposed to make payments okay if he is not authorized by his parents then or anybody who is managing the ios device that he is trying to use and if he's allowed to make payments then only make payments otherwise uh you can show in some message saying that hey you are not allowed to make a payment okay so this is really important and it's a best practice for the developers to check who their user is and if they're allowed to make payments or not okay so let's say let set so if the payments if the the person who's trying to use the app can make payments then only go ahead and allow them to purchase this okay so let's say let set and it's a set over here and which is of type string okay and is equal to we have to have or rather get the value of this okay so this the value that we are going to pass to the product request is of type set okay so we will have to specify or rather get the value which is the raw value of jem's case okay so let's write in products product dot gems dot raw value is that simple okay and next up what you're going to do is we have to request the product purchase okay so let's say let product request is equal to sk product product request okay and over here which will take in the product identifier which is our product identifier we have a product identifier in the set variable okay so let's pass in the set and that's it okay and just specify the product request product request dot delegate is equal to self okay and we have already specified the delegate for that which is right here and we are just going to set it to our product request variable and after this what we are going to do we just have to specify product request product oops product request why is it going up dot start and that's it so your first few lines of the code for the in-app purchases for purchasing the gems is done okay so similarly what we are going to do is we are quickly going to add in uh those lines again to our premium and the monthly subscription buttons as well so let's go ahead and copy them just to save time and over here we just have to add in the non ads premium case okay and over here just copy this go down oops did i miss it yeah okay and over here just add in the monthly subscription case and that's it so your three types of purchases are done so after we have specified the product request for our three products catalog the one thing that you got to do is you have to fetch in the product from the app store we are going to write in few lines of code to fetch the product catalog from our store kit configuration file that we have mentioned earlier okay so we have added or rather xcode had added these lines as a stuff for the product request uh earlier in the tutorial so what we are going to do is we are going to write in our few lines of code to specify if the product is available or not okay so if you know that we have all these lines of uh catalog identifiers already specified or the product identifier is already specified in the tutorial okay so it will check whether this product is available in our app store or not okay okay so let's write if let product is equal to response which is of type sk product response okay so this will check if the product is available or not okay so let's write products dot first okay and it is then only that the uh that we can go ahead and purchase it okay so let's write print and just to say hey product is available product is available okay and then what do we do we have to purchase that isn't it so for that people we are just going to write in a function okay which will go ahead and purchase that particular product for us okay and we are going to write it next okay so let's write else let's say product is not available product is not available okay so we are just going to write in this block here function so let's write function [Music] purchase okay it's a custom function that we are going to write okay and it will take in product from sk product types okay so let's write products and sk product okay so this is a type of the argument over here and open up the brackets and over here we'll just say let payment is equal to sk payment okay and it will take in the product that we had passed in the argument okay so let's write product over here oh in fact i want to change it to a products just to specify it as an argument okay and i don't mix up with the uh with the names over here so that's it okay next what we're going to do is uh we'll just specify sk payment queue okay dot default dot add okay and we are going to specify it as self over here and again we have to specify this payment to the sk payment queue okay so let's write sk payment queue dot default dot add and specify the payment variable that we have declared earlier yes that's it you have done the first purchase or rather written the lines of code to conduct your first purchase okay and after this what you're going to do is we are going to call in within the stub so let's go and write it here so let's write self dot purchase and what is our product our product is o product over here isn't it it's an object so we are just going to pass in the uh the variable to the argument over here within the purchase function okay so what happens if the payment or rather how do you know that payment is successful or not so for that you have to pay in attention to the payment queue stub that is right here okay so there is a different statuses of payments okay or rather transactions as apple calls it okay and we are going to iterate through each one of them okay so how do you iterate we're just going to write a for loop over here so let's write for transaction in transactions okay and just open on the curly braces and here you will have to specify a switch case so let's write switch okay and we will write transaction which is with the n just transaction dot transactions state okay so here you can identify what is the uh the state of the transaction okay so what are the different states dot purchased or before that we just going to specify purchasing okay so that is just the customer has started to purchase okay he has not completed the transaction over here whatever you want to show in as a weight cursor or something like that you can specify it over here okay so let's write let's say customer is in the process of purchase i messed it up yes purchase and leave it right there next it is purchase so let's write another case block and just specify it as purchase okay and over here you have to add in the escape payment queue dot default dot finish transaction very important it notifies as you can read the description it notifies the app store that the app has finished processing the transaction okay and whatever you need to pass over there you're just going to pass in the transaction variable that we have declared earlier okay and what are we going to say we're just going to write in a print statement which says purchased okay next what we're going to do is what if the transaction has failed okay so you have right click yes we are going to add in the field case as well so let's write sk payment queue dot default in regardless uh if the payment is successful or failed you have to finish the transaction okay so that is really important for the developers perspective okay and the transaction is passed over here and just i'm going to copy this so that i don't type in much and just write field over here what else uh restored okay yes and there are cases where the customer wants to restore uh the the purchase okay so that is not part of this tutorial but hey you know we are going to uh we are going to do that very soon we are going to add in a restore tutorial so just stick around we will have that as well okay so we are just going to add in uh restore as well and deferred okay so there is one more called as deferred okay so what does it say a transaction that is in queue but its final status is pending uh so and there are some situations like this so that is also good to be handled so let's see deferred and we have to add in a print statement over here otherwise swift will not allow me to go ahead oops did i miss a d yes okay so the final case is done and uh what to do next okay what is the error all about okay so that's right break over here yes that's it so i feel we have covered or rather written the lines of code which is okay for you guys to go ahead and start in uh developing your in-app purchases but there is one more thing once you have added all these lines of code how does xcode know that it doesn't need to connect to the app store rather it needs to connect to the store configuration file any guesses i know you know that we have created the configuration.storekit file but for xcode to know that you'll have to go in and edit the schema within your target okay so this is a target over here and i have to specify the edit scheme and click that and over here you will have to select the store kit configuration file okay so if you can see this is the store kit configuration section okay so once you click on the drop down you'll be seeing the same configuration.toolkit file that we have added to our project okay just click that okay and click close i guess we are done okay so let's quickly add in this particular project and run it within our simulator okay so let's go ahead and run it on our iphone 12 pro let's give it a build and see if things are okay wow the builder succeeded and it's launching the app within our simulator there you go so our apps catalog is right here so what do you want to buy okay so let's say i want to buy 500 gems for one dollar and see if we can purchase that you can see the product is available customer is in the process of purchase that's right so that means the purchasing case is selected over there the product is available and let's specify confirm payment is done i guess and yay we have purchased our first product in our in-app purchase tutorial isn't that interesting and you can see the uh the status is purchased okay so if you want to see the entire list of the purchases that has been done by uh the store kit platform okay so you have to go into the debug and just open up this toolkit and just click manage transactions okay so as you can see that one of the transaction that we have created right now is here okay this is our first transaction and which has gems which is consumable and that we have purchased okay so this is our first transaction now in case if you want to refund it you can refund it from here if you want to approve decline resolve and delete the transaction you can just do it right within this window okay and let's go ahead and open up the second purchase okay let's say no ads yes confirm great so it's that simple to implement the in-app purchases in yourself project we just use the apple store kit only and not relying on any third-party utilities that you find in github i hope you like this video if you have any doubts please reach me out in comment section i'll be happy to help as always and please please please like and subscribe to the channel and see you until next time cheers you
Info
Channel: R&D Labs with Rohant
Views: 553
Rating: undefined out of 5
Keywords: InAppPurchase, purchase, in-app, swift, ios, storekit
Id: aN-WWJA4TgM
Channel Id: undefined
Length: 30min 37sec (1837 seconds)
Published: Sat Sep 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.