2012-03-29 96 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