ios in-app purchase related crash

2019-09-18 17:18发布

问题:

I have a non consumable product to remove ads from my app, via in-app purchase. It works well sometimes, and if I close the app and open again, my code for detecting in NSDefaults if PRO version was purchased works fine. The things is, upon purchase, I want to remove the "upgrade to PRO" button from the tab bar and this code crashes the app. Here's the controller that handles in-app purchase:

#import "RemoveAdsViewController.h"
#import <StoreKit/StoreKit.h>
#import "Flurry.h"
#define kRemoveAdsProductIdentifier @"AiutoPro"

@interface RemoveAdsViewController ()

@end

@implementation RemoveAdsViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = NSLocalizedStringFromTable(@"aiutopro", @"i18n", @"");
    self.buyLabel.text = NSLocalizedStringFromTable(@"buyText", @"i18n", @"");
    self.recoverLabel.text = NSLocalizedStringFromTable(@"recoverText", @"i18n", @"");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma Actions
- (IBAction)buyNow:(id)sender {
    NSLog(@"User requests to remove ads");

    if ([SKPaymentQueue canMakePayments]) {
        NSLog(@"User can make payments");

        SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
        productsRequest.delegate = self;
        [productsRequest start];
    }
    else {
        NSLog(@"User cannot make payments due to parental controls");
    }
}

- (IBAction)alreadyBought:(id)sender {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    SKProduct *validProduct = nil;
    NSUInteger count = [response.products count];

    if ([response.invalidProductIdentifiers count] > 0) {
        NSLog(@"Invalid Product Identifier");
    }

    if (count > 0) {
        validProduct = [response.products objectAtIndex:0];
        NSLog(@"Products Available!");
        [self purchase:validProduct];
    }
    else {
        NSLog(@"The product id was valid, but it has no products available");
    }
}

- (void)purchase:(SKProduct *)product {
    SKPayment *payment = [SKPayment paymentWithProduct:product];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
    NSLog(@"received restored transactions: %lu", (unsigned long)queue.transactions.count);
    for (SKPaymentTransaction *transaction in queue.transactions) {
        if (SKPaymentTransactionStateRestored) {
            NSLog(@"Transaction state -> Restored");
            [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
            [self doRemoveAds];
            break;
        }
    }
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchasing:
                NSLog(@"Transaction state -> Purchasing");
                break;

            case SKPaymentTransactionStatePurchased:
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                [self doRemoveAds];
                [Flurry logEvent:@"Purchase"];
                NSLog(@"Transaction state -> Purchased");
                break;

            case SKPaymentTransactionStateRestored:
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                NSLog(@"Transaction state -> Restored");
                [self doRemoveAds];
                [Flurry logEvent:@"Restore"];
                break;

            case SKPaymentTransactionStateFailed:
                if (transaction.error.code != SKErrorPaymentCancelled) {
                    NSLog(@"Transaction state -> Cancelled");
                }
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                break;
        }
    }
}

- (void)doRemoveAds {
    // Persist to UserDefaults
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"areAdsRemoved"];
    [[NSUserDefaults standardUserDefaults] synchronize];

    // Remove upgrade tabbar button
    NSMutableArray* newArray = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
    [newArray removeLastObject];

    [self.tabBarController setViewControllers:newArray animated:YES];
}

@end

The last 3 lines of doRemoveAds method are the ones crashing the app, with the following error:

Thread 0 Crashed:
0   libobjc.A.dylib                 0x000000019489c1d0 objc_msgSend + 16
1   CoreFoundation                  0x00000001882815dc CFArrayApplyFunction + 64
2   StoreKit                        0x000000018b2b9474 -[SKPaymentQueue _notifyObserversAboutRemovals:] + 152
3   StoreKit                        0x000000018b2ba314 -[SKPaymentQueue _removePaymentsForMessage:] + 680
4   StoreKit                        0x000000018b2b8f28 __44-[SKPaymentQueue _handleMessage:connection:]_block_invoke + 156
5   libdispatch.dylib               0x0000000194e64010 _dispatch_call_block_and_release + 20
6   libdispatch.dylib               0x0000000194e63fd0 _dispatch_client_callout + 12
7   libdispatch.dylib               0x0000000194e671d8 _dispatch_main_queue_callback_4CF + 332
8   CoreFoundation                  0x0000000188342c28 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 8
9   CoreFoundation                  0x0000000188340f68 __CFRunLoopRun + 1448
10  CoreFoundation                  0x0000000188281c1c CFRunLoopRunSpecific + 448
11  GraphicsServices                0x000000018df69c08 GSEventRunModal + 164
12  UIKit                           0x000000018b3b2fd8 UIApplicationMain + 1152
13  Aiuto                           0x000000010004f4d8 main (main.m:15)
14  libdyld.dylib                   0x0000000194e7fa9c start + 0

Ideas?

回答1:

You need to put the

[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

in the dealloc method. This will tell the SKPaymentQueue that the RemoveAdsViewController will not listen to any transactions after it has been deallocated.