I have this code and all the process it's ok and enter in this state: SKPaymentTransactionStatePurchased
But I don't know what will happen when the subscription trial end. This implementation is correct?
On the other hand, when the user run the application show again the alert to confirm the product is already purchased. how to avoid this alert when the user open the app again when the application is already purchased?
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self requestPurchasableRequest];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
-(void) requestPurchasableRequest{
NSString *identifier = @"com.xxx.xxx";
NSSet *productIdentifierSet = [[NSSet alloc]initWithObjects:identifier, nil];
SKProductsRequest *productRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifierSet];
productRequest.delegate = self;
[productRequest start];
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
if (response.invalidProductIdentifiers.count>0) {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Warning"
message:@"This product is not available at this moment please try again later."
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}else {
products = response.products;
SKPayment *payment = [SKPayment paymentWithProduct:products[0]];
[[SKPaymentQueue defaultQueue]addPayment:payment];
}
-(void) paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStateDeferred:
break;
case SKPaymentTransactionStatePurchased:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
default:
break;
}
}
}