IOS应用程序内购买相关的崩溃(ios in-app purchase related crash)

2019-10-30 06:34发布

我有一个非消耗性产品,通过应用程序内购买,从我的应用程序中删除广告。 它运作良好,有时候,如果我再次关闭该应用程序并打开,他是我在NSDefaults检测是否专业版购买代码工作正常。 的一件事是,在购买时,我想删除从标签栏上的“升级到专业版”按钮,该代码崩溃的应用程序。 下面是处理应用程序内购买的控制器:

#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

doRemoveAds方法的最后3行是那些崩溃应用程序,具有以下错误:

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

想法?

Answer 1:

你需要把

[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

在dealloc方法。 这将告诉SKPaymentQueue,它已被释放后RemoveAdsViewController不会听任何交易。



文章来源: ios in-app purchase related crash