我想处理在没有互联网连接的情况下请求应用内购买产品的情况。
在模拟器和设备中测试此情况时(通过关闭wi-fi)而不是接到request:didFailWithError:
的来电,我收到一个带有空产品数组的电话productsRequest:didReceiveResponse:
然后到requestDidFinish:
。
这是预期的行为吗?如果是这样,我怎么知道请求是否因连接问题而失败?如果没有,可能出现什么问题?
如果它有帮助,这就是我要求产品的方式:
- (void) requestProducts:(NSSet*)identifiers
{
_productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:identifiers];
_productsRequest.delegate = self;
[_productsRequest start];
}
我正在使用iOS 6.
答案 0 :(得分:1)
我不知道它的预期行为是否因为文档在这个问题上有点稀疏。但我总是自己做检查,所以我可以向用户提供很好的错误消息,因为似乎有一半时间StoreKit错误非常不明显。这是我在最近的一个项目中使用的一些代码。
我有自己的storeManager委托来简化调用和继承但是应该很清楚发生了什么。
#pragma mark - Purchase Methods
- (void)purchaseProduct:(SKProduct *)product
{
// Check Internet
if ([self checkInternetConnectionAndAlertUser:YES]) {
// Check Restrictions
if ([self checkRestrictionsAndAlertUser:YES]) {
// Check Products
if ([_products containsObject:product]) {
// Purchase the product
[[SKPaymentQueue defaultQueue] addPayment:[SKPayment paymentWithProduct:product]];
} else {
[[[UIAlertView alloc] initWithTitle:@"Error" message:@"Sorry, we couldn't find that product." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
[self.delegate purchaseDidFailWithError:[NSError errorWithDomain:@"SDInAppPurchaseManager" code:404 userInfo:@{ NSLocalizedDescriptionKey : @"Product not found." }]];
}
} else {
// Not allowed to make purchase
[self.delegate requestdidFailWithError:[NSError errorWithDomain:@"SDInAppPurchaseManager" code:500 userInfo:@{ NSLocalizedDescriptionKey : @"Not authorized to make purchases." }]];
}
} else {
// No Internet
[self.delegate requestdidFailWithError:[NSError errorWithDomain:@"SDInAppPurchaseManager" code:300 userInfo:@{ NSLocalizedDescriptionKey : @"No internet connection." }]];
}
}
#pragma mark - Checks
- (BOOL)checkInternetConnectionAndAlertUser:(BOOL)alert
{
if ([[SDDataManager dataManager] internetConnection]) {
return YES;
} else {
// Alert the user if necessary.
if (alert) {
[[[UIAlertView alloc] initWithTitle:@"No Connection" message:@"You don't appear to be connected to the internet. Please check your connection and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
return NO;
}
}
- (BOOL)checkRestrictionsAndAlertUser:(BOOL)alert
{
if ([SKPaymentQueue canMakePayments]) {
return YES;
} else {
// Alert the user if necessary.
if (alert) {
[[[UIAlertView alloc] initWithTitle:@"Purchases Disabled" message:@"In App Purchasing is disabled for your device or account. Please check your settings and try again." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
}
return NO;
}
}