ios应用内购买相关崩溃 [英] ios in-app purchase related crash

查看:1203
本文介绍了ios应用内购买相关崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种非消费性产品,可以通过应用内购买从我的应用中移除广告。
它有效地工作,如果我关闭应用程序并再次打开,我的代码检测在NSDefaults如果PRO版本被购买工程正常。
事情是,在购买时,我想从标签栏中删除升级到PRO按钮,这个代码崩溃的应用程序。这里是处理应用内购买的控制器:

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

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

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


b $ b

想法?

Ideas?

推荐答案

您需要将

[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

。这将告诉SKPaymentQueue,RemoveAdsViewController在它被释放后不会监听任何事务。

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

这篇关于ios应用内购买相关崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆