Adding In-App Purchases, Sandbox Users, & Restoring Purchases (Swift 5)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome back today we're going to learn how to do in-app purchases so it's a combination of app store connect and the Apple Developer website and a store kit in Xcode so since it's those three things I broke it down into ten steps which seems like a lot but it's not going to take us that long but they were bouncing back from website to Xcode so yeah those 10 steps are going to create the Apple Developer account now I'd like to let you guys know so you don't go any further it does cost 100 bucks a year so if you're not in a position where you can do that right now or you're not ready to shell out that money and in order to do some of these features and put your apps and elapsed or things like that you know by all means skip this video and check out my other ones but but but yeah if you have a developer account great if you're ready to you know throw 99 bucks a year down starting now also great so you're in the right place next you're gonna register your app on the developer website the same one where you created the account we add the app on App Store Connect which is a different website also Apple website that's you know I don't know why there's two different websites but there is you know enable in-app purchases in Xcode that's where we jump back to Xcode and we're going to do a few things there we're also going to create a let constant for the product ID that we created in App Store connect and then we import store kit and add necessary protocol the payment transaction protocol we go in and we add ourself as the payment queue create the payment transaction method and then check if user can make payments and add payment request method so these last three steps eight nine and ten are all methods or functions that's the real nitty gritty of putting it in the app but before that we have to do some setup stuff on line so let's get started with the developer account now if you if you already have a Apple Developer account and you're familiar with where I'm at right now but otherwise you need to go to developer.apple.com click account and then there will be an option for you to register it I don't want to you know show it show my login info to the internet right now I trust you guys I just you know it's everyone else I don't trust of course but you'll see you'll see the option to register and you can just you know follow the inputs to you know go ahead and give them that that ninety-nine a month that Apple so desperately needs now that we're in I mean they provide a lot of services they come with that $99 some hunch but I just I just joke around about that you know then taking all the money but in here we're going to if you want to upgrade your membership go to membership and then so see mine's already set up like this but you know this is where you to add all your stuff and then we're going to go to certificates IDs and profiles and once we're in here we're gonna go to app IDs and then we're going to hit this little plus at the top and in the app ID description the name of our app that we haven't started yet but this is what I'm going to call it it's gonna be in [Music] purchase demo and then down here this is going to be all filled in with your team ID and then the app ID suffix you're gonna want to put a explicit app ID and then for the bundle ID it has a reverse domain name style string and that is correct you want to put if you have a website put comm dot your website dot the app name if you're a single developer just without you know company or anything just put comm dot your name dot at and do you use like your full name here because it has to be unique that's that's the reason for this so it's gonna be in that purchase demo so that's that's what mine I just use my full name and then in here the app services here are already checked the in-app purchase and Game Center these are automatically in so all we have to do is hit continue and then we hit register and so now it's registered and we have to take that bundle ID in-app purchase demo right here and we have to create an Xcode project with that name of course if you're just trying to add this to your app you would put your bundle ID right there to link it up so ready go Xcode new project in Xcode it's going to be a single view app the project name is going to be in app purchase demo and see the bundle identifier is going to be in app purchase demo and we don't need any of these boxes checked and we're ready to go on the desktop all right there we go so we have created this project and we're gonna come back to it in a minute for now we're going to go back into our browser go to app store connect which is app store connect Apple comm you're going to log in with your developer account which I already have and we're gonna go into well before before we register the app make sure you go into this agreements tax and banking here and make sure that these are set to active if they're not it'll tell you what you specifically need to do for this one when you're first starting out for the paid applications you have to accept certain terms and you also have to put in information you know contact information tax information just it's it's pretty simple to fill out it's just like you know like your your social and like a couple other things it's really you know it's pretty user friendly and it'll tell you what you need to fill out over here usually or when you click on it but yeah so when you have both of these are green also after you update that and might take say it says up to 24 hours for it to show up as active when you give it your data so if that happens just you know bookmark this and come back to this video but usually amita it'll take like took me like 30 minutes to an hour or something to hear back from Apple and they were like yeah you're good to go so now we're going to go into my apps and in here we're going to hit that Add button and for the name it's going to be in app purchase demo also this name has to be unique so you know you can just add since we're not gonna actually put this one on the App Store I'm just gonna add by Michael and maybe that'll go through and select for the primary language do the country that you're in for the primary language for the bundle ID want to select in-app purchase demo or whatever funnel ID you've just created for the SKU you put whatever you want here so for me I just put mic - 0 0 - for this particular one and we'll create and oh that name that I just put in in-app purchase demo by Michael you you can't change it so don't worry too much but put that's going to be what shows up on the app store so you know put what you want to show up if you in deep plan on putting an app on the app store next thing is to go into Xcode and we're going to go while we're in the main page we're gonna go to capabilities and in here in here go to in-app purchase right here and if you have a developer account this option will show up if not it won't but yeah we're going to flip that on and gives you the steps right here and as you can see we've already set this stuff up next we're gonna go and we're going to create that let constant for the product ID but in order to do that first we need to create a product ID for our in-app purchase and that is in back here an app store connect now we're going to go to the main page that should pop up after you've created your app features and in-app purchases obviously gonna kill us to add an in-app purchase now this is the different versions of in-app purchases that they have Apple handles you know all this billing and everything like that so what basically so a consumable is something that like if you have a game and you know you want to use a powerup that can go away like once you used it that's a consumable and then a non consumable is something that they purchased once and does not expire or decrease with use so once they've bought it they've bought it and that's what you know a lot of in-app purchases are you know mostly non consumables unless it's a game or something like that then you have Auto renewable which is pretty self-explanatory the non renewing subscription which is probably the least common of all purchases but it's you know it's just a buy a year of this service and it doesn't automatically bill you after the year so for this one it's going to be a non consumable I'm going to hit create and here we want to put what the purchase itself is called so for this one we're just going to call it purchase example and in the product ID it's wise to just take take whatever your bundle ID is and add the reference name just for you know the sake of congruence to keep everything simple easy to read easy to understand so this we're going to come miles which is me it's gonna be in app purchase demo dot purchase example and now we're gonna want to copy that to send it over to Xcode here in a minute and in the pricing and I'll just say whatever $0.99 just you know so if you do this accidentally you only wasted one dollar if you do it the wrong way I am creating another video to show you how to create a sandbox user but that's you know we'll go over that later and here in the app store information you can in here you can add if want to put it in other countries you can add the display name in the description they also they're all different but for this one we're gonna do the same as when we put up their purchase example and the description is a sample of in-app purchased content just to give it a description that's what the users gonna see when they're when they click on the Buy button so you want that to be explanatory but not you know a whole paragraph and so now if we're going to save now if you still have this copy or you can copy it right here go into Xcode and we're gonna go to our view controller constant that is the product ID it's gonna equal a string and the string will be the purchase example now we're just going to do a little bit of setting up the app this is going to be a super simple app but in here in the storyboard we want to create a UI button that's going to basically be our purchase button up the library button and let's go ahead and let's just fix this and align it horizontally and vertically in the container let's move it up I think my face might be in the way down there there we go horizontally very clean container and so that's there just make it more visible I want the background to be a different color be blue and then the text color can be white and then let's make the button a little bit bigger so for width we're gonna go 150 and then height of 50 see how that looks yeah there we go that's fine you just call it purchase there we go and so now we're gonna need a UI label to show us if we indeed purchased it so this label we want to do the same thing Center horizontally we don't want to Center it vertically so it's in the same spot just just horizontally so it's in the middle and then here we want to say you know 100 degrees from the bottom from the top of the purchase button so that's what we're doing in this case and there we go that's that's really all the constraints we need to add right now here in the view controller we're gonna link up yes let's go in here and we're gonna link up the label here purchase label let's just call it that and it's an outlet and down here we're going to create an action before the purchase button purchase wrist touch up inside everything looks good alright now we're going to go into the view controller and close out of this here I'm gonna go into the view controller and we're going to import another library called store kit and that's you know Apple's store store kit basically and here we're gonna add SK payment transaction observer so what we're looking for so we're gonna add that and then in a second it's going to pop up do you want to add protocol stubs and we're gonna say yes please and so this is basically what is going to process your payment and we're going to take that just you know keep everything nice and neat we're gonna drag it down here keep the variables on the top and we're gonna just delete that line just too much empty space here in the viewdidload I'm going to take SK payment queue which is added from the stork it dot default with the parentheses and we want to add an observer so as you see we are a inheriting from the SK payment transaction observer class and we want to make ourself the observer so we're going to say self and right here I want to go ahead and build oh the places you of course yeah I just want to build to get everything to you know all talk to each other like the importing of the store kid and the payment transaction observer just want everything you know you want the autofill to be there so I like to build a lot in the project all right going back to the project itself set up the protocol stub that we put in from the transaction observer and if you'll see it's a it's the input is expected to be an array of the sk payment transactions so we need to check each individual transaction in the transactions even if we only have one of them so we're going to use a for loop to do that and it's going to be for transaction in transactions and I got transactions from right here so in here we're going to check the transaction state and basically just see like if they've purchased the item or not and what to do if they have so it's gonna say if transaction transaction state equals purchased to so this is you know if item has been purchased we want to put whatever code right there so down here we want to go else if transaction transaction state equals failed I'm gonna go ahead and fill this one with a print statement transaction failed just because that's you know that's really important especially if something fails you want to know and in here let's go ahead and put a print statement saying the transaction successful it's one of my favorite ways to you know go through and and check for bugs and everything is to just have print statements and next we're going to actually change this purchase label when the transaction is completed so it's going to be purchase label dot text equals purchase complete it here we're gonna do nothing let's just go into the viewdidload and go ahead and set what purchase label is purchase not completed yet well we're going to do the last two steps now we're gonna add the payment request and we're gonna check if the user can make payments which is of course a necessary step yeah Apple has protections against you know things like parental controls where you know you don't want your kid buying the same powerup like a million times so that's why that's why you have to check to see if they're able to even make purchases within the app store with their account so here we're gonna create an if statement to see if basically see if they're able to make the payments so can make payments pretty pretty self-explanatory and then down here we're gonna do else yes or just else because that'll you know else just basically considers any other situation so that would be if they can't so which this is a bool so if it's true then it's going to happen here and if it's false then else will happen and we're gonna print user unable to make payments and in here we just basically put the product request or the payment request create a constant for it to SK mutable payment and the payment request product identifier is going to be the same as the product ID that we created up there and that should be in camel case for anyone that noticed there we go and then we're gonna go to SK payment Q dot default dot add and then down here you know add our payment request and there we go we're gonna we're gonna build and run just to see that how the app works but unfortunately we can't run the app without creating a sandbox user in the app store unless you want to pay a dollar for whatever this whatever you've created and creating sandbox users can't takes a minute so I created another video about it and as you can see so as purchased not completed yet hit purchase and the debugger is going to say the transaction failed because you can't do it on simulator unfortunately so to go to the next part with the sandbox you have to have your own device doesn't matter whether it's I phone XS or iPhone 4 like it really doesn't make a difference but just you know have something to test it on even an iPod touch yeah if they even make those anymore I don't know but yeah hope to see you in the next video where we create a sandbox user and we we get to actually test this in-app purchase what's up my fellow Swifty's today we're going to create a sandbox user for our app so basically so in order to test your in-app purchase on you have to have your own device so you test it on your own personal device doesn't matter if it's like a iPhone 3 or an iPhone 10s like it doesn't matter and you also have to create a sandbox account now in order to do this and to do it in app purchases anyway you have to have a Apple Developer account it's $99 a year you sign up on the developer website so just say and you know letting you know all this in advance now let's get started with our sandbox users in here see we're in App Store connect its App Store connect apple comm and I'm gonna go into users and access and on the Left sidebar here it's going to say testers under sandbox and a click on that and an add one and you have to put in all this information so I'm just going to put testing test face the email it's going to be an email that I created just to test things you do have to give a unique email and a good password which is kind of annoying but you know apples security feature stuff and you have to do this every time you add a sandbox user so a secret question is gonna be why - I need this answer is going to be you don't hey I did for our birthday just put some random stuff here United States make sure that's in the mines in the United States because I'm here but wherever you're at make sure it's in that area and it links up to your app what uh what country you set the app I've done now now that I have this I'm going to go into my phone and we're gonna we're gonna actually go to Xcode and run the app the in-app purchase demo app that was in our last video and we're going to run that and then I'm going to show it on the screen right here and we're going to go and we're going to click purchase and it's going to ask that you sign in so you want to sign in with the the account that you just created finicky so I'm going to go in and I'm going to delete the in-app purchase demo app and I'm going to go into Xcode and I'm going to reinstall the app so now here it is again you know hit purchase and it's going to ask for the password we're going to put that in one more time and then you're was successful ricketts going to say environment sandbox so make sure it says environment sandbox when you're putting in all this information or you're going to end up paying for your own in app purchases but yeah that's it it's a pretty pretty straightforward and there's one more thing I wanted to add so how do you check if you have a sandbox user and that's by going into your phone and going into settings and then I'm Marty therapy you go to scroll down to iTunes and App Store and you want to scroll down to the very bottom and it'll have your sandbox account right there you can click on it to sign out and sometimes you might need to sign out because a an in-app purchase that you made you may want to make again in order to test a new feature or something like that but I hope you found this informative stay tuned for the next part which is restoring purchases and yeah if you have any questions hit me up on Twitter and miles of tweet love to hear from you guys and have a good day hey what's up guys we're ready for our final video in neat and app purchase series and that is restoring your previous purchases now here you can see the the purchase is not completed yet now in order to get there I had to go into the app itself and go and delete this app which which I've already done so I'm not going to do it again and then go into settings and go to iTunes and App Store and go to the sandbox account and I signed out so see it's asking me to sign in now so now all that's left is to add a button to restore the in-app purchase that we've done and it's pretty simple so let's get to it right now and it's about as easy as just adding a button and linking up an IB action but the thing is Apple wants you to be very obvious about about where you put this because they don't want you to you know every time somebody updates their phone or gets a new phone they don't want to pay for the same in-app purchase things like that so be prepared for them to ask you where your in-app purchases are if you don't have any now so you can see here I've taken a navigation bar put it on the top and we set an exact amount for the height and pinned it to the top and sides so that auto layout can work its magic there we go the next thing we're going to do is we're gonna add a bar button item drag that onto the screen and rename this item restore and we're going to link this item up to our view controller by a control drag called restore pressed and in this restore press we just have to write one line of code and that is sk payment Q dot default dot restore completed transactions one more thing we have to add in the the payment queue function here we have to add another possibility so it's gonna be else--if put it up here else if transaction transaction state equals dot restored we also want to print restored and we want the purchased label dot text equal purchase restored alright let's run the demo and as you can see here purchased not completed yet we want to hit restore and we're going to sign in to an app store there we go purchase restored so that's how you do in-app purchases I hope you found this video useful and look forward to seeing you some more so thanks guys and ladies yeah I hope you got some value I just wanted to add one more thing if you look down in the description you can get the link to download the app that we just made so you can compare with yours if you have any more questions hit me up on Twitter at miles of tweets love to hear from you and thanks
Info
Channel: Michael Miles
Views: 6,580
Rating: undefined out of 5
Keywords: swift, swift 4, swift 5, xcode, iphone, mobile development, learn swift, in app purchases swift, in app purchase swift, in app purchases swift 4, in app purchases swift 5, in app purchase swift tutorial, in app purchase swift 4, in app purchase swift 5, iap swift tutorial, iap ios, iap swift 4, iap swift 5, iaps ios, in app purchases ios, in app purchase iphone tutorial, restore in app purchase tutorial, sandbox users swift, sandbox ios tutorial
Id: 8xO26l7CP7A
Channel Id: undefined
Length: 34min 22sec (2062 seconds)
Published: Mon Mar 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.