2011-11-11 39 views
1

在我的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 

我很絕望。任何幫助將是偉大的!謝謝!

回答

0

請首先檢查您的產品標識。 也會檢查這些方法實現的類是否以某種方式被釋放。 (可能會自動發佈並在您等待響應時自動發佈)

+0

產品標識符完美匹配,並且使用ARC進行即時通訊。我的代碼中沒有任何版本或autoreleases。 – user1007895

+0

嗯...也許一些更多類的代碼可能會啓發問題.. – samfisher

+0

我認爲這是我的項目中唯一真正與此相關的代碼。你能更具體地說明什麼可能有幫助嗎? – user1007895

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

+1,但是你不必保留辯論而不是請求,因爲它應該接收這些調用,並且請求也不保留它(它的屬性定義使用assign而不是強)。 – Drux