【发布时间】:2014-12-11 10:39:00
【问题描述】:
我正在尝试执行应用内购买,但失败了,我不知道为什么。奇怪的是,它可以很好地获取 SKProduct 并且可以打印所有详细信息,但是当我真正尝试购买它时,它就失败了。
现在,因为我知道产品被提取并记录到控制台,所以问题不在于那个。问题出在我尝试实际购买获取的产品时,所以这里是代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
// Setup notifaction to handle the successfull payment
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(succeedPayment)
name:@"kInAppPurchaseManagerTransactionSucceededNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(failedPayment)
name:@"kInAppPurchaseManagerTransactionFailedNotification"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(cancelPayment)
name:@"userCancel"
object:nil];
}
- (IBAction)purchase10Crystals:(id)sender {
InAppPurchaseManager *p = [InAppPurchaseManager IAPManager];
if ([p canMakePurchases]) {
[p purchaseGoldCoins:10];
NSLog(@"Buying 10 crystals");
}
}
所以我让视图控制器收听有关付款的通知,并实际进行付款。以下是 InAppPurchaseManager 内部发生的情况:
SKPayment *payment = [SKPayment paymentWithProduct:gold10];
[[SKPaymentQueue defaultQueue] addPayment:payment];
现在购买已发送到商店。经理等待响应,这些是选项:
//
// removes the transaction from the queue and posts a notification with the transaction result
//
- (void)finishTransaction:(SKPaymentTransaction *)transaction wasSuccessful:(BOOL)wasSuccessful
{
// remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, @"transaction" , nil];
if (wasSuccessful)
{
// send out a notification that we’ve finished the transaction
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionSucceededNotification object:self userInfo:userInfo];
NSLog(@"Success transaction!");
}
else
{
// send out a notification for the failed transaction
[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerTransactionFailedNotification object:self userInfo:userInfo];
}
}
//
// called when the transaction was successful
//
- (void)completeTransaction:(SKPaymentTransaction *)transaction
{
[self recordTransaction:transaction];
[self provideContent:transaction.payment.productIdentifier];
[self finishTransaction:transaction wasSuccessful:YES];
}
//
// called when a transaction has been restored and and successfully completed
//
- (void)restoreTransaction:(SKPaymentTransaction *)transaction
{
[self recordTransaction:transaction.originalTransaction];
[self provideContent:transaction.originalTransaction.payment.productIdentifier];
[self finishTransaction:transaction wasSuccessful:YES];
}
//
// called when a transaction has failed
//
- (void)failedTransaction:(SKPaymentTransaction *)transaction
{
if (transaction.error.code != SKErrorPaymentCancelled)
{
// error!
NSLog(@"Failed: %li", (long)transaction.error);
[self finishTransaction:transaction wasSuccessful:NO];
}
else
{
// this is fine, the user just cancelled, so don’t notify
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[[NSNotificationCenter defaultCenter] postNotificationName:@"userCancel" object:self userInfo:nil];
}
}
被调用的方法是failedTransaction。我不知道为什么。沙盒帐户正在运行,因为它可以在游戏中心进行测试。该产品被提取是因为它全部打印出来。但是当我尝试购买时,它只是失败了,并且没有告诉我为什么。被注销的代码只是很多数字。
希望任何人都可以帮助我。 最好的祝福 /JBJ
编辑 这就是我获取 SKProducts 的方式。
-
首先我调用它来加载它
- (void)loadStore{ // 如果上次应用程序打开时被中断,则重新开始购买 [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
// get the product description (defined in early sections) [self request10GoldData];}
-
从商店获取产品
-(void) request10GoldData{ NSSet *productIdentifiers = [NSSet setWithObject:@"10crystals" ]; productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers]; productsRequest.delegate = self; [产品请求开始]; }
-
打印出来验证它是否被提取
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{ // 创建一个新的 SKProduct,识别它是哪个产品,并添加到适当的实例变量中
NSArray *products = response.products; SKProduct *theProduct = [products count] == 1 ? [products objectAtIndex:0] : nil; if (theProduct) { // Identify the product and assign to instance variable SKProducts if ([theProduct.productIdentifier isEqualToString:@"10crystals"]) { gold10 = theProduct; } // Debugging NSLog(@"Product title: %@" , theProduct.localizedTitle); NSLog(@"Product description: %@" , theProduct.localizedDescription); NSLog(@"Product price: %@" , theProduct.price); NSLog(@"Product id: %@" , theProduct.productIdentifier); } for (NSString *invalidProductId in response.invalidProductIdentifiers) { NSLog(@"Invalid product id: %@" , invalidProductId); } [[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];}
这行得通,在上面的 NSLog 语句中,它准确地打印出我在 iTunes 连接中创建的内容。
【问题讨论】:
-
您是否已将产品 ID 添加到您在 iTunes Connect 网站上的应用程序中?
-
是的,我有,它可以毫无问题地获取数据。它只是无法完成交易。
-
请告诉我,您是如何将它们添加到您的应用程序中的。因为有两个步骤:1. 定义产品和 2. 添加到您的应用程序。我假设您已经完成了这两个基本步骤,对吧?
-
我编辑了帖子以展示我如何获取产品。它可以使用 NSLog 打印出产品信息,所以我知道它获取了正确的信息。这就是让我难过的地方。因为它似乎在工作,并且可以识别要获取的产品,但无法完成购买。
-
我把范围缩小到它说“无法连接到 iTunes Store”??
标签: ios xcode in-app-purchase