Swift In App Purchases In Your App (Xcode 11, Swift 5)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on everyone welcome back to another Swift tutorial in today's video we're gonna be learning how to set up an in-app purchase so basically what you see here we have our price or image and then we have this Buy button so unfortunately a net purchases can't be tested on a simulator but we're gonna go through all the semantics of registering a product with Apple fetching the product from Apple being able to handle different transaction states like purchasing purchased restoring failed etc and you'll basically get a very good bird's-eye overview of how the basics of in-app purchases work so that being said make sure you smash that like button down below if you're new to the channel welcome if you're returning welcome back get Xcode ready get excited and let's get into it so let's get started by creating a new Xcode project we're gonna stick with our single view application let's call this project I a P example save it on our desktop and let's get into it let me expand this Xcode window a little bit to give ourselves some more room to work so the first thing we want to actually do is come up here and go to this tab and we want to add the capability for in-app purchases to our app so let's hit this little plus for capabilities and search for in-app purchase and we can double click it and you'll see a new row gets added here this basically adds an entitlement under the hood that is required for in-app purchases to work the next thing we want to do before getting into the code is actually set up the in-app purchase product and apples portal so Apple requires that you set everything up on their portal because they review and approve different in that purchases so I've actually gone ahead and created this application instance so this ayah iOS Academy tutorial test app and if you're not familiar with this website its App Store connect dot Apple comm it's the portal we use to submit apps so the first thing that we need to do in here is match our bundle i D string to the one that we have entered here and you can see it's the one down here and the reason these aleena match is because we're gonna fetch products that are approved in our application in portal and identify for your app is used to map the request so let's come into here delete this and paste in the bundle ID so that's step one the next thing we're gonna do is go to features and we want to actually register in-app purchases so let's hit this plus and it'll get these different types of in-app purchase products we can have and they're pretty self-explanatory but it's just to run through them pretty fast we have consumable products like looks like coins and a game like candy crush there's no limit to how many you could buy non consumable which is what we're gonna be doing is something you can purchase once so let's say something like removing ads then Auto renewing and non auto renewing subscriptions which would be like a subscription to like tinder for example so let's do non consumable create it and we want to first give it an identifier just a reference name so remove ads the product ID is gonna be com let's do is Academy dot test remove ads so this is just convention for how you should name them it's not really a hard to find rule let's give this a price of $0.99 let's also give it a display title and description and you can give this information to multiple localizations and languages generally if you have this in the language of the user that they can read it will result in of course more purchases and I think that's everything we need to finish here and fill out this information down here is not needed for development this is needed for App Store review purposes when you submit and this is needed optionally if you want to promote the purchase in the App Store so let's hit save and we can actually dig into the code once this decides to save and stop being slow still saving okay it finally saved actually just kidding it didn't save looks like we hit an error let's try that again okay so let's let me actually change this because I've used this before so let's drop the s and just make it remove an ad so let's do save one more time okay cool so it's saved if we come back here and reload the page because apples websites never work we should see our in-app purchase in the list of products now so there it is so cool let's actually go to the code and put this together so we're gonna need a couple things the first thing we're gonna need is a screen aka another view controller where we can do all the purchase stuff we're going to need to fetch the product itself from Apple we're gonna have to handle the purchase and we should also probably save to disk that the user has unlocked an item so when they open the app the next time they still have access to it so we'll also do that so let's create an IB action in here and when the user taps a given button we're gonna show the upgrade screen so did tap remove ads let's guard let a view controller from the storyboard so we're gonna instantiate a view controller with an identifier of let's call it upgrade and let's say as a she rather else return and then we can say navigation controller push vc animated true and we're gonna say the title for this view controller will be remove ads let's actually create this view controller and do some setup in our storyboard so right click that new file cocoa touch class we want a subclass of a view controller and we're gonna call this UI view controller or sorry upgrade view controller let's create it get rid of this code that's template code that we get now let's go to our storyboard and in our storyboard we're gonna do a little bit of setup so make sure your storyboard is selected here and not your launch screen I've seen some folks use this instead and there are two very different things but let's first start by selecting the view controller coming up here and going to editor embed in navigation controller which gives us this bar up here let's select it and give it a title of home and let's add a bar button item to the bar here it's diminished smidge and call this remove ads let's connect our IB action to this button like so next we want to bring in another view controller which is going to represent our upgrade screen and I just call it an upgrade screen for the sake of my own sanity just because I've called that type of flow that for like 10 years at this point but it's basically just a screen where you're gonna make purchases so let's select that controller and give the class name as upgrade view controller which is a new file we created and the storyboard ID will be upgrade and on the screen we are going to want a button to actually buy the product so let's select this and find a button drop it in and let's make this button like a little nicer so people are actually enticed to select it and purchase our product so let's give a blue background color to the button and let's change the tint to be white and let's actually also change the text on it to be by now and finally we want to add some constraints by selecting it coming down here and let's give a constraint of twenty twenty ten and a height of 50 to cool so we have a nice for looking button now so let me go create a IB action in here that's gonna get triggered when we press that Buy button so did tap buy and let's go back to the storyboard and connect that IB action so let's select this right click drag from this IB action to our button and we want to select touch up inside and for the sake of this video being an example I'm gonna drag in a static image and text which is going to show our basically image to unlock remove ads and the label will show the price in a real app you probably want these things to have outlets so you can configure them but we only have one product so this this should definitely suffice for the video so let me add some constraints if I can select it properly we're gonna say 10 10 10 and height will be 300 and let's give us an image of I think LOC is one of them that's not what we want let's see what images do we want so we're gonna use this one this is not a great image for this type of purchase you should have something that indicates remove ads but again for the purposes of this video this is good enough it's dropping a label and let's give this some constraints - it's 1010 1010 and we are going to Center this and we're also going to bump the font size so let's select this and make this bold or semi bold let's also bump up the font size and let's double tap or double-click and put the price there so when you actually fetch the product which we're going to do in a second you'll actually get the localized price so for example if your user is in Europe using your app you probably don't want to show your price and dollars so you actually want to use the price that the product is getting from Apple's fetch response it just makes it more streamlined for the user and I also think it makes it more likely for them to purchases and it's in their own currency so let me actually go to the code now and let's put this together so we want a function which is gonna fetch the product so let's create a function called fetch products so I copied the name of the product when we created it in the portal so that's what the name was we want to import the framework that Apple gives us to deal with purchasing stuff which is Store kit we want to call this fetch products function we want to have a property on here which is my product and it's a SK product and once we fetch the product successfully we'll assign it to this so to fetch a product we're gonna simply say request is SK product request and you initialize it with a set of product IDs which is what we copied here then we want to set the delegate for the request lastly in here we want to actually call start on the request so this delegate will start complaining because we haven't actually conformed add the conformance so up here we can simply put a comma and say SK product request delegate and the other thing we're gonna need to conform to is the actual protocol that allows us to handle the purchase state for a transaction and let's ignore this error for a second so that protocol is SK payment and I can spell payment correctly transaction observer and you're gonna see an error pop up here again and if you click it you'll see it's basically yelling at us because we aren't implementing the two required functions so hit fix and it'll put them in here automatically for us so let me copy these and move these down here and basically the first one is the delegate function that gets called when this product request returns and this this one here is the function that gets called when you initiate a transaction so let's implement this one first so we're basically going to check if the response contains our product so we're going to say if let product equals response products stuff first and notice the products is plural because we could fetch more than one product at a time so you can check rather instead of doing this you can check the count of products returned that's usually what I do in my apps when there are more than one products so in here we're gonna say my product which is a variable we created up here we're just gonna basically assign my product to the fetched product and for the sake of this video let me also print out a couple of things about the product so the product identifier the price the localized title and the localized description so I can mention the product actually returns the price as well as the title and description and that title and description actually mapped to the given language that the user is using your app in so if you recall we can add any language that we want from this list and I encourage you to do so for a better user experience and also just to get more purchases so that's the first one this function is actually a little more interesting so we want to iterate over each transaction in this updated transactions array so for transaction in transactions we're gonna switch on the transaction transaction state and there's a couple states so let's do purchasing first so this state gets called of course when the user starts purchasing and we don't want to do anything here so we're gonna say no op and break and let's see this is complaining because okay the error went away so this is complaining because there's a bunch more states we want to handle so we're gonna do purchased and restored so this gets called once of course the user has restored a prior purchase or they purchased something new aka payment went through so you should unlock their item and then of course there is failed or deferred and deferred is basically like the user wasn't able to finish the transaction so it's still in the queue of transactions to finish but it's been deferred for one reason or another this case is very unlikely so I wouldn't worry about it too much so what we need to do in these two cases and also in default I guess is we want to actually finish a transaction that we're iterating over and we also want to remove the observer so we're gonna say SK payment queue default finish transaction transaction and we're gonna also take this and we're gonna say remove self and let's copy and paste this to the failed case also and the default case and let's see we want to actually implement the IB action to start a purchase and to do this we want to do a couple things we want to make sure that our product has been fetched so we're going to say guard let my product equals my product otherwise return and then we're gonna say if SK payment Q can make payments and this basically checks if the current device has an Apple ID signed in if they've have a valid payment etc etc and this is giving us a warning because we haven't used as variable yet but if we can make payments we're gonna basically create a payment object which is an SK payment and you create a payment with the product and of course a product is going to be the one that we're unwrapping here so this wording will go away and let's see it should go away there it goes so now what we want to do is in SK payment queue we want to select the default queue we want to add an observer which is self so the current class will handle the transactions that we're about to put into this payment queue and then we want to add a payment and that'll be our payment and that basically does it so once the user purchases whatever you're selling you probably want to do something along the lines of this so let's say the removing adds so I would do user defaults standard set and I would set true for adds removed so obviously I just made this up but basically wherever you're showing ads in your app so for example if that's in the first view controller let's say we're creating ads in here I would basically check with the value of this's so it's a bool for ki and if we haven't removed ad then basically show ads otherwise of course we don't want to show ads and same thing for actually showing the upgrade screen we don't want to show the upgrade screen if the user has already upgraded and purchased the item so you probably want to remove that button but yeah that's basically how you make in-app purchases and the other thing I'll mention after we hit command already build and run is unfortunately you can't actually test in-app purchases on your device so if we hit this we get our screen with the lock and the price and if you look at the console you'll see that we're printing out the things that we get in the fetch response so $0.99 is the price that we're getting back remove ads is a localized title and this is the localized description and this is the ID for the product and if we actually tap this Buy button you'll see that nothing actually happens because we're on a simulator and we're not capable of making payments on a simulator so in the by IB action that we checked if you recall we check if can make payments and of course we can't so they won't go in here so to actually test the purchase you want to deploy to a physical device once you have your device connected it'll show up in here in this list it'll be the first one but that's how you can actually test on device and the other thing I'll mention is if you look in the console you'll see that it shows 0.99 and there's no indication of currency type in here so if I recall correctly there is also a currency object off of this or maybe there isn't I recall there was so let's see a product dot price hmm let's see there is definitely a currency somewhere in here so let's see there's discounts content version is downloadable price title locale okay that's what we want we want price locale so if we hit command R and let's see what we get for price locale I think it'll give us a dollar sign or it'll at least tell us we're in the states so you'll see that the locale is English United States currency and the currency is USD so you can actually take this locale and convert it into the dollar sign so the similar thing would apply for folks that are in Europe for the Euro or Canadian dollars or Chinese yuan or whatever whatever currency or you're using so yeah that's basically how you can set up an in-app purchase so I know this tutorial was a little all over the place but just to recap you need to actually create the product in the app store Connect which is Apple's portal and the reason you need to create it here is you need to fetch a product you can't just create a payment of something that Apple doesn't know about then the majority of the code actually all of it to make the payment is in this controller now notice we're not handling restoring of purchases that's a little bit of a separate topic but we first fetched the product via a SK product request and notice all this stuff is inside a store kit we conform to the delegate and the transaction observer we created an IB action to actually create a payment and add the payment and observer and then we have the delegate response for the product request where we're just printing out the information about the product and assigning it to our local variable and then finally we have the function which handles the various transaction states of a given transaction so that said that's basically everything I wanted to cover hopefully you guys found this video helpful it's a very bird's-eye overview of this process I'll probably do a Matt in-app purchase depth videos for like subscriptions and whatnot so if you haven't done so already smash the like button down below comment if you have a question clarification if you want to see a particular type of in-app purchase video cool thanks for watching see the next one
Info
Channel: iOS Academy
Views: 18,926
Rating: undefined out of 5
Keywords: swift in app purchase, swift 5 tutorial, swift programming, in app purchase, how to set up in app purchase, swift storekit, swift 5, xcode 11, swift make app, swift app purchasing, make money with apps, swift for beginners, swift learning, swift tutorial, xcode storekit, storekit, app purchases, swift development, how to make app, how to make iphone app, app programming, in app products, appstore connect, apple app purchasing, swift tableview, swift make money, tableview
Id: qyKmpr9EjwU
Channel Id: undefined
Length: 24min 26sec (1466 seconds)
Published: Mon May 04 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.