Code Monkey home page Code Monkey logo

storekit's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

storekit's Issues

cannot convert value of type \'Int\' to expected element type \'Array<SwiftVersion>.ArrayLiteralElement\'

Showing Recent Messages

invalidManifestFormat("/var/folders/3t/04146p9j1zq58qk668c8_f_40000gn/T/com.apple.appstoreagent/TemporaryFile.lvoms6.swift:10:30: error: cannot convert value of type 'Int' to expected element type 'Array.ArrayLiteralElement' (aka 'SwiftVersion')\npkg.swiftLanguageVersions = [3, 4]\n

^\n/var/folders/3t/04146p9j1zq58qk668c8_f_40000gn/T/com.apple.appstoreagent/TemporaryFile.lvoms6.swift:10:33: error: cannot convert value of type 'Int' to expected element type 'Array.ArrayLiteralElement' (aka 'SwiftVersion')\npkg.swiftLanguageVersions = [3, 4]\n

^", diagnosticFile: Optional(AbsolutePath:"/Users/Adam-Milton-Barker/Library/Developer/Xcode/DerivedData/xxxxxxx-hckynpimisgfqhetportamtfadgo/SourcePackages/ManifestLoading/storekit.dia")) in https://github.com/PromiseKit/StoreKit.git

Transactions left unfinished when the purchases are interrupted

When using this extension I found edge cases when this extension does not work properly. These are mainly “interrupted purchases”, see the following excerpt from Apple documentation:

It is important to add the observer at launch, in application(_:didFinishLaunchingWithOptions:), to ensure that it persists during all launches of your app, receives all payment queue notifications, and continues transactions that may be processed outside the app, such as:

  • Promoted in-app purchases
  • Background subscription renewals
  • Interrupted purchases

Source: https://developer.apple.com/documentation/storekit/in-app_purchase/setting_up_the_transaction_observer_for_the_payment_queue

It is clear from this documentation that this extension does not implement observers according the Apple guidance. Due to the nature of PromiseKit it is hard to come up with a solution which would fit.

Reproducing the issue

The thing is, SKPayment extension currently leaves some transactions unfinished. Even worse, the behavior is different on major iOS versions: 12 works fine and 13+ do not work. They notify about updated transactions at different times.

Steps:

  1. During the purchase kill the app.
  2. When you try to purchase the same product again it is either bought instantly or it fails and new dialog for buying is presented.

This ends in one unfinished transaction which is persisted during the runs of the app. When using purely this extension you will end up in inconsistent state.

It is not as obvious with non-consumable in-app purchases, but when using with subscriptions the issue becomes clearer.

My solution

In the end, I ended up using PromiseKit nevertheless, since it is extensively used in the app where I found this issue.

Firstly, I implemented this class to complete any unfinished transactions:

final class SubscriptionFinisher: NSObject, SKPaymentTransactionObserver {
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
            case .purchased, .restored, .failed:
                queue.finishTransaction(transaction)
            default:
                break
            }
        }
    }
}

Secondly, I add this observer on application launch and remove it during StoreKit promise runs. Finishing transactions twice can end in an undefined behavior.

queue.remove(finisher)
let purchase = SKPayment(product: product)
   .promise()
   .ensure { queue.add(finisher) }

Next steps

I either propose to update the documentation for this extension or provide guidance how to prevent others from running into this issue. As for now, I hope this issue will serve as some kind of documentation by itself.

TLDR Do not use this extension when working with auto-renewable subscriptions, because sooner or later one of your users will encounter an edge case when the transactions won't be completed properly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.