在应用内购买iOS 6 [英] In-app purchase iOS 6

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

问题描述

我正在尝试在iOS 6的app购买中实施,我正在关注 Ray Wenderlich iOS 6应用程序内教程作为我的参考点。我从Ray的代码中做出的一个重大改变是,我只有一个按钮(应用程序购买中只有1个)用户点击购买而不是为它制作自定义表视图。我似乎无法让它工作,我一直在

I am trying to implement in app purchase for iOS 6 and I am following Ray Wenderlich iOS 6 in-app tutorial as my reference point. One big change I am making from Ray's code is that I have a single button (there is only 1 in app purchase) that the user taps to purchase opposed to making a custom table view for it. I cannot seem to get it work and I keep getting

-[__NSMallocBlock__ allObjects]: unrecognized selector sent to instance 0x1d5846d0
2012-10-03 00:03:25.715 myapp[752:907] *** Terminating app due to uncaught exception   'NSInvalidArgumentException', reason: '-[__NSMallocBlock__ allObjects]: unrecognized selector sent to instance 0x1d5846d0'

我认为问题发生在

- (void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHandler 

有2个辅助类光线已实施,我已实施。所有我改变的是应用程序ID号。这些类被称为IAPHelper.h / m和RageIAPHelper.h / m

There are 2 helper classes that ray has implemented and I have implemented. All I changed for these was the app id number. These classes are called IAPHelper.h/m and RageIAPHelper.h/m

我已经发布了我认为错误发生的代码(它不长)。如果有人能帮助我,我现在已经工作了大约4个小时了。
提前谢谢

I have posted the code where I think the error is happening (it is not long). If someone could please help me, I have been working on this for about 4 hours now. Thank you in advance

在buyViewController内

 -(void) viewDidLoad
 {
 [super viewDidLoad];
 [self reload]; //Customized for my own needs compared to what Ray had
  }

-(void)reload {
_products = nil;
[[RageIAPHelper sharedInstance] requestProductsWithCompletionHandler:^(BOOL success, NSArray *products) {
    if (success) {
        _products = products;
        //[self.tableView reloadData];
    }
    //[self.refreshControl endRefreshing];
 }];
}

名为IAPHelper.m的内部帮助方法

- (void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHandler {

// 1
 _completionHandler = [completionHandler copy];
// 2
_productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:_productIdentifiers]; // This is where I think the crash is
_productsRequest.delegate = self;
[_productsRequest start];
}


推荐答案

如果出现问题

-(void)requestProdcutsWithCompletionHandler:(RequestProductsCompletionHandler)
    completionHandler{
    ....
{

在第一次调用完成之前被调用两次。原因是因为使用了SKProductsRequestDelegate并在收到响应时调用以下函数:

is called twice before the first call completes. The reason is because the SKProductsRequestDelegate is used and calls the following function when it receives a response:

-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:
    (SKProductsResponse *)response{
    ...
    _completionHandler(YES, availableProducts)
    _completionHandler = nil;
}

注意处理响应后_completionHandler如何设置为nil。好吧,当下一个响应来自对requestProductsWithCompletionHandler的重复调用时,_completionHandler为nil并抛出异常。您可以通过以下两种方式解决这个问题:要么确保不要多次调用该函数(错误方法),要么放置一些逻辑以确保未使用completionHandler:

Notice how _completionHandler is set to nil after the response is processed. Well, when the next response comes through from the duplicate call to "requestProductsWithCompletionHandler", _completionHandler is nil and throws the exception. You can fix this two ways, either make sure you do not call that function multiple times (bad way) or put some logic to make sure the completionHandler is not in use:

-(void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHandler{

NSLog(@"Request Products");
if (!_completionHandler) {
    NSLog(@"CompletionHandler called, new one was made");
    _completionHandler = [completionHandler copy];

    NSMutableSet * productIdentifiers = [NSMutableSet setWithCapacity:_products.count];
        for (IAPProduct * product in _products.allValues) {
            product.availableForPurchase = NO;
            [productIdentifiers addObject:product.productIdentifier];
        }

    _productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
    _productsRequest.delegate=self;
    [_productsRequest start];
}else{
    NSLog(@"Duplicate call!");
}
}

productRequest首先检查以确保_completionHanlder为零,并且如果没有,那么它会忽略请求,因为处理程序正在使用中。

productRequest first checks to make sure _completionHanlder is nil, and if not then it ignores the request since the handler is in use.

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

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