2012-03-29 93 views
4

我遇到问题。 SKProductsRequest委托方法永远不会被调用。这是以前问的,但它没有答案。 StoreKit delegate functions are not getting calledStoreKit代理方法未被调用

我不是英语为母语,我也许听起来滑稽次:第:(

我要实现我的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); 

} 

我在我的应用程序委托创建一个实例

@interface VRAppDelegate 
@property (strong, nonatomic) VRStoreController *store; 
@end 

奇怪的是,这个代码工作正常,当我在我的APPD运行[店requestProducts] idFinishLaunching:方法。

- (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; 
    } 
} 

我不知道我在做什么坏事。尝试几乎所有的东西,没有错误,没有崩溃,但委托永远不会被调用。任何帮助将非常感激。

非常感谢!

回答

16

我终于找到了我遇到的问题。 我没有保留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]; 
} 

问题虽然是固定的固定它,我认为有should't是固定的要求,因为我已经叫start方法,并建立其委托的需要,它应该做的一切都由自己完成,并且应该保留自己,直到它收到回应(或错误),然后再释放自己。

+0

恭喜修理!如果可以,请确保将答案标记为“已接受”,以便其他人会注意到您的问题已得到解决,并能够从您的解决方案中学习。干杯〜 – 2012-03-30 14:27:24

+1

我同意,它不应该被保留,只是更多不必要的代码行。 – spybart 2014-05-08 21:11:12

+0

最后它能正常工作。在添加上面的技巧后,它没有工作,然后我也综合了我的inApp类(这是在另一个类中调用)。所有的作品,没有更多的崩溃和代表正在呼叫:) – Muzammil 2014-05-22 19:30:39

18

SKProductsRequestDelegate协议也符合SKRequestDelegate协议,它有下面的方法:

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error; 
- (void)requestDidFinish:(SKRequest *)request; 

执行这些,看看它们中的被调用。您可能会收到错误或意外的回应。

+2

那些不被称为 – BoteRock 2012-03-30 13:13:12

+0

这帮助了我,谢谢。 – 2013-10-16 11:18:35