我有问题。永远不会调用SKProductsRequest委托方法。这是先前被问到的,但它没有答案。 StoreKit delegate functions are not getting called
我不是母语为英语的人,有时听起来很可笑:P :(
我想在我的iOS应用中实现StoreKit。我创建了一个类来处理所有StoreKit通信。这是代码:
@implementation VRStoreController
- (void) requestProducts
{
SKProductsRequest *request= [[SKProductsRequest alloc]
initWithProductIdentifiers:
[NSSet setWithObject: @"myProductID"]];
request.delegate = self;
[request start];
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
}
- (void) productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSLog(@"F7U12");
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
NSArray *myProducts = response.products;
NSLog(@"%@", myProducts);
}
我在我的app delegate
中创建了一个实例@interface VRAppDelegate
@property (strong, nonatomic) VRStoreController *store;
@end
奇怪的是,当我在appDidFinishLaunching:方法中运行[store requestProducts]时,这段代码工作正常。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//app initialization...
self.store = [[VRStoreController alloc]init];
[store requestProducts]; //This works ok, delegate method gets called.
return YES;
}
但是当我在设置tableView:didSelectRowAtIndexPath中运行代码时:委托方法永远不会被调用。
//VRAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//app initialization...
self.store = [[VRStoreController alloc]init];//initialize store
return YES;
}
//VRSettingsViewController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
//case 0, 1...
case 2:
{
VRStoreController *store = ((VRAppDelegate *)[UIApplication sharedApplication].delegate).store;
[store requestProducts]; //calls SKProductRequest start, but never calls delegate method.
NSLog(@"GO PRO");
}
default:
break;
}
}
我不知道我做得不好。几乎尝试了一切,没有错误,没有崩溃,但委托从未被调用。任何帮助都将非常感激。
提前非常感谢!
答案 0 :(得分:18)
SKProductsRequestDelegate
协议也符合SKRequestDelegate
协议,该协议具有以下方法:
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error;
- (void)requestDidFinish:(SKRequest *)request;
实现它们并查看是否正在调用它们中的任何一个。您可能会收到错误或意外的响应。
答案 1 :(得分:18)
我终于找到了问题。 我没有保留SKProductRequest,因此请求已启动,然后被转储。 我通过这样做来修复它
//VRStoreController.m
@interface VRStoreController ()
@property (strong, nonatomic) SKProductRequest *request; //Store the request as a property
@end
@implementation VRStoreController
@synthesize request;
- (void) requestProducts
{
self.request = [[SKProductsRequest alloc] //save the request in the property
initWithProductIdentifiers:
[NSSet setWithObject: @"myProductID"]];
self.request.delegate = self;
[self.request start];
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
}
虽然问题已解决,但我认为不需要保留请求,因为我已经调用了start方法并设置了它的委托,它应该自己做其他所有事情,并且应该保留自己直到它接收响应(或错误),然后解除分配。