Reputation: 979
I have a ViewController where the user makes the purchase for their in-app product. They press a button to make the purchase, however once the purchase is complete, I want the button to hide and I want to show thankyou.text = @"Thanks for purchasing";
Here's the code in my viewcontroller:
#import "SponsorViewController.h"
#import "NJKWebViewProgressView.h"
#import "RageIAPHelper.h"
#import <StoreKit/StoreKit.h>
@interface SponsorViewController ()
{
NSMutableArray *_objects;
NSArray *_products;
NSNumberFormatter * _priceFormatter;
}
@end
@implementation SponsorViewController
- (NSString *)publisherIdForAdSdkBannerView:(AdSdkBannerView *)banner {
return @"e0616d4190bff65279ed5c20de1b5653";
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[RageIAPHelper sharedInstance];
_products = nil;
[[RageIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
if (success) {
_products = products;
}
}];
// Price New
SKProduct * product = (SKProduct *) [_products objectAtIndex:0];
([[RageIAPHelper sharedInstance] productPurchased:product.productIdentifier]);
// Unlock your features code comes here
UIButton *buyButton = [[UIButton alloc] initWithFrame:CGRectMake(-1, 310, 320, 60)];
UIImage *btnImage = [UIImage imageNamed:@"upgrade-new.png"];
[buyButton setImage:btnImage forState:UIControlStateNormal];
[buyButton.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
[buyButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[buyButton.titleLabel setShadowColor:[UIColor colorWithWhite:0.1 alpha:1.0]];
[buyButton.titleLabel setShadowOffset:CGSizeMake(0, -1)];
[buyButton addTarget:self action:@selector(buyButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
buyButton.tag = 0;
[[self view] addSubview:buyButton];
UIButton *restoreBtn = [[UIButton alloc] initWithFrame:CGRectMake(-1, 370, 320, 60)];
UIImage *restoreImg = [UIImage imageNamed:@"restore.png"];
[restoreBtn setImage:restoreImg forState:UIControlStateNormal];
[restoreBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
[restoreBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[restoreBtn.titleLabel setShadowColor:[UIColor colorWithWhite:0.1 alpha:1.0]];
[restoreBtn.titleLabel setShadowOffset:CGSizeMake(0, -1)];
[restoreBtn addTarget:self action:@selector(restoreAction:) forControlEvents:UIControlEventTouchUpInside];
restoreBtn.tag = 0;
[[self view] addSubview:restoreBtn];
// Purchase Action End
[super viewDidLoad];
_priceFormatter = [[NSNumberFormatter alloc] init];
[_priceFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[_priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[_priceFormatter setLocale:product.priceLocale];
self.view.layer.shadowOpacity = 0.75f;
self.view.layer.shadowRadius = 10.0f;
self.view.layer.shadowColor = [UIColor blackColor].CGColor;
}
- (IBAction)dismissView:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (void)buyButtonTapped:(id)sender {
UIButton *buyButton = (UIButton *)sender;
SKProduct *product = [_products objectAtIndex:buyButton.tag];
// NSLog(@"Buying %@...", product.productIdentifier);
[[RageIAPHelper sharedInstance] buyProduct:product];
}
- (IBAction)restoreAction:(id)sender
{
[[RageIAPHelper sharedInstance] restoreCompletedTransactions];
}
Would appreciate any help with this :)
Upvotes: 0
Views: 249
Reputation: 27608
This is how I do it in my apps. you can change this code purchaseMyAppSuccessful and do whatever you need to after the purchase is successful
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
NSLog(@"About to purchase ...");
break;
case SKPaymentTransactionStatePurchased:
NSLog(@"Item is successfully purchased ...");
[self purchaseMyAppSuccessful];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
NSLog(@"Payment Restored ...");
[self purchaseMyAppRestored];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
NSLog(@"Payment Failed ...");
if(transaction.error.code != SKErrorPaymentCancelled)
{
NSLog(@"An error occured ...");
[self purchaseMyAppFailed];
}
else
{
NSLog(@"Payment was cancelled ...");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
default:
break;
}
}
}
- (IBAction) purchaseMyAppSuccessful
{
[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"MyAppWasPurchasedStr"];
[[NSUserDefaults standardUserDefaults] synchronize];
//Alert Show
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success"
message:@"You have successfully unlocked this app. Have fun!"
delegate:self cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
}
Upvotes: 0
Reputation: 4737
The Singleton you're using to do the IAP (RageIAPHelper) comes from this tutorial:
Introduction to In-App Purchases in iOS 6 Tutorial by Ray Wenderlich http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial
I would suggest you read thru that article for details on how exactly to use it.
If you dig thru the source, you'll see this snippet in the IAPHelper.m class:
- (void)buyProduct:(SKProduct *)product {
NSLog(@"Buying %@...", product.productIdentifier);
SKPayment * payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction * transaction in transactions) {
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
};
}
The buyProduct
method initiates the purchase, and the paymentQueue:updatedTransactions
method is called when the App Store responds. Knowing those two things, you can respond accordingly. For example, you might subclass IAPHelper
, implement your own paymentQueue:updatedTransaction
method and have it do what you wish.
Upvotes: 1
Reputation: 843
You are creating a property for your button to the interface (so you can reach it from you whole class):
@property (nonatomic, strong) UIButton * buyButton;
then:
self.buyButton = [[UIButton alloc] initWithFrame:CGRectMake(-1, 310, 320, 60)];
and when the transaction is completed (I assume you have a function somewhere what handles the transaction states):
_buyButton.hidden = YES;
Upvotes: 0