In App Purchases Basics & Intro (Swift 5, Xcode 12, iOS 2020) - iOS Development

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on you guys welcome back to another swift video in today's video we're going to be talking about something that has been requested a ton by all of you and of course that is in-app purchases and how to set them up for your app so here we are apple's developer page for in-app purchases and they've got a whole lot of information here about you know what they are different types all the things you would care about i'm sure the majority of you know you know in-app purchases to unlock things in your app here they've got a visual kind of showing what those screens look like so we're going to have a birdseye overview in this video of how to set them up and fetch them from apple the products themselves present the payments as well as how to structure your code for a scalable model for presenting in-app purchases we are going to have follow-up videos because this topic is pretty broad and there's a lot to cover and we can do it in different segments so that all said make sure you destroy the like button as usual uh helps out the videos and channels quite a bit uh every time you hit that like button it gives me a little bit of a warm feeling inside so make sure you hit it and again if you're a returning viewer i think 50 of you guys watch these videos consistently and haven't hit subscribe so please make sure to do that so we can grow this channel together that all said i'm gonna be quiet and we're gonna open up xcode and jump into some in-app purchases quick pause before we get into the video if you haven't seen it already i am hard at work putting together ios academy.io a community where all of us ios engineers can come together learn how to build some of the top apps like facebook youtube and instagram in addition to interview prep to land some of these ios roles at top tech companies so if you're interested in the free and premium content to come head on over to ios academy dot io and enter your email address in the waitlist form and you will be notified as content becomes available that said let's get into the video all right so we're going to go ahead and get started by opening up xcode and creating a new project we'll stick with the app template here and let's go ahead and call this project in app purchases uh intro and let me hit next make sure your language was swift uh lifecycle ui kit and interface storyboard go ahead and save it and for the purposes of this video we're not going to actually run it in the simulator we're going to talk a lot about how in-app purchases are really builds under the hood and then i'm going to have a follow-up video where we'll take a look at uh using kind of mock data in xcode to test our in-app purchases in our simulator which is something new in xcode 12. so that all being said let's uh first and foremost come to our view controller and what we want to start by doing is importing storekit and essentially storekit is the framework that allows you to interact with the app store hence store and this is uh the framework we're going to use to communicate with in-app purchases so next up go back to your primary project and select your target here in the top bar you want to come up here to signing and capabilities and let's see we want to add a capability so hit this little plus and search in here for in-app purchases go ahead and double-click it and you'll see it gets added right here and you will need to add that there for the purposes of testing later on but we'll just do it now to start off with the next thing i'll call out is your bundle id really matters for in-app purchases so we have this set up here right now but we actually test it in the follow-up we'll take a look at why that matters but just be aware that you want to have a bundle id here that is something reasonable that you plan to use so that'll send back to our view controller so what's the flow of in-app purchases well we're just going to add it in comments here so the first thing we want to do is fetch product objects from apple and basically our products are all the in-app purchases we have registered with apple and they're kind of enumerated based on their product identifier after that when a user interacts with like a buy now button you want to prompt a product payment transaction after that we want to observe the transaction state so these are basically the three key components of interacting with uh in app purchases and creating a in-app purchase so let's actually implement all this instead of me going on and on so the first thing we want to do is fetch our products so let's create a function here called be a private function called fetch products and later on the video we'll take a little look at some good practices of how to structure your code as well but here in view to low so it's called fetch products and let's see how to fetch a product so we're gonna have a request and this request is going to be a sk product uh request like so and the constructor takes a set of product identifiers and those identifiers are nothing more than strings and these identifiers generally people in apple's portal and apps4connect will register it along the lines of your bundle identifier i think it was in-app purchases it's not the product name so let's say we have a product to remove ads so it'd be something along the lines of this and of course you can pass in multiple here the next thing we're going to do here is supply a delegate and this is how we're going to get uh you know when the callback comes back and we get the actual products or a failure and then we're going to say request dot start so right now you're going to see an error pop up because we don't conform to this delegate yet so let me actually clean up our code a little bit already let me create an extension and put all of our code here in our extension on the view controller and this extension will also conform to sk product request delegate and if you click into the delegate here you'll see there's one function in here and that function is product request did finish with products so let's go ahead and paste that back in here and what we're going to basically do is we're going to pull out of the response that the request gives us the products so we're going to say what products equals response dot products and for now instead of having it in this local variable here let's go ahead and create products on our view controller this will be a private var for products and it's going to be an array of sk product and like i was saying a product basically represents a purchase purses purchasable products it has a price associated to it a title a description all that good stuff that you would expect so this is part one where we need to fetch a product now there is another function in here for a request did finish so an sk request did finish rather fill with error and there's actually two types of requests so in here we're going to say guard request is a sk product request if it's an sk product request and it fails we want to know about it so we're just going to put a print in here we're going to say product fetch request fails and of course if it fails you want to show your user some appropriate ui we're not going to do that here the next thing we're going to want to do is create a function called purchase and purchase is going to take a product so we're going to basically call this function and say present a purchase so basically the card that slides up for any in-app purchase similar to how you download an app in the app store and it's going to basically ask the user to confirm if it's you know free or paid with either face id touch id or their password and the first thing we're going to want to do in here is say sk payment queue can make payments and as the name implies here this static function basically checks if the user can make payments so aka has a you know itunes store account linked and set up and good to go the next thing we're going to say is we're going to create a payment so payment is going to be a sk payment and you create a payment with a product so this is slightly misleading think of this more so as a payment request so you're not actually making a payment here you're creating a payment request to hand to the transaction queue so the next thing we're going to do is we're going to say sk payment q defaults and we're going to want to add a payment and this should be product singular not plural and before we actually add it we also want to say sk payment queue defaults and we want to add an observer and this observer is going to be whether we want to let's see add and we want to add a sk payment transaction observer so this function right here and we're simply going to do self an sk payment transaction observer is nothing more than a protocol and as the name implies this allows you to observe any new transactions that come into a payment queue and the updated state on it so the function we're going to want here is updated transactions and this is where we can handle any of the transactions that come in so before we do that let's talk about what this payment queue is so the way that apple has architected their in-app purchases under the hood is there's basically a list of transactions in this app-wide or system-wide queue think of it literally as a linear queue in which there are transactions and all you're doing in your app is you're going to add another payment request to that queue and the system is responsible for prompting that in-app purchase window to pop up and you have all of these delegates and call backs to get different uh you know states of what's going on and handle appropriately in your app but that's basically how it works fairly straightforward the system does a lot of heavy lifting for you so now that we have the updated transactions here in here all we need to do is uh iterate over the transactions so we're going to say transactions for each whoops i can spell for each and in here dollar zero now represents the nth transaction let's make it simpler and just say tran zaction in and we want to switch on the transactions state so we're going to say switch transaction dot transaction state and i believe there's five cases here so the first one is purchasing which of course implies that the user has started purchasing the next one is purchased and this implies that the user has finished purchasing then it's restored that the user restored that in-app purchase which of course is a requirement for apps for approval for folks that have previously purchased your item next is failed which is self-explanatory as well deferred i believe is at the top of my head is when a transaction gets added to the queue but is deferred for presentation and let's see i believe that's that's all five of them so these are the five states that you must handle and let's see if the error goes away let's see why this is giving us a warning so we want to also add per this morning here a unknown default in case some other state gets added later on basically something unknown occurred so when we get this function called we get an array of transactions that have been updated and the way you want to handle this is depending on how you save it so let's say we want to save our current product being purchased so let's say we say product being purchased and we can say that's product and we can create this product being purchased variable on our controller it's an sk product and we assign it just like that and here we could say in our uh for each statement we can say guard transaction dot so a transaction has a bunch of things on it right so we're gonna say transaction dot payment dot product identifier equals self dot product being purchased dot identifier else continue so basically what you're saying here is if the transaction in our for loop here is the product that we're purchasing at the moment we can basically go ahead and continue the reason that's giving me an error is because a full reach is going to iterate every single time but you could have this be an if statement but the idea is when we come into each of these cases in the transaction state switch we want to handle it right so when you come in here and uh you get a purchased event this you need to be explicit about handling that the product for the target transaction in the loop was purchased is not necessarily the payment you queued up here so the way you would handle this is handle purchase and you would hand in the transaction dot payment dot product identifier and this would be a separate function that would take in of course the product id as a string and based on this you would handle however you unlock your product in your app whether it's user default so on and so forth the takeaway here that i want to really drive home is you need to be very explicit about the transaction payment and what product it's for and the reason is imagine if you prompt a in-app purchase and your phone dies right the the payment queue might not clear out to that transaction so the next time the user opens the app it might be there so you don't want to accidentally unlock everything for your user or assume that a payment went through and things like that you also want to be very aware of handling failure states in that graceful way show the user a nice error let them retry don't just assume that this is going to work there's a reason apple gives you these states and of course you must implement restored as well which i'll have a separate video on to allow the user to unlock or re-unlock purchases they've made before that are so valid so that's how in our purchases work in a nutshell and if i wanted to uh basically prompt a purchase let's say we have fetch products here and we're able to fetch a product let's say here we say guard let's product is products dot first else we're gonna return if there's not a first product in here and we can simply say purchase product and let's uh refactor our code before we actually wrap up so this is the bird's eye view of how in-app purchases really work let's actually refactor a bit because you can imagine that you might have multiple view controllers in your application where you want to present you know in-app purchase upsell opportunities to the user right so you might have a unlock screen you might have a discount screen and implementing this stuff in every single view controller is a pretty bad practice but b it's a lot of redundant code right and we're lazy we don't want to write this much code so let's create one object to handle all of this for us so i'm going to create a new file here and we're going to go ahead and call it iap manager so in-app purchase manager and it's going to be a basic class called the iap manager and it's going to be used as a singleton so we're gonna have a shared instance of it just like that and we're also going to want to import storekit and all we're going to do now is take all of this code that we wrote down here in this extension take all of that code get rid of this fetch products and also we're going to grab this in just a moment but go back to your iap manager and simply paste that stuff in here let's come back and grab these properties and go ahead and paste that in here as well we want to make this fetch products public and let's see delegate itself so we also want to add conformance to all that good stuff here so iep manager needs to conform to sk product request delegate as well as sk payment queue let's see sk transaction i think transaction observer which is i always forget what it's called because it's transaction or something along those lines sk payment transaction observer cannot declare conformance to ns object protocol so what it's basically complaining about here is we want iep manager to be inheriting from ns object just like that and you should see your errors go away and if you hit command b everything should still be compiling and the beauty of this is uh before we actually see it let's go ahead and make the function for purchase public as well the beauty of this is you can go to your app delegates and in here you can actually prompt your in-app purchase to fetch your products and you can store them early on in your app's life cycle to be used wherever so you would say in that purchase rather in that purchase manager shared fetch products and for those of you who don't know you can hit command shift o to get this window to search for files across your project and in your view controller when you want to prompt a purchase for the user let's say they hit a button you can say iap manager dot shared dot purchase and you just need a mechanism to pass in a given product so you might want to have another function on here which takes some uh enum or something and returns a product object so the point is you have now one class that can handle all of this stuff for you and the absolute last thing that i will call out in here is uh when you want to request things like multiple products passing in these ids like this is not very scalable and clean so a great practice that is very popularly used is create an enum on here and have it be have it have a raw value of string and make a case iterable so we can get all the cases in here and let's say we have three products so remove ads and unlock more and a tip we could basically throw our identifiers in here so instead of you know passing these in directly like this if i just go ahead and paste all of these the beauty of this is now you have a definitive list of the different products that your user can purchase so when we want to request with all of them we can simply say product dot all cases and we want to compact and map these to each products raw value so let's see looks like this is complaining let's see why it's complaining i believe it's because it's expecting a set and we're passing an array so you can actually take this whole thing and wrap it as a set and the set is just needed so we don't actually request any redundant products because every product should be unique and this now allows us to essentially figure out a definitive list of products across our whole app so if we want to show options for the user to unlock it makes everything nice and clean the other thing i'll mention actually i keep saying my last thing and i keep adding a thing so this will actually be the last thing an sk product has a bunch of properties on it which are useful that come back from apple things like localized description localized title these are all properties you add in app store connect when you register a in-app purchase with apple so while you might have strings in your app to show the product you kind of want to do it you want to show the information the user's local you know description and title their local price as well uh price locales things like if you're in europe you'll see euros if you're in the us you'll see dollars so things like that so fetching a product point being is pretty important to even render a product purchase moment and there you have it that's how in-app purchases are put together in a nutshell takeaways are you need to request a product you need to validate the user can make purchases add a payment for the product and then manage the states of that transaction you should do it in a object that can be used app-wide so you don't duplicate your code and it's pretty straightforward so in a follow-up video like i said i'll take a look at how to you know debug these in xcode and have some mock data as well as how to register these with apple so that all said thank you so much for watching if you haven't destroyed the like button already make sure you hit that like button as per usual for the video and channel at large helps out with the algorithm quite a bit comment with any questions suggestions feedback down below i love hearing from each and every one of you thanks again i'll catch you guys in the next video you
Info
Channel: iOS Academy
Views: 5,270
Rating: undefined out of 5
Keywords: swift 5 tutorial, swift 2020, swift, swift 5, swift tutorial, swift for beginners, swiftUI 2020, swift programming, swift basics, swift apps, make iPhone app, swift make first app, Swift in app purchases, how to set up in app purchases, iOS swift in app purchases, in app purchases swift, in app purchases restoring, prompt in app purchase, swift 5 in app purchase, in app purchase swiftUI, in app purchase Xcode 12, swift 5 2020 in app purchase, 2020 in app purchase, iOS, app
Id: skhYzNcfmnU
Channel Id: undefined
Length: 23min 36sec (1416 seconds)
Published: Sat Oct 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.