11

I have a simple app, with one non-consumable in-app purchase option.

From my initial view controller I have a single 'enter' button.

This button will send a 'free' user (has not made non-consumable purchase) to one TabBarController "A" and series of views and a 'paid' user to the other TabBarController "B" a different set of views. These views will never intersect.

I would like to check wether my code is able to distinguish effectively as to whether a user has made the in-app purchase or otherwise.

Here is my code:

import UIKit
import StoreKit

class MainMainViewController: UIViewController, UIScrollViewDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver {

let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;

...

  override func viewDidLoad() {

      product_id = "some.iap.id";
      SKPaymentQueue.defaultQueue().addTransactionObserver(self)


    super.viewDidLoad()
}

@IBAction func Enter(sender: AnyObject) {

    //Check if product is purchased
    if (defaults.boolForKey("purchased")){
         print("User has purchased da goods!")

        // Grant or otherwise full access based on whether user has purchased/not purchased.

        // Goto TabBarController A - FULL Access:
        let vc = self.storyboard!.instantiateViewControllerWithIdentifier("TabBarControllerPaid") as! TabBarControllerPaid
        self.presentViewController(vc, animated: true, completion: nil)
    }

    else if (!defaults.boolForKey("purchased")){
           print("user has NOT purchased yet")

        // Goto TabBarController B - PARTIAL Access:
        let vc = self.storyboard!.instantiateViewControllerWithIdentifier("TabBarControllerFree") as! TabBarControllerFree
        self.presentViewController(vc, animated: true, completion: nil)
   }


}

}

Many thanks in advance for any answers, comments or thoughts :-)

1 Answers1

4

Take a look at this tutorial, under the "Purchased Items" section.

It shows how to keep track of items that were purchased (and yes, you'll be using NSUserDefaults if you follow this tutorial closely), and you'll also learn how to restore previous purchases if the app is deleted or moved to a user's new device.

In your code up there, I see "defaults.boolForKey("purchased")". The big problem with this code that I can see is that it only allows for one item to be purchased (either the "purchased" boolean exists in NSUserDefaults or it doesn't).

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Great tutorial - could not ask for one more current! A day old I believe! I may amend my whole code in line with that content. Just a side note to your second para - if I only intend to have one non-consumable ever, could I not just stick to my code above? For a one IAP product app it is fine the way it is? Will my app be able to determine whether the product was purchased if the user has no internet connection? I believe it will? – Katherine Jenkins Mar 31 '16 at 14:16
  • And what exactly in that tutorial? `purchasedProductIdentifiers` is only being filled from local preferences... – user924 Aug 29 '18 at 09:13
  • I think this userDefault with boolean will work for him since his purchase type is non-consumable. – Naval Hasan Jul 31 '20 at 01:08
  • was just wondering, this works perfectly when the user makes the purchase and is on the app on the same device. What if the user deletes the app or installs it on another device - is there a way for us to know this user has purchased something before ? Can something like this be achieved without a server ? I know we have the restore purchases option but from a UX point of view, knowing and activating features automatically would be great. – Shawn Frank Feb 25 '21 at 06:38