Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

ios - in-app purchase in Swift with a single product

Is there a simple way to implement a in-app purchase in swift for a single product?

I want a button that triggers the in-app purchase like a [ad-removal] or [unlock-premium-content]

I can't understand the full logic of it.

I'm trying to follow and translate this tutorial from [Techotopia] http://www.techotopia.com/index.php/An_iOS_7_In-App_Purchase_Tutorial

But It's my first time with the StoreKit Framework, and also with Swift.

I just want to know the logic of the in-app purchase transaction with the Swift StoreKit Framework.

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Step 0: In your iTunes Connect account, create an In-App purchase.

For Single purchases, use this method:

  1. Import
import StoreKit
  1. Conform to StoreKit Delegate
class YOURViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
  1. Use a user default to track transactions
let defaults = NSUserDefaults.standardUserDefaults()
  1. Product ID. This one will be same as in your iTunes Connect in app purchase
var product_id: NSString?

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

    //Check if product is purchased

    if (defaults.boolForKey("purchased")){  
       // Hide a view or show content depends on your requirement
       overlayView.hidden = true     
    } else if (!defaults.boolForKey("stonerPurchased")) {
        print("false")            
    }
}
  1. Unlock Content. This is button action which will initialize purchase
@IBAction func unlockAction(sender: AnyObject) {

   print("About to fetch the products")

   // We check that we are allow to make the purchase.
   if (SKPaymentQueue.canMakePayments()) {
        var productID:NSSet = NSSet(object: self.product_id!);
        var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID);
        productsRequest.delegate = self;
        productsRequest.start();
        println("Fetching Products");
    } else {
        print("can't make purchases");
    }    
}
  1. Helper Methods
func buyProduct(product: SKProduct) {
    println("Sending the Payment Request to Apple");
    var payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment);
}
  1. Delegate Methods for IAP
func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {

    var count : Int = response.products.count
    if (count>0) {
        var validProducts = response.products
        var validProduct: SKProduct = response.products[0] as SKProduct
        if (validProduct.productIdentifier == self.product_id) {
            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: [AnyObject]!)    {
    print("Received Payment Transaction Response from Apple");

    for transaction:AnyObject in transactions {
        if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
            switch trans.transactionState {
            case .Purchased:
                print("Product Purchased");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
                defaults.setBool(true , forKey: "purchased")
                overlayView.hidden = true
                break;
            case .Failed:
                print("Purchased Failed");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction)
                break;
            case .Restored:
                print("Already Purchased");
                SKPaymentQueue.defaultQueue().restoreCompletedTransactions()    
            default:
                break;
            }
        }
    }        
}

Swift > 3.0

import StoreKit
class YOURVIEWController:UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {

let product_id: NSString = "com.some.inappid" // <!-- Change it to your inapp id

In your viewDidLoad add

override func viewDidLoad() {
    super.viewDidLoad()
    SKPaymentQueue.default().add(self)

In your buy button action

@IBAction func buyNowAction(_ sender: UIButton) {


    if (SKPaymentQueue.canMakePayments()) {
        let productID:NSSet = NSSet(array: [self.product_id as NSString]);
        let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
        productsRequest.delegate = self;
        productsRequest.start();
        print("Fetching Products");
    } else {
        print("can't make purchases");
    }
}

In Your Restore button action

// MARK: - Restore In App Purchase
    @IBAction func restoreAction(_ sender: UIButton) {

    if (SKPaymentQueue.canMakePayments()) {
        SKPaymentQueue.default().add(self)
        SKPaymentQueue.default().restoreCompletedTransactions()
    } else {
        // show error
    }

}

Add Delegates:

// SKProductRequest Delegate

func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

    print(response.products)
    let count : Int = response.products.count
    if (count>0) {

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

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction:AnyObject in transactions {
        if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{

            self.dismissPurchaseBtn.isEnabled = true
            self.restorePurchaseBtn.isEnabled = true
            self.buyNowBtn.isEnabled = true

            switch trans.transactionState {
            case .purchased:
                print("Product Purchased")
                //Do unlocking etc stuff here in case of new purchase
                SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)

                break;
            case .failed:
                print("Purchased Failed");
                SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
                break;
            case .restored:
                print("Already Purchased")
                //Do unlocking etc stuff here in case of restor

                SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
            default:
                break;
            }
        }
    }
}


//If an error occurs, the code will go to this function
    func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
        // Show some alert
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...