Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

HarryWorksInc

macrumors regular
Original poster
Feb 21, 2010
179
0
I have been working on adding in app purchase to one of my apps for the first time. It checks the parental controls, adds the payment, does everything that I know of but then the payment doesn't work.

This is the code:
- (void)viewDidLoad {
[super viewDidLoad];

Reachability *reachability = [Reachability reachabilityWithHostName:mad:"google.com"];
NetworkStatus status = [reachability currentReachabilityStatus];

if (!status == NotReachable) {
if ([SKPaymentQueue canMakePayments]) {
NSLog(@"Parental Controls are disabled");

SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:mad:"com.harryworks.starfighter.booster1"]];
productsRequest.delegate = self;
[productsRequest start];

statusLabel.text = @"You are connected to the Store";
} else {
statusLabel.text = @"You are not connected to the Store";
}

} else {
statusLabel.text = @"You are not connected to the Internet";
}
}

-(IBAction)booster1 {
SKPayment *payment = [SKPayment paymentWithProductIdentifier:mad:"com.harryworks.starfighter.booster1"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
NSLog(@"Payment Added");
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
SKProduct *validProduct = nil;
int count = [response.products count];
if (count > 0) {
validProduct = [response.products objectAtIndex:0];
}
NSLog(@"Recieved Responce");
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
NSLog(@"Purchasing...");
statusLabel.text = @"Downloading...";

break;

case SKPaymentTransactionStatePurchased:
//SAVE DOWNLOADED ITEM
NSLog(@"Got Item!");

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
statusLabel.text = @"Thanks for your purchase, comeback soon!";

break;

case SKPaymentTransactionStateRestored:

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
statusLabel.text = @"Thanks for your repurchase, comeback soon!";

break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code !=SKErrorPaymentCancelled) {
NSString *error = [NSString stringWithFormat:mad:"There was an error: %@", transaction.error.code];
NSLog(error);
statusLabel.text = @"An Error Occured";
}

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];

break;
}
}
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.