ios - (iOS) 内购失败?

标签 ios xcode in-app-purchase

我正在尝试执行应用内购买,但失败了,我也不知道为什么。奇怪的是,它可以很好地获取 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");
    }
}

所以我让 View Controller 收听有关付款的通知,并实际进行付款。这是 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 的方式。

  1. 首先我调用它来加载它

    - (void)loadStore
    

    { //如果上次应用程序打开时中断,则重新开始任何购买 [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    // get the product description (defined in early sections)
    [self request10GoldData];
    

  2. 从商店中获取产品

    -(void) request10GoldData
    

    { NSSet *productIdentifiers = [NSSet setWithObject:@"10crystals"]; productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers]; productsRequest.delegate = self; [产品请求开始];

  3. 打印出来以验证它是否已被提取

    - (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 connect 中创建的内容。

最佳答案

好吧,真是无赖。我发现这就像在测试这个时注销我的实际帐户一样简单 -.-' 我没有发现其他人有这个问题,因为我没有打印出交易错误。

所以对于其他有问题的人,试试这个: 1. 像这样记录错误描述:

    - (void)failedTransaction:(SKPaymentTransaction *)transaction
{
    // Log the error
    NSLog(@"%@", [transaction.error localizedDescription]);

    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        // 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];
    }
}

如果错误显示“无法连接到 iTunes 商店”-> 然后转到您的测试设备上的设置,然后退出您的实际帐户,以便您可以登录到您的测试帐户!

这为我解决了。感谢您提供帮助。下次我一定会先记录错误。

关于ios - (iOS) 内购失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26398026/

相关文章:

iphone - 应用商店的演示视频?

iphone - 后备语言似乎不起作用(iOS/iPhone)

ios - 用于使用 expo SDK react native ios 应用程序的 Info.plist 文件

javascript - 我的 CocoaTouch 应用程序如何响应用户在 UIWebView 中按下 HTML 输入按钮?

ios - Apache2 服务器视频流与移动设备和 PC 的兼容性

iphone - 在表格 View 中触摸单元格后标签栏消失

iphone - 如何在没有服务器的情况下使用iPhone自动续订

Android 应用内计费错误

ios - 我可以获得虚拟 iOS 钥匙串(keychain)访问吗?用于构建 phonegap 验证

ios - 类别冲突 : instance method in category from conflicts with same method from another category