Skip to content

Commit

Permalink
add Product
Browse files Browse the repository at this point in the history
  • Loading branch information
masuidrive committed Aug 22, 2010
1 parent d215b77 commit cc730fe
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Classes/JpMasuidriveTiStorekitModule.h
Expand Up @@ -7,9 +7,11 @@
#import "TiModule.h"
#import "PaymentQueue.h"

@interface JpMasuidriveTiStorekitModule : TiModule <SKPaymentTransactionObserver>
@interface JpMasuidriveTiStorekitModule : TiModule <SKPaymentTransactionObserver, SKProductsRequestDelegate>
{
@private
PaymentQueue* paymentQueue;
NSMutableArray* productRequestCallback;
}

@end
48 changes: 47 additions & 1 deletion Classes/JpMasuidriveTiStorekitModule.m
Expand Up @@ -11,6 +11,8 @@

#import "Payment.h"
#import "PaymentTransaction.h"
#import "Product.h"


@implementation JpMasuidriveTiStorekitModule

Expand All @@ -35,6 +37,7 @@ -(void)startup
// this method is called when the module is first loaded
// you *must* call the superclass
[super startup];
productRequestCallback = [NSMutableArray array];

NSLog(@"[INFO] %@ loaded",self);
}
Expand All @@ -55,6 +58,7 @@ -(void)dealloc
{
// release any resources that have been retained by the module
[paymentQueue release];
[productRequestCallback release];
[super dealloc];
}

Expand Down Expand Up @@ -96,7 +100,6 @@ - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)tran
for (SKPaymentTransaction *transaction in transactions) {
PaymentTransaction* t = [[[PaymentTransaction alloc] _initWithPageContext:[self pageContext] transaction:transaction] autorelease];
NSDictionary* evt = [NSDictionary dictionaryWithObject:t forKey:@"transaction"];
NSLog(@"evt=%@", t);

switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
Expand Down Expand Up @@ -132,13 +135,56 @@ - (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedW
NSLog(@"> paymentQueue:restoreCompletedTransactionsFailedWithError:");
}

- (void)productsRequest:(SKProductsRequest *)request
didReceiveResponse:(SKProductsResponse *)response
{
KrollCallback* callback = nil;
for(NSArray* line in productRequestCallback) {
if([line objectAtIndex:0] == request) {
callback = [[[line objectAtIndex:1] retain] autorelease];
[productRequestCallback removeObject:line];
break;
}
}
if(callback) {
NSMutableArray* products = [NSMutableArray array];
for(SKProduct* product in response.products) {
[products addObject:[[Product alloc] _initWithPageContext:[self pageContext] product:product]];
}
[callback call:[NSArray arrayWithObjects:products, response.invalidProductIdentifiers, nil] thisObject:self];
}
}

#pragma Public APIs

-(id)createPayment:(id)args
{
return [[[Payment alloc] _initWithPageContext:[self executionContext] args:args] autorelease];
}

-(void)findProducts:(id)args
{
ENSURE_ARG_COUNT(args, 2);
id arg0 = [args objectAtIndex:0];
NSSet* productIds = nil;
if([arg0 isKindOfClass:[NSString class]]) {
productIds = [NSSet setWithObject:arg0];
}
else if([arg0 isKindOfClass:[NSArray class]]) {
productIds = [NSSet setWithArray:arg0];
}
else {
[self throwException:TiExceptionInvalidType subreason:[NSString stringWithFormat:@"expected: Array or Stinrg, was: %@",[arg0 class]] location:CODELOCATION]; \
}

id callback = [args objectAtIndex:1];
ENSURE_TYPE(callback, KrollCallback);
SKProductsRequest *req = [[[SKProductsRequest alloc] initWithProductIdentifiers:productIds] autorelease];
req.delegate = self;
[req start];
[productRequestCallback addObject:[NSArray arrayWithObjects: req, callback, nil]];
}

-(id)paymentQueue
{
if(paymentQueue==nil) {
Expand Down
19 changes: 19 additions & 0 deletions Classes/Product.h
@@ -0,0 +1,19 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#import "TiProxy.h"
#import <StoreKit/StoreKit.h>

@interface Product : TiProxy {

@private
SKProduct* product;
}

-(id)_initWithPageContext:(id<TiEvaluator>)context product:(SKProduct*)product_;


@end
55 changes: 55 additions & 0 deletions Classes/Product.m
@@ -0,0 +1,55 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#import "Product.h"

#import "TiUtils.h"

@implementation Product

-(id)_initWithPageContext:(id<TiEvaluator>)context
product:(SKProduct*)product_
{
if (self = [super _initWithPageContext:context]) {
product = [product_ retain];
}
return self;
}

-(void)dealloc
{
[product release];
[super dealloc];
}

-(id)productIdentifier
{
return [[[product productIdentifier] retain] autorelease];
}

-(id)localizedDescription
{
return [[[product localizedDescription] retain] autorelease];
}

-(id)price
{
return [[[product price] retain] autorelease];
}

-(id)localizedTitle
{
return [[[product localizedTitle] retain] autorelease];
}

-(id)priceLocale
{
return [[[[product priceLocale] localeIdentifier] retain] autorelease];
}


@end
12 changes: 10 additions & 2 deletions tistorekit.xcodeproj/project.pbxproj
Expand Up @@ -29,6 +29,8 @@
5951574F12204BA50059B0F3 /* StoreKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5951574E12204BA50059B0F3 /* StoreKit.framework */; };
598C92281221151B00DD87E6 /* PaymentTransaction.h in Headers */ = {isa = PBXBuildFile; fileRef = 598C92261221151B00DD87E6 /* PaymentTransaction.h */; };
598C92291221151B00DD87E6 /* PaymentTransaction.m in Sources */ = {isa = PBXBuildFile; fileRef = 598C92271221151B00DD87E6 /* PaymentTransaction.m */; };
598C92FB1221C9F900DD87E6 /* Product.h in Headers */ = {isa = PBXBuildFile; fileRef = 598C92F91221C9F900DD87E6 /* Product.h */; };
598C92FC1221C9F900DD87E6 /* Product.m in Sources */ = {isa = PBXBuildFile; fileRef = 598C92FA1221C9F900DD87E6 /* Product.m */; };
59DCC21912205B87007EDC2E /* Payment.h in Headers */ = {isa = PBXBuildFile; fileRef = 59DCC21712205B87007EDC2E /* Payment.h */; };
59DCC21A12205B87007EDC2E /* Payment.m in Sources */ = {isa = PBXBuildFile; fileRef = 59DCC21812205B87007EDC2E /* Payment.m */; };
59DCC24A1220719B007EDC2E /* PaymentQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = 59DCC2481220719B007EDC2E /* PaymentQueue.h */; };
Expand Down Expand Up @@ -56,6 +58,8 @@
5951574E12204BA50059B0F3 /* StoreKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = StoreKit.framework; path = System/Library/Frameworks/StoreKit.framework; sourceTree = SDKROOT; };
598C92261221151B00DD87E6 /* PaymentTransaction.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PaymentTransaction.h; path = Classes/PaymentTransaction.h; sourceTree = "<group>"; };
598C92271221151B00DD87E6 /* PaymentTransaction.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = PaymentTransaction.m; path = Classes/PaymentTransaction.m; sourceTree = "<group>"; };
598C92F91221C9F900DD87E6 /* Product.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Product.h; path = Classes/Product.h; sourceTree = "<group>"; };
598C92FA1221C9F900DD87E6 /* Product.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Product.m; path = Classes/Product.m; sourceTree = "<group>"; };
59DCC21712205B87007EDC2E /* Payment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Payment.h; path = Classes/Payment.h; sourceTree = "<group>"; };
59DCC21812205B87007EDC2E /* Payment.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Payment.m; path = Classes/Payment.m; sourceTree = "<group>"; };
59DCC2481220719B007EDC2E /* PaymentQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = PaymentQueue.h; path = Classes/PaymentQueue.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -109,12 +113,14 @@
08FB77AEFE84172EC02AAC07 /* Classes */ = {
isa = PBXGroup;
children = (
598C92261221151B00DD87E6 /* PaymentTransaction.h */,
598C92271221151B00DD87E6 /* PaymentTransaction.m */,
598C92F91221C9F900DD87E6 /* Product.h */,
598C92FA1221C9F900DD87E6 /* Product.m */,
59DCC21712205B87007EDC2E /* Payment.h */,
59DCC21812205B87007EDC2E /* Payment.m */,
59DCC2481220719B007EDC2E /* PaymentQueue.h */,
59DCC2491220719B007EDC2E /* PaymentQueue.m */,
598C92261221151B00DD87E6 /* PaymentTransaction.h */,
598C92271221151B00DD87E6 /* PaymentTransaction.m */,
24DE9E0F11C5FE74003F90F6 /* JpMasuidriveTiStorekitModuleAssets.h */,
24DE9E1011C5FE74003F90F6 /* JpMasuidriveTiStorekitModuleAssets.m */,
24DD6CF71134B3F500162E58 /* JpMasuidriveTiStorekitModule.h */,
Expand Down Expand Up @@ -145,6 +151,7 @@
59DCC21912205B87007EDC2E /* Payment.h in Headers */,
59DCC24A1220719B007EDC2E /* PaymentQueue.h in Headers */,
598C92281221151B00DD87E6 /* PaymentTransaction.h in Headers */,
598C92FB1221C9F900DD87E6 /* Product.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -213,6 +220,7 @@
59DCC21A12205B87007EDC2E /* Payment.m in Sources */,
59DCC24B1220719B007EDC2E /* PaymentQueue.m in Sources */,
598C92291221151B00DD87E6 /* PaymentTransaction.m in Sources */,
598C92FC1221C9F900DD87E6 /* Product.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit cc730fe

Please sign in to comment.