我有一个ViewController,用户可以在其中购买应用内商品。他们按下按钮进行购买,但是一旦购买完成,我希望按钮隐藏,我想显示thankyou.text = @"Thanks for purchasing";
以下是我的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];
}
感谢任何帮助:)
答案 0 :(得分:1)
您用来执行IAP(RageIAPHelper)的Singleton来自本教程:
iOS 6教程中的应用内购买简介 作者Ray Wenderlich http://www.raywenderlich.com/21081/introduction-to-in-app-purchases-in-ios-6-tutorial
我建议你通过那篇文章阅读有关如何使用它的详细信息。
如果您通过源代码挖掘,您将在IAPHelper.m类中看到此代码段:
- (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;
}
};
}
buyProduct
方法启动购买,并在App Store响应时调用paymentQueue:updatedTransactions
方法。了解这两件事后,您可以做出相应的回应。例如,您可以创建IAPHelper
的子类,实现自己的paymentQueue:updatedTransaction
方法并让它按照您的意愿执行。
答案 1 :(得分:0)
您正在为界面的按钮创建一个属性(因此您可以从整个班级到达它):
@property (nonatomic, strong) UIButton * buyButton;
然后:
self.buyButton = [[UIButton alloc] initWithFrame:CGRectMake(-1, 310, 320, 60)];
并且当事务完成时(我假设你有一个处理事务状态的函数):
_buyButton.hidden = YES;
答案 2 :(得分:0)
这是我在我的应用中执行此操作的方式。您可以更改此代码purchaseMyAppSuccessful并在购买成功后执行您需要的任何操作
- (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];
}