In App Purchases & Testing in Xcode 12 (Swift 5, 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 continue learning about in-app purchases and more specifically we're going to look at how you can set up with xcode 12 the ability to test your in-app purchases right in your simulator so um this is a huge huge huge advantage uh now that this is available uh prior it was quite annoying to test in-app purchases um you can simulate things like refunds and purchases and basically you can even test different um like localizations and the whole nine yards so we'll set up this and take a look at what goes into it so that said make sure you destroy the like button as always get excited hit subscribe if you're a returning viewer get xcode ready and let's talk about some more 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 dot 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 testing iaps once again make sure all of these values match for the language lifecycle and interface go ahead and continue and cool we'll save it to the desktop and first things first let's go ahead and pick a simulator here and just hit that run button to get it loaded and let me also expand my xcode window here and get rid of my antivirus pop-up that likes to make an appearance in every video and let's get into some in-app purchases so i've got a previous video on how to request in-app purchases from uh the app store but essentially what we'll do is we're gonna quickly stub out that code and we're more so gonna focus on how do you actually test them in a simulator since that's what's new here in xcode 12. so of course those of you that don't know how to fetch those products feel free to follow along but basically we're going to have an array here of product types for our table view models and we're going to use a table view to list out our products that we have fetched from the app store so this will be a basic ui table view and we're just going to register a basic table view cell with an id of cell and here we're going to add this as a sub view and set its delegate and data source we're going to want to conform to these protocols for the table view so ui table view delegate and ui table view data source are also going to want to conform to sk product request delegate and this is how we actually get the products back from the request we're going to make and let's go ahead and stub out the protocol functions so number of rows we're going to return models.count self row we're going to say let's sell equals a table view dequeuing a cell with the id we registered which is simply cell for the given index path and then we want to return a cell and let's just go ahead and say the text is a test to make sure it is showing up we're also going to want did select row at index path and this is how we're going to prompt the in-app purchase in this case of course in the debug fashion so we're going to say show purchase just like that and we also want the product set up so we're going to say products the first one is product request did receive response and all we're going to say is that the models now equals the response.product so basically whatever products we get back from the app store and we'll take a look at how to set that up in a moment and then we want to reload the table view just like that we also want to do this on the main thread the reason being we're doing a table view reload which is a ui operation so we should do that on the main thread and add self here and for those of you who i know are going to ask the way you do this multi-cursor thing is you hold option and drag and you can actually have multiple cursors if you want to write on two lines or delete on two lines at the same time so cool little pro tip for editing your code faster now let's see we also want to give the table view a frame we'll just say it's the entirety of the screen and we want to fetch products so i'm going to create a function for that and put it right down here and we're going to want to have let's say we have three products for the purposes of this video let's go ahead and put them in an enum and we'll see why in just a second the enum will have a raw value of a string and the enum will be case iterable and we're going to have remove ads unlock everything and unlock gems let's call this get gems and we're going to say it has a id of com.myapp.gems let's go ahead and paste this up here and just change these up go ahead and hit command b your app should be compiling if you go ahead and just hit command r there should be no crashing going on you should see an empty table view here with the cell separators let's go ahead and create that product fetch request so the way we create it is by simply initializing a xk product request and you initialize it with a set of product ids and we can say product rather as a set product dot all cases and we can say compact map the raw value and that's why we created it as a enum if we ever wanted to add more products we can simply add to this enum next up we want to set the delegate of the request where the response will be handled down here and then we want to lastly start the request and i'm also going to be printing out the account of products we get for testing purposes just like that so if you actually go ahead and hit command r you'll notice something a little strange and that strange thing is we don't see any print statement here but we are requesting three products and let's make sure we're calling fetch products which we are so the question becomes uh what gives right and the answer is uh before xcode 12 there was no way to test in-app purchases in a simulator and it was a major pain to actually go and you know run it on your device and do a bunch of setup so let's talk about story kit configuration files so we're going to go to file uh new and we want to find a configuration file for store kit and go ahead and create it and this box does not need to be checked and that gives you something like this and basically we can create three products local to our application and these are not basically anything the user will see strictly for testing purposes and use these to test so here once we hit the plus we see there's three types of products that are supported we're going to do three non-consumable products and the one thing we're going to copy from here is the id so let's see we've got remove ads everything and gems and you can use two fingers if you have a trackpad to swipe between the previous and ladder file so firstly we're going to go to each of these and paste in that bundle id we're going to give it a name this is just a reference name and you can also control the description and the display name that comes back in a bunch of different languages for testing we're going to stick with english and we're going to say remove ads removes all ads in the app let's go to the next one and i believe this one was something like gems gems let's go ahead and edit the english localization info and this is really great for those of you that are new to xcode this is such a big time saver that i can't stress it uh enough it so traditionally you would have to set up a bunch of things in app store connect and configure users to test your products and go through a bunch of hoops basically to get this even to be testable so being able to do this write an x code on the simulator is a major time saver so now that we have all of these added here if we go ahead and hit command r you'll still notice that the console actually doesn't print anything and there's a good reason for this the reason is we need to tell xcode that we should use this configuration file when we're testing and the way you do that is by editing the scheme so you want to select this drop down and hit edit scheme you want to come to the run option here and the options sub option here and there's a row titled store kit configuration it will be none by default but in the drop down you'll see it includes the new one we created so if you go ahead and uh just select that and if we take a look at our app right now we don't have any cells rendering i think in the view controller we're just using a text label text of uh test i believe yep so let's go ahead and hit command r not only should we see the print statement here we should see three cells here so we got three products back from the request i'm actually logging the looks like the products itself what i wanted to do was log the account but let's actually configure the cell with the info from our product so the product at the given position is in the models the index path dot row and we can say the text is the product dot uh localized this title i guess they call a title instead of display name and we can then say uh there's a colon here and next we want product dot localized description and then finally we of course want to show the user a price so we're going to say product and there's actually two price things on here you should be aware of the first thing is the price itself and the other thing is the price locale and that's things like that basically correlates to which country the user is coming from so the price or locale can be used to derive a currency symbol so i believe on the locale you can get the currency symbol directly just like that so let's actually go ahead and hit command r let's see what this looks like so cool so one issue is our table view cell is uh wrapping the text label let's go ahead and say number of lines is zero it looks like this is optional so let's go ahead and give it a default value of a dollar sign and if we run this again now we get more interesting so we see not only our display title we see that i spelled removes wrong but we see the description as well as a price and for those of you uh who missed it i didn't really call it out you can specify a dummy price here as well uh but when you click on the cell nothing happens right now so let's go ahead and hook that up so the way you actually prompt a purchase is by just requesting a payment so we're going to say let payment is a sk payment with a product and we're simply going to pull the product out of our models and then we want to add it to the transaction queue so we're going to say default and we want to add a payment and of course to observe different payments and transactions we want to add a observer so we'll do that in view did a load and we want to add a sk payment transaction observer and of course we want to conform to that protocol so it doesn't yell at us so sk payment transaction observer this one right here and i believe there's one required function to it and that is updated transactions and i'm not going to implement this uh since it's not really relevant to this video but let's go ahead and run and now you'll see when we click on one of these if you look at that we get our uh sheet down here and if you had an app icon it would show like right here um and before i hit this actually let's actually quickly implement this even though i said i wasn't going to to just portray something else i wanna show you guys uh so we're gonna do a for each over transactions and we're going to switch on the transaction type let's see we want to do transactions for each and we want to switch on dollar zero dot state actually transaction state it was is what it's called and i'm gonna wait for the error and just hit this and hit fix and it'll stub out all the cases for us and let's just add some print statements here just so you guys can see what uh what happens we go ahead and interact with um that bottom in our purchased sheet so go ahead and hit command r and first we see that we successfully fetched three products and i'm gonna go ahead and try to unlock everything and you see that we are now purchasing so this function was in fact called and if i select this we get you know the nice in-app purchase noise and we get it dismissing and generally i believe it should come into uh purchased looks like it didn't for whatever reason and i believe that reason actually is we need to say sk payment queue default and we want to finish the transaction in this case so let's go ahead and try that once more but point being that you can uh test everything related to uh you know your product uh you see actually now it came in said purchased but let's try that again so we're gonna do this purchasing purchased and let's see we have this alert if we hit that we actually see purchase gets called after the user dismisses that alert so yeah and the very last thing that i'll cover uh since this video is getting longer than i thought is i believe what you also can do is go to debug uh store kit manage transactions and if you open this up you'll actually see our two transactions in here for this app are listed here so if you wanted to simulate things like a refund of a purchase you can do that and you can also delete both of these again because you know in a proper app you should be saving what's been purchased and whatnot and if you delete these and run your app again you'll notice that we can purchase these again and there's no print statements here so you can go through the motions of testing you know a bunch of scenarios right in xcode with this approach so that all said that's all i've got for you guys today if you haven't hit that like button already make sure to do so for the youtube algorithm once again to recap really fast all we did is set up a storekit a table view and the important thing was we created this configuration.storkit file and set it up in our scheme here to allow us to test on the simulator so that all said hit subscribe if you haven't done that yet really appreciate it leave a comment with any questions i'll catch you guys in the next video
Info
Channel: iOS Academy
Views: 17,201
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, in app purchases swift 5, swift 5 in app purchases, swift 5 2020, swift in app purchases, Xcode 12, Xcode 12 in app purchases, iOS in app purchases, swiftUI in app purchases, swift 5 2020 store kit, store kit, swift 5 store kit, in app purchases swift, swift 5 purchases, store kit swift, iOS
Id: YPQG0adwioA
Channel Id: undefined
Length: 18min 25sec (1105 seconds)
Published: Tue Oct 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.