This not ok: SKProduct *removeAdsProductID = (SKProduct *)kRemoveAdsProductIdentifier;
- you can't use a NSString*
instead of a SKProduct*
.
You should instead use validProduct
from - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
.
You only need 1 button/action, the tapRemoveAds
, which should be initiating the purchase (no need for the purchase
IBAction
).
You need to take hold of an actual SKProduct*
:
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
SKProduct *validProduct = nil;
int count = [response.products count];
if(count > 0){
validProduct = [response.products objectAtIndex:0];
NSLog(@"Products Avalable!");
// initiate the actual purchase
[self purchase:validProduct];
}
else if(!validProduct){
NSLog(@"No products avalable");
}
}
-(void)purchase:(SKProduct *)product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
A better solution would be to retrieve the list of valid products in a previous step (e.g: when the app starts), and only enable the upgrade
button when the SKProduct *
object for it is available.
There are other issues as well, like not handling the restore (should be handled similarly as in SKPaymentTransactionStatePurchasing
case). I suggest you have a look over In-App Purchase Programming Guide for a better understanding.