Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 6    Views: 838

Absentia
buzztouch Evangelist
Profile
Posts: 960
Reg: Oct 20, 2011
Alaska
20,600
02/24/14 12:35 PM (10 years ago)

iOS In-App purchases made easy

Note - this tutorial is meant for BT users who are familiar with the BT project and aren't afraid to hack around a little bit. But, even if you aren't one of those people, you should still read through it and give it a shot..it might make more sense than you think Thought you guys might be interested in checking this out - https://github.com/robotmedia/RMStore - it lets you activate in-app purchases with just a couple lines of code. I've tested it out and it works perfect..I never would of thought something like this existed. Note that this doesn't discount the easy-in-app purchase plugin..that is still a great way of unlocking screens and is probably still the way to go if that is what you're trying to do. This method works great if you want to disable ads, purchase in-game money, unlock certain features, etc. so, say you have an in-app purchase in your app with the id "myPurchase" First you would drop the .h and .m files into your project Then you would load the purchase in the viewDidLoad method of whatever screen the IAP will be available on...like this - //Load the IAP in the background//////////////////////// NSSet *products = [NSSet setWithArray:@[@"myPurchase"]]; [[RMStore defaultStore] requestProducts:products success:^(NSArray *products, NSArray *invalidProductIdentifiers) { //this code runs if the IAP loads successfully NSLog(@"Products loaded"); } failure:^(NSError *error) { //this code runs if the IAP fails to load NSLog(@"Something went wrong"); }]; That block of code loads the IAP and gets it ready for purchase - the next step is to have the block of code that runs the purchase. So, on your screen you might have a button that will activate the IAP. The way I did it was to have the button set off an Alert View with info and options for the IAP. So when the user taps the button, this code will run - - (IBAction)iapTapped:(UIButton *)sender { alert = [[UIAlertView alloc] initWithTitle:@"Buy this awesome in-app purchase" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Buy", @"Restore", nil]; [alert show]; } Then you need to have the method that will check for what button the user tapped after viewing the alert - "Buy", "Restore", or "Cancel" - - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { //Cancel if(buttonIndex == 0){ [BT_debugger showIt:self theMessage:@"cancelled IAP"]; } //Purchase IAP if(buttonIndex == 1){ [self activateIAP]; } //Restore IAP if(buttonIndex == 2){ [self restoreIap]; } } As you can see, the above didDismissWithButtonIndex will call either [self activateIap] or [self restoreIap], depending on what the user selects..so you need to have those methods in your screens code as well (you can obviously rename those methods to whatever you want) . So if the user taps "Buy", the activateIap method will run, which looks like this - -(void)activateIAP{ [[RMStore defaultStore] addPayment:@"myPurchase" success:^(SKPaymentTransaction *transaction) { //This is where you will run the code for whatever your IAP is unlocking. NSLog(@"Product purchased"); } failure:^(SKPaymentTransaction *transaction, NSError *error) { //This code will run if the purchases fails NSLog(@"Something went wrong"); }]; } In the above method, you can see where I put the note about running the code for whatever you want the IAP to unlock. This is where NSUserDefaults come in handy - they save information that the app will remember permanently. So if you don't want to show ads, you might have this line of code right below where I put that note - [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"noAds"] then every time you call an ad in your project, you would check for the IAP first - //if the iap has been purchased, don't show an ad... if([[NSUserDefaults standardUserDefaults] boolForKey:@"noAds"] == YES){ [BT_debugger showIt:self theMessage:@"not showing ad"]; }else{ //...otherwise, show an ad [self showInterstitial]; } That's just an example...I know this probably isn't sounding easy to everyone reading it. You have to be a little creative to do IAP's this way, but it can definitely pay off. If you have ever tried coding In-App purchases on your own by following one of those huge tutorials, then you know how easy this is in comparison The final step is to have the restore method in your project as well. I won't go over because it behaves exactly the same way as the purchase method. Apple requires you to have an option for users to restore IAPs if they have already purchased them. Here is what this method should look like - - (void)restoreIap{ [[RMStore defaultStore] restoreTransactionsOnSuccess:^{ //This is where you will run the code for whatever your IAP is restoring. NSLog(@"Transactions restored"); } failure:^(NSError *error) { //This code will run if the restore fails or if the user has nothing to restore NSLog(@"Something went wrong"); }]; That's all there is to it. The documentation on the GitHub page is pretty thorough as well, so be sure to check that out. Let me know how it goes
 
Uelsimon
Lost but trying
Profile
Posts: 272
Reg: Mar 25, 2012
NYC
4,470
like
02/24/14 12:40 PM (10 years ago)
sweet share. -€
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
02/24/14 02:06 PM (10 years ago)
Thanks!
 
nadthevlad
Code is Art
Profile
Posts: 1025
Reg: Jun 07, 2012
Denver
21,850
like
02/24/14 03:16 PM (10 years ago)
Thanks for that.
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
02/24/14 06:34 PM (10 years ago)
Nice share -- you are good at explaining things. It takes effort to figure out these things. Appreciate the hard work and the share! :-) -- Niraj
 
SheriDee
Code is Art
Profile
Posts: 1094
Reg: Sep 23, 2011
location unknow...
22,840
like
02/25/14 01:17 AM (10 years ago)
:) Awesome!
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
03/16/14 10:02 AM (10 years ago)
Duplicate
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.