I am trying IAP for the first time and I need some help connecting the dots.
My app has two different non-consumable IAPs which I have set up in a "product" class if its own like this:
enum IAPProduct: String{
case tempoLevels = ".....TempoLevels"
case timingLevels = ".....TimingLevels"
}
Then I set up a helper class via a tutorial I found like this:
class IAPService: NSObject {
private override init() {}
static let shared = IAPService()
var products = [SKProduct]()
let paymentQueue = SKPaymentQueue.default()
func getProducts() {
let products: Set = [IAPProduct.tempoLevels.rawValue, IAPProduct.timingLevels.rawValue]
let request = SKProductsRequest(productIdentifiers: products)
request.delegate = self
request.start()
paymentQueue.add(self)
}
func purchase(product: IAPProduct) {
guard let productToPurchase = products.filter({ $0.productIdentifier == product.rawValue }).first
else {return}
let payment = SKPayment(product: productToPurchase)
paymentQueue.add(payment)
}
}
extension IAPService: SKProductsRequestDelegate {
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
self.products = response.products
for product in response.products {
print(product.localizedTitle)
}
}
}
extension IAPService: SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
print(transaction.transactionState.status(), transaction.payment.productIdentifier)
switch transaction.transactionState {
case .purchasing: break
default: queue.finishTransaction(transaction)
}
}
}
}
extension SKPaymentTransactionState {
func status() -> String{
switch self{
case .deferred: return "deferred"
case .failed: return "failed"
case .purchased: return "purchased"
case .purchasing: return "purchasing"
case .restored: return "restored"
#unknown default:
return("Something is wrong...")
}
}
}
I would like to trigger some functions in another view controller when the product is purchased. How do I do this?
I sort of set up a function in one of the view controllers that checks UserDefaults in one of the view controllers like this:
func isPurchased() -> Bool {
let purchaseStatus = UserDefaults.standard.bool(forKey: ??)
if purchaseStatus == true {
print("Previously purchased!")
return true
}else{
print("Never purchased!")
return false
}
}
I'm not sure how I can use this function, but if I can somehow, I have code in my app that would work with it if at all possible.
You can use directly SKPaymentTransactionState for purchase checking.
You can call like thi after creating the transaction if want to Bool value
func isPurchased(transaction: SKPaymentTransaction) -> Bool {
return transaction.transactionState == .purchased
}
or directly String value from extension ofSKPaymentTransactionState
func isPurchased(transaction: SKPaymentTransaction) -> String {
return transaction.status
}
In the mean time, you should not ever store a boolean for checking if user has bought in-app purchase in UserDefaults. User can change it very easily (without jailbreaking) and get your goodies for free! You should Use Keychain instead of UserDefaults.
Related
As I understand, it is best to only test public methods of a class.
Let's have a look at this example. I have a view model for the view controller.
protocol MyViewModelProtocol {
var items: [SomeItem] { get }
var onInsertItemsAtIndexPaths: (([IndexPath]) -> Void)? { get set }
func viewLoaded()
}
class MyViewModel: MyViewModelProtocol {
func viewLoaded() {
let items = createDetailsCellModels()
updateCellModels(with: items)
requestDetails()
}
}
I want to test class viewLoaded(). This class calls two other methods - updateItems() and requestDetails()
One of the methods sets up the items and the other one call API to retrieve data and update those items. Items array us updated two times and onInsertItemsAtIndexPaths are called two times - when setting up those items and when updating with new data.
I can test whether after calling viewLoaded() expected items are set up and that onInsertItemsAtIndexPaths is called.
However, the test method will become rather complex.
What is your view, should I test those two methods separately or just write this one huge test?
By testing only viewLoaded(), my idea is that the implementation can change and I only care that results are what I expect.
I think the same thing, only public functions should be tested, since public ones use private ones, and your view on MVVM is correct. You can improve it by adding a DataSource and a Mapper that allows you to improve testing.
However, yes, the test seems huge to me, the tests should test simple units and ensure that small parts of the code work well, with the example you show is difficult, you need to divide by layers (clean code).
In the example you load the data into the viewModel and make it difficult to mockup the data. But if you have a Domain layer you can pass the UseCase mock to the viewModel and control the result. If you run a test on your example, the result will also depend on what the endpoint returns. (404, 200, empty array, data with error ...). So it is important, for testing purposes, to have a good separation by layers. (Presentation, Domain and Data) to be able to test each one separately.
I give you an example of how I would test a view mode, sure there are better and cooler examples, but it's an approach.
Here you can see a viewModel
protocol BeersListViewModel: BeersListViewModelInput, BeersListViewModelOutput {}
protocol BeersListViewModelInput {
func viewDidLoad()
func updateView()
func image(url: String?, index: Int) -> Cancellable?
}
protocol BeersListViewModelOutput {
var items: Box<BeersListModel?> { get }
var loadingStatus: Box<LoadingStatus?> { get }
var error: Box<Error?> { get }
}
final class DefaultBeersListViewModel {
private let beersListUseCase: BeersListUseCase
private var beersLoadTask: Cancellable? { willSet { beersLoadTask?.cancel() }}
var items: Box<BeersListModel?> = Box(nil)
var loadingStatus: Box<LoadingStatus?> = Box(.stop)
var error: Box<Error?> = Box(nil)
#discardableResult
init(beersListUseCase: BeersListUseCase) {
self.beersListUseCase = beersListUseCase
}
func viewDidLoad() {
updateView()
}
}
// MARK: Update View
extension DefaultBeersListViewModel: BeersListViewModel {
func updateView() {
self.loadingStatus.value = .start
beersLoadTask = beersListUseCase.execute(completion: { (result) in
switch result {
case .success(let beers):
let beers = beers.map { DefaultBeerModel(beer: $0) }
self.items.value = DefaultBeersListModel(beers: beers)
case .failure(let error):
self.error.value = error
}
self.loadingStatus.value = .stop
})
}
}
// MARK: - Images
extension DefaultBeersListViewModel {
func image(url: String?, index: Int) -> Cancellable? {
guard let url = url else { return nil }
return beersListUseCase.image(with: url, completion: { (result) in
switch result {
case .success(let imageData):
self.items.value?.items?[index].image.value = imageData
case .failure(let error ):
print("image error: \(error)")
}
})
}
}
Here you can see the viewModel test using mocks for the data and view.
class BeerListViewModelTest: XCTestCase {
private enum ErrorMock: Error {
case error
}
class BeersListUseCaseMock: BeersListUseCase {
var error: Error?
var expt: XCTestExpectation?
func execute(completion: #escaping (Result<[BeerEntity], Error>) -> Void) -> Cancellable? {
let beersMock = BeersMock.makeBeerListEntityMock()
if let error = error {
completion(.failure(error))
} else {
completion(.success(beersMock))
}
expt?.fulfill()
return nil
}
func image(with imageUrl: String, completion: #escaping (Result<Data, Error>) -> Void) -> Cancellable? {
return nil
}
}
func testWhenAPIReturnAllData() {
let beersListUseCaseMock = BeersListUseCaseMock()
beersListUseCaseMock.expt = self.expectation(description: "All OK")
beersListUseCaseMock.error = nil
let viewModel = DefaultBeersListViewModel(beersListUseCase: beersListUseCaseMock)
viewModel.items.bind { (_) in}
viewModel.updateView()
waitForExpectations(timeout: 10, handler: nil)
XCTAssertNotNil(viewModel.items.value)
XCTAssertNil(viewModel.error.value)
XCTAssert(viewModel.loadingStatus.value == .stop)
}
func testWhenDataReturnsError() {
let beersListUseCaseMock = BeersListUseCaseMock()
beersListUseCaseMock.expt = self.expectation(description: "Error")
beersListUseCaseMock.error = ErrorMock.error
let viewModel = DefaultBeersListViewModel(beersListUseCase: beersListUseCaseMock)
viewModel.updateView()
waitForExpectations(timeout: 10, handler: nil)
XCTAssertNil(viewModel.items.value)
XCTAssertNotNil(viewModel.error.value)
XCTAssert(viewModel.loadingStatus.value == .stop)
}
}
in this way you can test the view, the business logic and the data separately, in addition to being a code that is very reusable.
Hope this helps you, I have it posted on github in case you need it.
https://github.com/cardona/MVVM
I have no problem when I click on restore purchase. But when I click on unlockProButt or buy100CoinsButt, I get the error "Thread 1: Fatal error: Index out of range". What is the problem? How can I make the purchase smoothly? I have always received errors in all the in-app purchase examples I have tried. I always have problems with this line of code.
Error Line: purchaseMyProduct(validProducts[productIndex])
Error: Thread 1: Fatal error: Index out of range
import UIKit
import StoreKit
class ViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
#IBOutlet weak var buy100coinsButton: UIButton!
#IBOutlet weak var unlockProButton: UIButton!
#IBOutlet weak var restorePurchaseButton: UIButton!
var productsRequest = SKProductsRequest()
var validProducts = [SKProduct]()
var productIndex = 0
// viewDidLoad()
override func viewDidLoad() {
super.viewDidLoad()
buy100coinsButton.isHidden = true
unlockProButton.isHidden = true
fetchAvailableProducts()
}
func fetchAvailableProducts() {
let productIdentifiers = NSSet(objects:
"..", // 0
"..." // 1
)
productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>)
productsRequest.delegate = self
productsRequest.start()
}
func productsRequest (_ request:SKProductsRequest, didReceive response:SKProductsResponse) {
if (response.products.count > 0) {
validProducts = response.products
// 1st IAP Product
let prod100coins = response.products[0] as SKProduct
let prodUnlockPro = response.products[1] as SKProduct
print("1st rpoduct: " + prod100coins.localizedDescription)
print("2nd product: " + prodUnlockPro.localizedDescription)
buy100coinsButton.isHidden = false
unlockProButton.isHidden = false
}
}
func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {
return true
}
func canMakePurchases() -> Bool { return SKPaymentQueue.canMakePayments() }
func purchaseMyProduct(_ product: SKProduct) {
if self.canMakePurchases() {
let payment = SKPayment(product: product)
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().add(payment)
} else { print("Purchases are disabled in your device!") }
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction {
switch trans.transactionState {
case .purchased:
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
if productIndex == 0 {
print("You've bought 100 coins!")
buy100coinsButton.setTitle("Buy another 100 Coins Chest", for: .normal)
} else {
print("You've unlocked the Pro version!")
unlockProButton.isEnabled = false
unlockProButton.setTitle("PRO version purchased", for: .normal)
}
break
case .failed:
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
print("Payment has failed.")
break
case .restored:
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
print("Purchase has been successfully restored!")
break
default: break
}}}
}
func restorePurchase() {
SKPaymentQueue.default().add(self as SKPaymentTransactionObserver)
SKPaymentQueue.default().restoreCompletedTransactions()
}
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
print("The Payment was successfull!")
}
// Buttons -------------------------------------
#IBAction func buy100CoinsButt(_ sender: UIButton) {
productIndex = 0
purchaseMyProduct(validProducts[productIndex])
}
#IBAction func unlockProButt(_ sender: UIButton) {
productIndex = 1
purchaseMyProduct(validProducts[productIndex])
}
#IBAction func restorePurchaseButt(_ sender: UIButton) {
restorePurchase()
}
}
you should create a completion handler so you know when your app actually receives the inApp products, so when the delegate "productsRequest" has completed its operation, for this add to your class the appropriate protocol => "SKRequestDelegate" (all these requests are made asynchronously, I hope you know the differences between synchronous and asynchronous, I recommend you to search on google) Now that you have added the SKRequestDelegate protocol dependency to your class, you can implement the two functions that will inform you of the completion or failure of your request: "requestDidFinish" and "request ... didFailWithError", I'll give you an example with some code below, I hope I've been of help to you, for other clarifications please ask :)
extension ViewController : SKRequestDelegate {
// fine della richiesta in app
func requestDidFinish(_ request: SKRequest) {
debugPrint("[IAP] ---- REQUEST COMPLETED ---- ")
}
// Error Request
func request(_ request: SKRequest, didFailWithError error: Error) {
debugPrint("[IAP] Error: ", error)
}
}
Ah I forgot: check that "self.validProducts.count" is greater than 0 otherwise it means that your array is empty and so when you go to => "purchaseMyProduct(validProducts[productIndex])" your application will crash because the "productIndex" position in your "validProducts" array doesn't exist...
I have a class that represents Calendar items (model) retrieved from the event store. I haven't implemented any delegation yet for the AppDelegate or ViewControllers.
All my methods in this class are static functions - the main reason is so that I can "see" them from the AppDelegate or the VC. I have a suspicion that:
1) I need to make this a singleton - whose only function is to retrieve calendar items from the eventStore and post to the UI
2) learn how to code better - perhaps creating an instance of the class in the AppDelegate and the VC
This is still very fuzzy to me - not sure if posting code would help, but the class has a bunch of "static func .... doSomething() { ...}" and is called by the AppDelegate and VC as "ClassName.doSomething()..."
I'm prepared to refactor the Class code, thinking that a singleton would work - or perhaps things are just fine as they are...
EDITED: Adding code:
import Foundation
import EventKit
class Calendars: NSObject {
enum calendarAuthState {
case restricted
case authorized
case denied
case notDetermined
}
struct Calendar {
var id: String
var color: NSColor
var title: String
var isUserActive: Bool
var events: [EventItem]
}
struct EventItem {
var originalStartDate: Date
var date: String
var title: String
var isAllDayEvent: Bool
}
static var calendarState: calendarAuthState = .notDetermined
static var eventStore = EKEventStore()
static var currentCalendars = [Calendar]()
//MARK: Check Calendar Authorization Status
static func calendarAuthorizationStatus() {
let status = EKEventStore.authorizationStatus(for: .event)
switch (status) {
case EKAuthorizationStatus.notDetermined:
// This happens on first-run
calendarState = .notDetermined
case EKAuthorizationStatus.authorized:
calendarState = .authorized
case EKAuthorizationStatus.restricted:
self.requestAccessToCalendar()
calendarState = .restricted
case EKAuthorizationStatus.denied:
self.requestAccessToCalendar()
calendarState = .denied
}
}
static func requestAccessToCalendar() {
self.eventStore.requestAccess(to: EKEntityType.event, completion: {
(accessGranted: Bool, error: Error?) in
if accessGranted == true {
DispatchQueue.main.async(execute: {
self.calendarState = .authorized
})
} else {
DispatchQueue.main.async(execute: {
self.calendarState = .denied
})
}
})
}
//MARK: Do the two below
static func createMenuFromCalendars() {
guard calendarState == .authorized else {
return
}
let calendars = self.returnCalendars()
guard calendars.count >= 0 else {
return
}
self.addCalendarsToMenuItems(from: calendars)
}
//MARK: First, return the calendar titles from the Store
static func returnCalendars() -> [Calendar] {
guard self.calendarState == .authorized else {
return[]
}
let calendars = self.eventStore.calendars(for: .event)
for calendar in calendars {
self.currentCalendars.append(Calendar(id: calendar.calendarIdentifier, color: calendar.color, title: calendar.title, isUserActive: false, events: []))
}
return self.currentCalendars
}
//MARK: Next, send those to the Menu for MenuItem creation
static func addCalendarsToMenuItems(from calendars:[Calendar]) {
let appDelegate = NSApplication.shared.delegate as! AppDelegate
let appMainMenu = NSApp.mainMenu
if let calendarMenu = appMainMenu?.item(withTitle: "Calendars") {
let calendarSubMenu = calendarMenu.submenu
for calendar in calendars {
let menuItem = calendarSubMenu?.addItem(withTitle: calendar.title, action: #selector(appDelegate.actionFromSelectedCalendar) , keyEquivalent: "")
menuItem?.isEnabled = true
menuItem?.state = .off
menuItem?.target = appDelegate.self
menuItem?.toolTip = calendar.id
}
}
}
class func retrieveCalendarEvents() {
guard self.calendarState == .authorized || !(self.currentCalendars.isEmpty) else {
return
}
let startDate = Date()
let endDate = Date(timeIntervalSinceNow: 4*24*3600)
var activeCalendars = findUserActiveCalendars(in: currentCalendars)
//need to flush the events at this stage or they'll pile
guard !((activeCalendars?.isEmpty)!) else {
return
}
var eventCalendar = [EKCalendar]()
for dayBookCalendar in activeCalendars! {
// much of the risk here is unwrapping optionals unsafely!!!!! - refactor this and other please
eventCalendar.append(self.eventStore.calendar(withIdentifier: dayBookCalendar.id)!)
let eventPredicate = eventStore.predicateForEvents(withStart: startDate, end: endDate, calendars: eventCalendar)
let returnedEvents = eventStore.events(matching: eventPredicate)
let calendarIndex = findCalendarIndex(by: dayBookCalendar.id, in: currentCalendars)
for event in returnedEvents {
let eventItems = eventItem(from: event)
currentCalendars[calendarIndex!].events.append(eventItems)
}
}
}
//MARK: Helper methods and stuff
static func changeUserCalendarState(with id:String, state:Bool) {
guard !(currentCalendars.isEmpty) else {
return
}
let calendarIndex = findCalendarIndex(by: id, in:self.currentCalendars)
if let calendarIndex = calendarIndex {
currentCalendars[calendarIndex].isUserActive = !state
retrieveCalendarEvents()
}
}
static func findCalendarIndex(by id:String, in calendarArray: [Calendar]) -> Int? {
return calendarArray.index(where: {$0.id == id})
}
static func findUserActiveCalendars(in calendarArray: [Calendar]) -> [Calendar]? {
return calendarArray.filter({$0.isUserActive == true})
}
// static func flushEventsFromCalendar(in calendarArray: inout [Calendar]) {
// calendarArray.map({$0.events.removeAll()})
// }
static func eventItem(from events:EKEvent) -> EventItem {
return EventItem(originalStartDate: events.startDate, date:eventTime(from: events.startDate), title: events.title!, isAllDayEvent: events.isAllDay)
}
static func parseCalendarEvents(from events:[EKEvent]) -> [EventItem] { //can this be variadic?
var calendarEvents = [EventItem]()
for event in events {
calendarEvents.append(eventItem(from: event))
}
return calendarEvents
}
static func eventTime(from date:Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeStyle = .short
dateFormatter.locale = Locale.current
let stringTime = dateFormatter.string(from: date)
return stringTime
}
}
''
I think you're making an elementary mistake about object-oriented programming. In your Calendars class you seem to have encapsulated all the code for accessing the user's calendar. Then you seem to have reasoned: "Well, this code needs to be callable from anywhere. Therefore all my class's members need to be global (static / class)."
That's a mistake. There is nothing wrong with doing such encapsulation; indeed it's a good thing. But then the way to use your encapsulation is with a helper instance. For example, let's say you're in a view controller (which is most likely after all). Then it can have a property:
let calendarHelper = Calendars()
Now all (or nearly all) your members can (and should) become instance members. Remember, instances of the same type each get to maintain state separately from one another; that is part of their encapsulation. You're going to want that ability.
If your underlying reason for thinking you need static/class members is that you only want one EKEventStore instance for the life of the app, then push the globalness / staticness down to that one object (e.g. by a "shared" EKEventStore and methods for accessing it) and let everything else be a normal instance member.
From what you've said, suspicion 1) is correct - you need to use a singleton:
class CalendarService {
private var eventStore = EKEventStore()
//Static shared instance, this is your singleton
static var sharedInstance = CalendarService()
//Your public methods for adding events can go here
public func doSomething() {
//...
}
//As can your private methods for producing, deleting and editing calendar events + checking permissions
}
Usage:
CalendarService.sharedInstance.doSomething()
I can't really say much more without specific examples of your existing code.
I am writing my first iOS app in Swift and have been having some problems with the in-app purchases functionality. I think I've got the actual buy function working but I'm not sure about the restore functionality. I'm just providing one product which is a 'pro' version of the app. This is stored in the User Defaults location and having it unlocks the extra functionality. My restore function is confusing me, can someone have a look at it and tell me where I should be setting my User Defaults value? Should it be within the paymentQueueRestoreCompletedTransactionsFinished function or the paymentQueue function?
import UIKit
import StoreKit
class StoreController: UIViewController, SKProductsRequestDelegate,
SKPaymentTransactionObserver {
// Properties
var defaults = UserDefaults.standard
var list = [SKProduct]()
var p = SKProduct()
// Methods
override func viewDidLoad() {
super.viewDidLoad()
// Get list of products for the store
localTitle.isEnabled = false
localDescription.isEnabled = false
price.isEnabled = false
buy.isEnabled = false
restore.isEnabled = false
getProducts()
}
// Outlets
#IBOutlet weak var localTitle: UILabel!
#IBOutlet weak var localDescription: UILabel!
#IBOutlet weak var price: UILabel!
#IBOutlet weak var buy: UIButton!
#IBOutlet weak var restore: UIButton!
// Actions
#IBAction func buy(_ sender: UIButton) {
print("buy pressed")
for product in list {
let prodID = product.productIdentifier
if(prodID == "com.squidgylabs.pro") {
p = product
buyProduct()
}
}
}
#IBAction func restore(_ sender: UIButton) {
print("restore pressed")
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()
}
// Methods
func getProducts() {
print("starting getproducts function")
if(SKPaymentQueue.canMakePayments()) {
print("IAP is enabled, loading")
let productID: NSSet = NSSet(objects: "com.squidgylabs.pro")
let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
} else {
print("please enable IAPS")
}
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("starting productsrequest")
let myProduct = response.products
for product in myProduct {
print("product added")
list.append(product)
}
localTitle.isEnabled = true
localDescription.isEnabled = true
restore.isEnabled = true
buy.isEnabled = true
price.isEnabled = true
// Update labels
localTitle.text = list[0].localizedTitle
localDescription.text = list[0].localizedDescription
// Format the price and display
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .currency
if let formattedPrice = formatter.string(from: list[0].price){
price.text = formattedPrice
}
}
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
print("starting paymentQueueResoreCompletedTransactionsFnished")
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction
let prodID = t.payment.productIdentifier as String
switch prodID {
case "com.squidgylabs.pro":
print("case is correct product ID in payment...finished")
default:
print("IAP not found")
}
}
}
func buyProduct() {
print("buy " + p.productIdentifier)
let pay = SKPayment(product: p)
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().add(pay as SKPayment)
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("entering func paymentQueue")
for transaction: AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction
switch trans.transactionState {
case .purchased:
print("buy ok, unlock IAP HERE")
print(p.productIdentifier)
let prodID = p.productIdentifier
switch prodID {
case "com.squidgylabs.pro":
print("setting pro in defaults...")
defaults.set(true, forKey: "pro")
default:
print("IAP not found")
}
queue.finishTransaction(trans)
break
case .failed:
print("buy error")
queue.finishTransaction(trans)
break
case .restored:
print("case .restored in paymentQ")
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
queue.finishTransaction(trans)
break
default:
print("Default")
break
}
}
}
}
It may be done like this.
public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchased:
completePurchaseForTransaction(transaction)
case .restored:
completePurchaseForTransaction(transaction)
default:
break
}
}
private func completePurchaseForTransaction(_ transaction: SKPaymentTransaction) {
let productIdentifier = transaction.payment.productIdentifier
// Update UserDefaults here
SKPaymentQueue.default().finishTransaction(transaction)
}
You might consider using other class for StoreKit access instead of a UIViewController. Because
Your application should always expect to be notified of completed
transactions.
https://developer.apple.com/documentation/storekit/skpaymentqueue/1506042-add
And View controllers come and go. It might even crash the app if you don't remove object you added as an observer to the SKPaymentQueue.
I have simple, well-documented StoreKit library on GitHub that you might check out to get the idea -- https://github.com/suvov/VSStoreKit
I am trying to implement in app purchases in a Swift app, of a consumable in-app purchase, with product ID productID
Research
Using this answer, I learned about the basic pattern for in app purchases, and tried to adapt it, like so. I do not need to use the NSUserDefaults method of having the app remember if a purchase is made, as I am handling that with a different solution.
Code
class ViewController: UICollectionViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
viewDidLoad() {
//Unimportant stuff left out
product_id = "productID"
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}
#IBAction func thisISTheButtonToTriggerTheInAppPurchases(sender: AnyObject) {
if (SKPaymentQueue.canMakePayments()) {
var productID:NSSet = NSSet(object: self.product_id!);
var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
productsRequest.delegate = self;
productsRequest.start();
print("Fetching Products");
}else{
print("can't make purchases");
}
}
func buyProduct(product: SKProduct){
print("Sending the Payment Request to Apple");
var payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment);
}
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)
break;
case .Failed:
print("Purchased Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
break;
case .Restored:
print("Already Purchased");
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
default:
break;
}
}
}
}
}
Error
Type 'ViewController' does not conform to protocol 'SKPaymentTransactionObserver'
I researched this error a bit, and sources like this suggest that such an error is due to not having required functions.
According to Apple, paymentQueue:updatedTransactions: is the only one required, but I seem to be implementing that. A possible theory of mine is that I am not implementing it correctly - if true, how do I fix that?
Expected Behavior
The app presents, and if given favorable user input, performs the IAP with the ID productID.
Thanks for helping, and let me know if you need any more information!
It's very simple. The error is about SKPaymentTransactionObserver. Its only required method is:
func paymentQueue(queue: SKPaymentQueue,
updatedTransactions transactions: [SKPaymentTransaction])
The signature for your method doesn't match that:
func paymentQueue(queue: SKPaymentQueue!,
updatedTransactions transactions: [AnyObject]!)
...so it isn't the same method, so you don't conform.