SpokaneDude
SpokaneDude

Reputation: 4974

What is wrong with the way I have set up the Category .h file?

I have an iOS 9.0 app that I am trying to add In-App Purchase to; (I have never used IAP before and found some code online that will hopefully get me started).

I decided to make the code reside in a Category for maintenance simplicity (I have only used Categories once before). That said, I'm having problems with the actual structure of the Category. This is the .h file's code in the category:

#import "SettingsViewController.h"
#import <StoreKit/StoreKit.h>

@interface SettingsViewController (Purchases)

@end

#define kInAppPurchaseManagerProductsFetchedNotification
@"kInAppPurchaseManagerProductsFetchedNotification"

@interface InAppPurchaseManager : NSObject <SKProductsRequestDelegate>  {

SKProduct *proUpgradeProduct;
SKProductsRequest *productsRequest;
}

@end

This is the .m code:

#import "SettingsViewController+Purchases.h"

@implementation SettingsViewController (Purchases)

- (void)requestProUpgradeProductData {

NSSet *productIdentifiers = [NSSet setWithObject:@"com.runmonster.runmonsterfree.upgradetopro" ];
productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];

// we will release the request object in the delegate callback
}

#pragma mark -
#pragma mark SKProductsRequestDelegate methods

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response  {

NSArray *products = response.products;
proUpgradeProduct = [products count] == 1 ? [[products firstObject] retain] : nil;
if (proUpgradeProduct)
{
    NSLog(@"Product title: %@" , proUpgradeProduct.localizedTitle);
    NSLog(@"Product description: %@" , proUpgradeProduct.localizedDescription);
    NSLog(@"Product price: %@" , proUpgradeProduct.price);
    NSLog(@"Product id: %@" , proUpgradeProduct.productIdentifier);
}

for (NSString *invalidProductId in response.invalidProductIdentifiers)
{
    NSLog(@"Invalid product id: %@" , invalidProductId);
}

// finally release the reqest we alloc/init’ed in requestProUpgradeProductData
[productsRequest release];

[[NSNotificationCenter defaultCenter] postNotificationName:kInAppPurchaseManagerProductsFetchedNotification object:self userInfo:nil];
}

@end

The problem is the SKProduct and SKProductsRequest are not available to the .m file, which I'm sure is caused by the way I set up the .h file. Help would be greatly appreciated.

Upvotes: 0

Views: 49

Answers (1)

matt
matt

Reputation: 535222

It looks like you're trying to use a category for an unsuitable purpose. In particular, it looks like your category is supposed to contain a property declaration. But, as the documentation says:

Categories can be used to declare either instance methods or class methods but are not usually suitable for declaring additional properties. It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods. You can write your own accessor methods in the category implementation, but you won’t be able to keep track of a value for that property unless it’s already stored by the original class.

Upvotes: 1

Related Questions