hpique
hpique

Reputation: 120324

SKProductRequest doesn't fail without connection

I'd would like to handle the case in which in-app purchase products are requested without an internet connection.

When testing this case both in the simulator and a device (by turning off the wi-fi), instead of receiving a call to request:didFailWithError:, I receive a call productsRequest:didReceiveResponse: with an empty products array and then to requestDidFinish:.

Is this the expected behavior? If so, how can I know if the request failed due to a connection issue? If not, what might be wrong?

In case it helps, this is how I request the products:

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

I'm using iOS 6.

Upvotes: 0

Views: 263

Answers (1)

Ryan Poolos
Ryan Poolos

Reputation: 18551

I don't know if its expected behavior because the docs are a little sparse on the subject. But I always do the checks myself so I can provide nice error messages to the user because it seems half the time the StoreKit Errors are very nondescript. Here is a bit of code I used in a recent project.

I have my own storeManager delegate to simplify calls and inheritance but it should be pretty clear whats happening.

#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;
    }
}

Upvotes: 1

Related Questions