Game Development Reference
In-Depth Information
Listing 10-2. ExtrasController.m (viewDidLoad)
#define PURCHASE_INGAME_SAUCERS @"com.beltcommander.ingame.saucers"
#define PURCHASE_INGAME_POWERUPS @"com.beltcommander.ingame.powerups"
//....
- (void)viewDidLoad
{
[super viewDidLoad];
idsToProducts = [NSMutableDictionary new];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
//Get set of product ids
NSSet* potentialProducts = [NSSet setWithObjects:PURCHASE_INGAME_POWERUPS,
SKProductsRequest* request = [[SKProductsRequest alloc] initWithProductIdentifiers:potentialProducts];
[request setDelegate:self];
[request start];
[self setGameParams: [GameParameters readFromDefaults]];
viewDidLoad
for the class
ExtrasController
. In this task, we start by
idsToProducts
map and by getting the default
SKPaymentQueue
by calling
defaultQueue
,
self
as an observer. Because
self
conforms to the protocol
SKPaymentTransactionObserver
,
ExtrasController
will be informed of all changes in state for a purchase. Once we
have a registered observer, we create a set of known product IDs. In this case, the
NSSet
potentialProducts
is created from two constants:
PURCHASE_INGAME_POWERUPS
and
PURCHASE_
INGAME_SAUCERS
. As we can see, these constants are the same strings as we specified when we
created the products in iTunes Connect.
Once we have specified our set of potential products, we create an
SKProductsRequest
, set
self
as
the delegate, and call
start
on it. While StoreKit is working out which product IDs are valid, we call
setGameParams
and specify that we should use the
GameParameters
object we have stored the default
NSUsersDefaults
. When StoreKit has its results, the tasks
productsRequest:didReceiveResponse:
,
as shown in Listing 10-3.
Listing 10-3. ExtrasController.m (productsRequest:didReceiveResponse:)
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
for (SKProduct* aProduct in response.products){
if ([aProduct.productIdentifier isEqualToString:PURCHASE_INGAME_SAUCERS]){
[saucerButton setEnabled:YES];
[saucerButton setHidden:NO];
[idsToProducts setObject:aProduct forKey:PURCHASE_INGAME_SAUCERS];
} else if ([aProduct.productIdentifier isEqualToString:PURCHASE_INGAME_POWERUPS]){
[powerupButton setEnabled:YES];
[powerupButton setHidden:NO];
[idsToProducts setObject:aProduct forKey:PURCHASE_INGAME_POWERUPS];
}
}
}