title: How to check if subscription is active in iOS on app restart or reinstall
body: <p>I have an iOS app (Objective C) with a subscription (non renewing).
  How can I check that it is still active when the user restarts the app?</p>
  <p>I have read a lot about this, but does answer not seem clear how to do this correctly.</p>
  <p>What I have currently is when the app starts I register the TransactionObserver,</p>
  <pre><code>IAPManager* iapManager = [[IAPManager alloc] init];
  [[SKPaymentQueue defaultQueue] addTransactionObserver: iapManager];
  </code></pre>
  <p>Then when the user makes a purchase I have,</p>
  <pre><code>- (void) paymentQueue: (SKPaymentQueue *)queue updatedTransactions: (NSArray *)transactions {
      for (SKPaymentTransaction *transaction in transactions) {
          switch (transaction.transactionState) {
              case SKPaymentTransactionStatePurchasing:
                  [self showTransactionAsInProgress:transaction deferred:NO];
                  break;
              case SKPaymentTransactionStateDeferred:
                  [self showTransactionAsInProgress:transaction deferred:YES];
                  break;
              case SKPaymentTransactionStateFailed:
                  [self failedTransaction:transaction];
                  [queue finishTransaction: transaction];
                  break;
              case SKPaymentTransactionStatePurchased:
                  [self persistPurchase: transaction];
                  [queue finishTransaction: transaction];
                  break;
              case SKPaymentTransactionStateRestored:
                  [self restoreTransaction:transaction];
                  [queue finishTransaction: transaction];stopBusy];
                  break;
              default:
                  break;
          }
      }
  }
  </code></pre>
  <p>So this works fine when the user first subscribes.
  But I am confused on how you should store/track this purchase. I store in a static variable that the user subscribed to enable app functionality, but when the user restarts the app, how should I check that they have an active subscription?</p>
  <p>The code I was using was storing the subscription in either iCloud or NSUserDefaults.
  Storing in iCloud did not work at all, and when they restarted the app they lost their subscription. Storing in NSUserDefaults works, but the subscription will eventually expired, and could be refunded or canceled. How to check if it is active? I could store the subscription date and assume the duration and try to check myself, but this seems very wrong.</p>
  <p>Also what if the user uninstalls the app and reinstalls, or gets a new phone/etc.</p>
  <pre><code>- (void) persistPurchase: (SKPaymentTransaction*) transaction {
  #if USE_ICLOUD_STORAGE
      NSUbiquitousKeyValueStore *storage = [NSUbiquitousKeyValueStore defaultStore];
  #else
      NSUserDefaults *storage = [NSUserDefaults standardUserDefaults];
  #endif
      if ([transaction.payment.productIdentifier isEqualToString: SUBSCRIPTION]) {
          [storage setBool: true forKey: SUBSCRIPTION];
          [IAPManager upgrade];
      }
      [storage synchronize];
      [self unlockPurchase: transaction];
  }
  </code></pre>
  <p>For this I think I need to call restoreCompletedTransactions. I think it would make sense to not store the subscription in NSUserDefaults, but instead call restoreCompletedTransactions every time the app starts.</p>
  <p>But from what I read Apple seems to want you to have a &quot;Restore Purchases&quot; button that does this? Does it make sense to just call it every time the app starts?
  This will call the callback with every payment you ever processed (I think??) for each of these payments how to know if they are still active?</p>
  <p>I can get the transaction date but this does not tell me if the payment is expired or not, unless I assume it was not canceled and assume the duration and check the date myself? or does Apple only give you active subscriptions/payments from restoreCompletedTransactions?</p>
  <p>Also do I need to call finishTransaction for the restore again?</p>
  <pre><code>[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
  </code></pre>