Hi, I’m trying to enter my Product ID’s in Xcode. The In-App Purchased code is already set up, I just need some information on where I can place the Product ID’s in Xcode that created in App Store Connect.
You can put them in a constants file. It’s a file of constant strings, so you can then refer to them anywhere in the code, and not have to type a string value everywhere.
Create a new Swift file, name it Constants.swift
In the file:
enum Constants {
static let prodID1 = “name you put in AppStoreConnect”
static let prodID2 = “name you put in AppStoreConnect”
}
here’s the Code I have for making my in-app purchase. you should be able to tailor it for your needs. the code I have in the ‘fail’ case is actually the success logic. When I run it on my phone to test it, it will only go through the fail path so I put it there to test that the logic works.
/
import UIKit
import StoreKit
class UpgradeController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
let model = DataModel()
var myProduct: SKProduct?
var user: User?
@IBOutlet weak var imageLock: UIImageView!
@IBOutlet weak var buttonPurchase: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
print (user!.nameLast!)
print (user!.unlimited)
fetchProducts()
}
//MARK: FETCH PRODUCT
func fetchProducts() {
/
print ("fetch products")
let request = SKProductsRequest(productIdentifiers: ["YOURPRODUCTNAMEHERE"])
request.delegate = self
request.start()
}
@IBAction func buttonActionPurchase(_ sender: Any) {
guard let myProduct = myProduct else {
return
}
if SKPaymentQueue.canMakePayments() {
let payment = SKPayment(product: myProduct)
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().add(payment)
}
}
//DELEGATE METHODS
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print ("made it to delegate function")
if let product = response.products.first {
myProduct = product
print (myProduct?.productIdentifier)
print (myProduct?.localizedDescription)
print (myProduct?.localizedTitle)
print (myProduct?.price)
print (myProduct?.priceLocale)
}
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchasing:
print ("purchase is in process")
break
case .purchased, .restored:
print ("purchase was made")
UserDefaults.standard.set(true, forKey: "Unlimited")
SKPaymentQueue.default().finishTransaction(transaction)
SKPaymentQueue.default().remove(self)
user?.unlimited = true
print (user!.unlimited)
model.updateToUnlimited(self.user!)
//TODO: UNLOCK LOCK
imageLock.image = nil
break
case .failed, .deferred:
print ("purchase failed")
self.user?.unlimited = true
print (self.user!.unlimited)
model.updateToUnlimited(self.user!)
//TODO: UNLOCK LOCK
imageLock.image = nil
SKPaymentQueue.default().finishTransaction(transaction)
SKPaymentQueue.default().remove(self)
break
default:
print ("default")
SKPaymentQueue.default().finishTransaction(transaction)
SKPaymentQueue.default().remove(self)
}
}
}