1

In my iPhone app, i've set up an in app purchase. I start the request like this:

SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: @"com.nicknoble.tiprounder.upgrade"]];
request.delegate = self;
[request start];

And I use this method to get the response:

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{   
    NSLog(@"response recieved");
}

And this one to catch an error:

-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
    NSLog(@"%@",error.description);
}

But neither ever get called. My project has no warnings or errors, and I am running this on my device (iPhone). Any suggestions?

EDIT:

So it works on my window's root view controller, but not on modal view controllers i present. Here is the code from my main view controller:

TestViewController *testView = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil];
[self presentModalViewController:testView animated:YES];

Here is the .h file for TestViewController:

#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>

@interface TestViewController : UIViewController <SKProductsRequestDelegate>
@end

Here is the code for my .m file:

#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

Im desperate. Any help would be great! Thanks!

user1007895
  • 3,925
  • 11
  • 41
  • 63

2 Answers2

6

The SKProductsRequest instance need to be retained for the duration of the request. If it's not, it will die silently (without informing its delegate) because nothing else is retaining it.

Right now, ARC will get rid of your SKProductsRequest as soon as the code exits the scope where you allocate it.

A solution would be to keep your SKProductsRequest in an ivar and set it to nil when the request completes/fails. This is also handy to prevent starting a request while there's already one in progress:

// 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
}
Pascal Bourque
  • 5,101
  • 2
  • 28
  • 45
  • 1
    +1 but wounldn't you have to retain the debate instead of the request, as it should receive the calls and the request does not retain it either (its property definition uses assign instead of strong). – Drux Jul 11 '14 at 09:58
0

check your product identifier first. also check if the class in which these methods are implemented is not released somehow. (might be autoreleased and got released automatically while you are waiting for response)

Saurabh Passolia
  • 8,099
  • 1
  • 26
  • 41
  • Product Identifier matches perfectly, and im using ARC. No releases or autoreleases anywhere in my code. – user1007895 Nov 11 '11 at 14:58
  • I thought this was the only code in my project really relevant to this. Could you be more specific on what might help? – user1007895 Nov 12 '11 at 00:25
  • Ok now my mind is blown. I just copied and pasted this code into another viewController... it works there. What is going on. – user1007895 Nov 12 '11 at 00:46