Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StoreKit delegate method not getting called

I have a problem. SKProductsRequest delegate methods are never getting called. this was previously asked but it had no answers. StoreKit delegate functions are not getting called

I'm not native english speaker, I may sound funny at times :P :(

I want to implement StoreKit in my iOS app. I made a class to handle all the StoreKit communication. This is the code:

@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);

}

I created an instance in my app delegate

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

Strangely, this code works ok when I run [store requestProducts] in my appDidFinishLaunching: method.

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

But when I run the code in settings tableView:didSelectRowAtIndexPath: the delegate method never gets called.

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

I don't know what am I doing bad. Tried almost everything and no errors, no crashes, but the delegate never gets called. Any help would be very appreciated.

Thanks so much in advance!

like image 928
BoteRock Avatar asked Mar 29 '12 23:03

BoteRock


2 Answers

The SKProductsRequestDelegate protocol also conforms to the SKRequestDelegate protocol, which has the following methods:

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

Implement those and see if either of them is being called. You're probably getting an error or otherwise unexpected response.

like image 69
rosslebeau Avatar answered Nov 16 '22 21:11

rosslebeau


I finally found the problem I had. I was not retaining the SKProductRequest, so the request was started and then it was dumped. I fixed it by doing this

//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];
}

Although the problem is fixed, I think that there should't be the need of retaining request, as I already called the start method and have set its delegate, it should do everything else by its own, and should retain itself until it receives response (or error) and after that deallocate itself.

like image 21
BoteRock Avatar answered Nov 16 '22 23:11

BoteRock