0

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;
        }
    }

}
Fabio
  • 1,913
  • 5
  • 29
  • 53
  • 1
    Why are you initiating a purchase in `viewDidLoad`? If you are using auto renewing subscriptions then you need to examine the receipt periodically t see if the subscription has expired or been renewed. – Paulw11 Nov 05 '17 at 03:38
  • The transaction model of `StoreKit.framework` is here to provide states for actions being initiated by user (purchase / restore purchases) and update user interface respectively. Validation of purchases is another part of story. This also includes auto-renewable subscription logic. See the doc: https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html – Yevhen Dubinin Nov 05 '17 at 04:48
  • @Paulw11 do you have an example about how can I implement this subscription correctly ? – Fabio Nov 06 '17 at 16:16

0 Answers0