(iOS)应用内购买失败?

时间:2014-10-16 06:56:05

标签: 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;     [productsRequest start]; }

  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];
    

    }

  4. 这是有效的,在上面的NSLog语句中,它打印出我在iTunes connect中创建的内容。

1 个答案:

答案 0 :(得分:1)

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

所以对于其他有问题的人,试试这个: 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商店” - >然后转到测试设备上的设置,退出实际帐户,以便登录测试帐户!

这为我解决了。感谢您的帮助。下次我将确保首先记录错误。