我在应用内购买设置中遇到了一个奇怪的故障:
我的ViewController
有2个按钮: A 和 B 。当用户单击其中一个按钮时,我将productID设置为所选项目。然后,我有一个购买按钮,用户应该点击该按钮购买该项目。我遇到的问题是我似乎无法让SKProductRequest
使用正确的项目启动productsRequest
方法。
换句话说,对Apple的productRequest总是落后于正确的产品。这是我实施的代码:
的 ViewController.h
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface ViewController : UIViewController <SKPaymentTransactionObserver, SKProductsRequestDelegate>
/// In app purchase crap
@property (strong, nonatomic) SKProduct *product;
@property (strong, nonatomic) SKProductsRequest *request;
@property (retain, nonatomic) NSString *productID;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
int start;
- (void)viewDidLoad {
[super viewDidLoad];
// Attempt to get request aligned with item selected.. :\
start = YES;
_productID = @"com.example.IAP.itemA";
[self Buy:self];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)Buy:(id)sender {
// Check for user to be able to make payment
if ([SKPaymentQueue canMakePayments]) {
_request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:_productID]];
_request.delegate = self;
[_request start];
} else {
NSLog(@"Please enable In App Purchasing in your settings");
}
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSArray *products = response.products;
if (products.count != 0) {
NSLog(@"Products available: %@", _product.localizedTitle);
_product = products[0];
if (start != YES) {
[self beginPaymentQueue];
}
else {
start = NO;
}
} else {
NSLog(@"Products not found.");
}
products = response.invalidProductIdentifiers;
for (SKProduct *product in products) {
NSLog(@"Product not found: %@", product);
}
}
- (void)beginPaymentQueue {
SKPayment *payment = [SKPayment paymentWithProduct:_product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased: { [self UnlockPurchase];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break; }
case SKPaymentTransactionStateFailed: { NSLog(@"Transaction Failed");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break; }
default:
break;
}
}
}
- (void)UnlockPurchase {
NSLog(@"Purchase Successful!");
}
// Item Buttons
- (IBAction)buttonA:(id)sender {
_productID = @"com.example.IAP.itemA";
}
- (IBAction)buttonB:(id)sender {
_productID = @"com.example.IAP.itemB";
}
不确定该怎么做...我试过在viewDidLoad
方法中调用CanPurchase方法,我甚至在productID的方法中尝试了switch语句,但没有任何效果。
如果有人可以提出解决方案或尝试帮助我会非常感激。我一直在努力解决这个问题。谢谢!
仅供参考:这是一个正在发生的事情的日志(我在这里为每一行手动添加了评论,让您知道用户为了获取该日志而取代完整日期所做的工作)
app loads 12:14:49.488 IAP[4928:370136] Products available: (null)
click B then buy 12:14:52.053 IAP[4928:370136] Products available: IAP A
cancel iap 12:15:01.086 IAP[4928:370136] Transaction Failed
click B then buy 12:15:02.622 IAP[4928:370136] Products available: IAP B
cancel iap 12:15:04.256 IAP[4928:370136] Transaction Failed
click A then buy 12:15:02.622 IAP[4928:370136] Products available: IAP B
cancel iap 12:15:04.256 IAP[4928:370136] Transaction Failed
click A then buy 12:15:02.622 IAP[4928:370136] Products available: IAP A
cancel iap 12:15:04.256 IAP[4928:370136] Transaction Failed
该循环继续,使用选择的IAP,productRequest始终落后于1。请帮忙!
答案 0 :(得分:1)
看起来你只是在这里迷惑自己:
NSArray *products = response.products;
if (products.count != 0) {
NSLog(@"Products available: %@", _product.localizedTitle);
_product = products[0];
您正在从之前存储的产品中获取本地化标题。这就是你落后的原因。相反,从响应返回给您的产品中获取产品及其标题:
NSArray *products = response.products;
if (products.count != 0) {
SKProduct* product = products[0];
NSLog(@"Products available: %@", product.localizedTitle);