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:
This is the code:
- (void)viewDidLoad {
[super viewDidLoad];
Reachability *reachability = [Reachability reachabilityWithHostName"google.com"];
NetworkStatus status = [reachability currentReachabilityStatus];
if (!status == NotReachable) {
if ([SKPaymentQueue canMakePayments]) {
NSLog(@"Parental Controls are disabled");
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject"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"com.harryworks.starfighter.booster1"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPaymentayment];
NSLog(@"Payment Added");
}
- (void)productsRequestSKProductsRequest *)request didReceiveResponse
SKProductsResponse *)response {
SKProduct *validProduct = nil;
int count = [response.products count];
if (count > 0) {
validProduct = [response.products objectAtIndex:0];
}
NSLog(@"Recieved Responce");
}
- (void)paymentQueueSKPaymentQueue *)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"There was an error: %@", transaction.error.code];
NSLog(error);
statusLabel.text = @"An Error Occured";
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}