在我的 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)上运行它。有什么建议么?
编辑:
所以它适用于我的窗口的 Root View Controller ,但不适用于我目前的模态视图 Controller 。这是我的主视图 Controller 的代码:
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
我很绝望。任何帮助都会很棒!谢谢!
最佳答案
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
}
关于iphone - 应用内购买响应永远不会到来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8095367/