[英]In-App Purchases Crashing App With No Internet Connection
My In-App Purchases is works perfect but if I my iPhone is not on Wi-Fi and using cellular connection with slow signal or without signal I'm getting this message: 我的应用内购买功能非常完美,但是如果我的iPhone不在Wi-Fi上,并且使用蜂窝连接且信号较慢或没有信号,我会收到以下消息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid product identifier: (null)'
由于未捕获的异常“ NSInvalidArgumentException”而终止应用程序,原因:“无效的产品标识符:(空)”
Should I check Internet connection before enabling buy button? 在启用“ 购买”按钮之前,我应该检查互联网连接吗? I hope there is easier way.
我希望有更简单的方法。 All I need is to catch no connection error from
StoreKit.
我需要做的只是从
StoreKit.
捕获任何连接错误StoreKit.
Update 更新资料
For my In-App Purchases code I am using a little bit modified this guide from raywenderlich.com. 对于我的应用内购买代码,我正在对raywenderlich.com的本指南进行一些修改。 You also can download the test project.
您也可以下载测试项目。
My app is for iOS 8 and 7, and this tutorial is for iOS 6. Maybe it's a problem. 我的应用程序适用于iOS 8和7,本教程适用于iOS6。也许是个问题。
I only added (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
for catching error while restoring app and SKPaymentTransactionStateDeferred
case. 我只添加了
(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
用于在恢复应用程序和SKPaymentTransactionStateDeferred
情况时捕获错误。
Release the delegate in the didFailWithError
method to prevent a crash: 在
didFailWithError
方法中释放委托以防止崩溃:
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"Failed to load list of products.");
_productsRequest = nil;
_completionHandler(NO, nil);
_completionHandler = nil;
request.delegate = nil; // Release the delegate
}
To prevent this crash, I need to add some code inside this method which will prevent user to make a purchase. 为防止崩溃,我需要在此方法内添加一些代码,以防止用户进行购买。
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"Failed to load list of products.");
_productsRequest = nil;
_completionHandler(NO, nil);
_completionHandler = nil;
}
This works perfect while testing in Airplane mode. 在飞行模式下测试时,这非常完美。 We need one more error handler in case Airplane mode will be turned off after showing In-App Purchases screen and turned on before tapping buy.
我们还需要一个错误处理程序,以防在显示“应用内购买”屏幕后关闭飞行模式并在点击“ 购买”之前打开飞行模式。 And if the signal disappear during purchase.
并且如果信号在购买过程中消失。
We should handle SKPaymentTransactionStateFailed
case: 我们应该处理
SKPaymentTransactionStateFailed
情况:
- (void)failedTransaction:(SKPaymentTransaction *)transaction
{
// failedTransaction
if (transaction.error.code != SKErrorPaymentCancelled)
{
NSLog(@"Transaction error: %@", transaction.error.localizedDescription);
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
// Update UI and / or post error message
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
} }
You absolutly don't want to show alert when user taps Cancel. 当用户点击“ 取消”时,您绝对不希望显示警报。
I am also blocking buy button if following success BOOL
is not YES.
如果成功之后
BOOL
不是YES.
我也将阻止购买按钮YES.
requestProductsWithCompletionHandler:^(BOOL success, NSArray *products)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.