在应用程序购买响应从未到来

时间:2011-11-11 14:28:13

标签: iphone objective-c ios

在我的iPhone应用中,我已经设置了应用内购买。我这样开始请求:

SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: @"com.nicknoble.tiprounder.upgrade"]];
request.delegate = self;
[request start];

我使用这种方法得到答案:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{   
    NSLog(@"response recieved");
}

这是一个错误:

-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
    NSLog(@"%@",error.description);
}

但两者都没有被召唤。我的项目没有警告或错误,我在我的设备(iPhone)上运行它。有什么建议吗?

编辑:

所以它适用于我的窗口的根视图控制器,但不适用于我提供的模态视图控制器。这是我的主视图控制器中的代码:

TestViewController *testView = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
[self presentModalViewController:testView animated:YES];

这是TestViewController的.h文件:

#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>

@interface TestViewController : UIViewController <SKProductsRequestDelegate>
@end

以下是我的.m文件的代码:

#import "TestViewController.h"

@implementation TestViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers:     [NSSet setWithObject: @"com.nicknoble.tiprounder.upgrade"]];
    request.delegate = self;
    [request start];
}

-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
    NSLog(@"%@",error.description);
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{   
    NSLog(@"response recieved");
}

@end
我绝望了。任何帮助都会很棒!谢谢!

2 个答案:

答案 0 :(得分:6)

SKProductsRequest实例需要在请求期间保留。如果不是,它将会默默地死亡(没有通知其代表),因为没有别的东西保留它。

现在,只要代码退出您分配的范围,ARC就会摆脱您的SKProductsRequest

解决方案是将SKProductsRequest保留在ivar中,并在请求完成/失败时将其设置为nil。这也有助于防止在已经有一个请求的情况下启动请求:

// Define _productsRequest as an ivar of type SKProductsRequest in your class

- (void)someMethodThatInitiatesTheProductsRequest
{
    if( _productsRequest != nil )
        return; // There's already a request in progress. Don't start another one.

    SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: @"com.nicknoble.tiprounder.upgrade"]];
    request.delegate = self;
    [request start];

    _productsRequest = request; // <<<--- This will retain the request object
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{   
    NSLog(@"response recieved");
    _productsRequest = nil; // <<<--- This will release the request object
}

-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
    NSLog(@"%@",error.description);
    _productsRequest = nil; // <<<--- This will release the request object
}

答案 1 :(得分:0)

首先检查您的产品标识符。 还要检查实现这些方法的类是否以某种方式释放。 (可能是自动释放并在您等待响应时自动释放)