mercredi 5 octobre 2016

Swift In App Purchases Tutorial, Confused over Button to Method Linking.

Vote count: 0

i am fairly new IOS developer, but have recently managed to get IAP working by following this tutorial.

http://ift.tt/25yjA8d

Now this all works, and items are displayed in a listview with a buy button generated when a item is detected as purchasable.

Now what I am confused about is the following:

1.How to induce the payment method from a seperate view controller, i can not see any calls to buy the product from the existing buttons?

2.How would they know what product i was wanting to buy, how is the product reference being passed through when buying a product.

Looking through the code i am struggling to understand these 2 vital elements.

Code attached below

Thanks

Product Cell class, appears to dynamically create buy button in the table view based on the products available from the store import UIKit import StoreKit

class ProductCell: UITableViewCell {
   static let priceFormatter: NumberFormatter = {
let formatter = NumberFormatter()

formatter.formatterBehavior = .behavior10_4
formatter.numberStyle = .currency

return formatter
  }()

  var buyButtonHandler: ((_ product: SKProduct) -> ())?

  var product: SKProduct? {
    didSet {
  guard let product = product else { return }

  textLabel?.text = product.localizedTitle

  if RageProducts.store.isProductPurchased(product.productIdentifier) {
    accessoryType = .checkmark
    accessoryView = nil
    detailTextLabel?.text = ""
  } else if IAPHelper.canMakePayments() {
    ProductCell.priceFormatter.locale = product.priceLocale
    detailTextLabel?.text = ProductCell.priceFormatter.string(from: product.price)

    accessoryType = .none
    accessoryView = self.newBuyButton()
  } else {
    detailTextLabel?.text = "Not available"
  }
}
   }

  override func prepareForReuse() {
super.prepareForReuse()

textLabel?.text = ""
detailTextLabel?.text = ""
accessoryView = nil
  }

  func newBuyButton() -> UIButton {
let button = UIButton(type: .system)
button.setTitleColor(tintColor, for: UIControlState())
button.setTitle("Buy", for: UIControlState())
button.addTarget(self, action: #selector(ProductCell.buyButtonTapped(_:)), for: .touchUpInside)
button.sizeToFit()

    return button
 }

  func buyButtonTapped(_ sender: AnyObject) {
buyButtonHandler?(product!)
  }
}

RageProducts, contains product identifiers for products in itunes connect

 import Foundation

 public struct RageProducts {

  public static let Item1 = "com.justlift.Recipe1"
  public static let Item2 = "com.justlift.Test"

 fileprivate static let productIdentifiers: Set<ProductIdentifier> =[RageProducts.Item1,Item2]

  public static let store = IAPHelper(productIds:  RageProducts.productIdentifiers)
     }

 func resourceNameForProductIdentifier(_ productIdentifier: String) -> String?     {
   return productIdentifier.components(separatedBy: ".").last
      }

asked 31 secs ago

Let's block ads! (Why?)



Swift In App Purchases Tutorial, Confused over Button to Method Linking.

Aucun commentaire:

Enregistrer un commentaire