0

我想处理在没有互联网连接的情况下请求应用内购买产品的情况。

在模拟器和设备中测试这种情况时(通过关闭 wi-fi),我没有request:didFailWithError:收到对 的调用,而是收到一个productsRequest:didReceiveResponse:带有空产品数组的调用,然后是对requestDidFinish:

这是预期的行为吗?如果是这样,我如何知道请求是否由于连接问题而失败?如果没有,可能有什么问题?

如果有帮助,这就是我要求产品的方式:

- (void) requestProducts:(NSSet*)identifiers
{
    _productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:identifiers];
    _productsRequest.delegate = self;
    [_productsRequest start];
}

我正在使用 iOS 6。

4

1 回答 1

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;
    }
}
于 2012-12-06T13:47:31.553 回答