Single IAP in Swift 2

suggest change

After creating an IAP in iTunesConnect:

In the view controller that you want to buy in

import StoreKit

and add the relevant delegates

class ViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {

declare a variable with the product id from iTunesConnect

var product_id: NSString?

override func viewDidLoad() {
    
    
    product_id = "YOUR_PRODUCT_ID"
    super.viewDidLoad()
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)

   //Check if product is purchased
   if (NSUserDefaults.standardUserDefaults().boolForKey("purchased")){
   
       // Hide ads
       adView.hidden = true
        
   } else {
       print("Should show ads...")
        
   }

}

wire a button to a function to purchase the IAP

@IBAction func unlockAction(sender: AnyObject) {
    
   print("About to fetch the product...")

// Can make payments
if (SKPaymentQueue.canMakePayments())
    {
        let productID:NSSet = NSSet(object: self.product_id!);
        let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<NSString>);
        productsRequest.delegate = self;
        productsRequest.start();
        println("Fetching Products");
    }else{
        print("Can't make purchases");
    }

}

And here are some helper methods

func buyProduct(product: SKProduct){
    println("Sending the Payment Request to Apple");
    let payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment);
    
 }

adView is not declared on this page. Remove the transaction observer when this controller goes away. The purchased flag is not a receipt check. The payment-queue method has to use the Swift 2 name, or it is not the observer callback. Finish a .Restored transaction. Calling restoreCompletedTransactions from that case starts another restore.

the delegate methods that must be declared

func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {

    let count : Int = response.products.count
    if (count>0) {
        var validProduct: SKProduct = response.products[0] as SKProduct
        if (validProduct.productIdentifier == (self.product_id as! String)) {
            print(validProduct.localizedTitle)
            print(validProduct.localizedDescription)
            print(validProduct.price)
            buyProduct(validProduct);
        } else {
            print(validProduct.productIdentifier)
        }
    } else {
        print("nothing")
    }
}


func request(request: SKRequest!, didFailWithError error: NSError!) {
    print("Error Fetching product information");
}

func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    print("Received Payment Transaction Response from Apple");

    for transaction in transactions {
        switch transaction.transactionState {
        case .Purchased:
            print("Product Purchased");
            SKPaymentQueue.defaultQueue().finishTransaction(transaction)
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "purchased")
            adView.hidden = true
            break;
        case .Failed:
            print("Purchased Failed");
            SKPaymentQueue.defaultQueue().finishTransaction(transaction)
            break;
        case .Restored:
            print("Already Purchased");
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "purchased")
            adView.hidden = true
            SKPaymentQueue.defaultQueue().finishTransaction(transaction)
            break;
        default:
            break;
        }
    }
}

And then the code to restore a non-consumable in app purchase

if (SKPaymentQueue.canMakePayments()) {
  SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents