应用内购买产品 ID 更新一个落后
Posted
技术标签:
【中文标题】应用内购买产品 ID 更新一个落后【英文标题】:In-App Purchase Product ID Updates One Behind 【发布时间】:2015-10-14 16:50:20 【问题描述】:我对我的应用内购买设置中的一个奇怪故障有一个简短的问题:
我的ViewController
有 2 个按钮:A 和 B。当用户单击这些按钮之一时,我将我的 productID 设置为所选项目。然后我有一个 Buy 按钮,用户应该点击该按钮来购买该商品。我遇到的问题是我似乎无法让SKProductRequest
使用正确的项目启动productsRequest
方法。
换句话说,对 Apple 的 productRequest 总是落后于正确的产品 1。这是我实现的代码:
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 语句,但没有任何效果。
如果有人可以提出解决方案或尝试提供帮助,我将不胜感激。这几天我一直在努力解决这个问题。谢谢!
仅供参考:这是正在发生的事情的日志(我在这里为每一行手动添加了 cmets,让您知道用户为获取该日志而不是完整日期所做的工作)
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。请帮忙!
【问题讨论】:
为什么将产品、产品请求等保留为属性?这似乎是一个公开的错误邀请(比如落后 1)!将 request 保留为属性尤其让我担心。 (我知道您可能需要将产品交给视图控制器。) 【参考方案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);
【讨论】:
好吧,我不觉得很笨哈哈。感谢您的帮助 - 我认为这很简单,但我完全忽略了这一点。 并尽量避免在不必要时使用属性。您放入请求的产品将从响应中返回;你没有也不应该坚持下去。当然没有理由坚持这个请求。以上是关于应用内购买产品 ID 更新一个落后的主要内容,如果未能解决你的问题,请参考以下文章