应用内购买在尝试购买时崩溃了 [英] In-App Purchase Is Crashing When Trying to Buy

查看:63
本文介绍了应用内购买在尝试购买时崩溃了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过iAP购买时,应用程序崩溃并以Xcode显示.我是从某人那里购买此代码的,但是没有人抱怨在评论页面上iAP出现错误,我可以发誓经过测试,并且之前效果很好.我什至尝试过该项目,但它仍然崩溃.我也是编码的初学者,所以我可以做一些基本的事情,但请特别注意我.我认为这与规定配置文件或证书无关,这与代码中的权利无关.另外它已经有一段时间了,听起来可能很愚蠢,但是是否需要在iTunes中配置所有内容并使用相同的捆绑包ID和iAP信息才能正常工作?香港专业教育学院尝试了一切设置,它仍然无法正常工作.无论如何,代码不应崩溃.请看一看,我将发布屏幕截图和代码.

When trying to purchase with the iAP the app crashes and shows in Xcode. I bought this code from someone but nobody else complained of an error with iAP on the comments page and I could swear I tested and it worked fine before. I even tried the project fresh and it still is crashing. I'm only a beginner at coding as well so I can do basic things but please be specific Id appreciate it. I don't think it has anything to do with provisions profiles or certificates cause its right in the code. Also its been a while and might sound dumb, but does everything need to be configured in iTunes connect with the same bundle ID and iAP info to work? Ive tried it with everything setup and it still don't work. Regardless, code shouldn't crash. Please take a look, I'll post the screenshots and code.

错误屏幕

我的AppController.mm中与iAP有关的大多数代码都在这里:(很抱歉,不确定为什么将它怪异地以代码格式拼写了...我只是复制并粘贴了.所以我只想放置错误部分)

Most the code from my AppController.mm that has to do with iAP is Here: (Sorry, not sure why it is piecing it weird in code format...I just copied and pasted. So I'm only going to put the error parts)

    - (void) openRestore:(NSObject*)prms
{

    [[SKPaymentQueue defaultQueue]restoreCompletedTransactions];

}



- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    NSArray *myProduct = response.products;
    NSLog(@"%@",[[myProduct objectAtIndex:0] productIdentifier]);

    SKPayment *newPayment = [SKPayment paymentWithProduct:[myProduct objectAtIndex:0]];
    [[SKPaymentQueue defaultQueue] addPayment:newPayment];


}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                break;
        }
    }
}

推荐答案

根据您发布的屏幕截图,会发生以下情况:您的应用向服务器请求了产品列表.服务器通过说没有产品(即空数组)来响应.然后,该应用尝试将有关阵列中第一个产品的信息打印到控制台(即索引为"0"的产品).由于空数组中没有索引0,它会因索引超出范围异常而崩溃.

According to the screenshot you posted, the following happens: your app requests a list of products from the server. The server responds by saying that there are no products (i.e. an empty array). The app then tries to print info about the first product in the array to the console (i.e. the product at index "0"). Since there is no index 0 in an empty array, it crashes with an index out of bounds exception.

因为您的代码强烈期望总是至少有一个产品退货(这似乎不是一个很好的设计选择,但这是另一点),所以很可能是直接原因发生此特定崩溃的原因是未为此应用正确设置iAP.您的iAP处理代码看不到预期的内容.

Because your code is strongly expecting that there is always at least one product returned (which does not seem like a great design choice, but that's another point), it seems very likely that the immediate cause behind this particular crash is that iAPs are not set up correctly for this app. Your iAP handling code is not seeing what it is expecting to see.

我建议参考

I suggest referring to documentation how to create and test In-App Purchase Products for you app.

编辑(以在评论中回答Zach的请求):

好吧,让应用程序在这个特定位置不会崩溃很容易,但是就像我说的那样,如果应用程序内购买设置不正确,这将无法神奇地使应用程序内购买本身正常工作.

Well, making the app not crash in this particular spot is easy, but like I said, that's not going to magically make the In-App Purchase itself work if it's not set up correctly.

为了防止您发布的代码崩溃,您可以添加一个明确的检查,以确保产品数组不为空,像这样:

In order to prevent the crash in the code you posted, you can add an explicit check that the array of products is not empty, like so:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];

    NSArray *myProduct = response.products;

    if (myProduct.count > 0) { 
        NSLog(@"%@",[[myProduct objectAtIndex:0] productIdentifier]);

        SKPayment *newPayment = [SKPayment paymentWithProduct:[myProduct objectAtIndex:0]];
        [[SKPaymentQueue defaultQueue] addPayment:newPayment];
    }
}

这篇关于应用内购买在尝试购买时崩溃了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆