bevy_ios_iap

Crates.iobevy_ios_iap
lib.rsbevy_ios_iap
version
sourcesrc
created_at2024-05-02 13:54:45.774046
updated_at2024-12-02 20:50:43.522214
descriptionBevy Plugin and Swift Package to provide access to iOS native StoreKit2 from inside Bevy Apps
homepage
repositoryhttps://github.com/rustunit/bevy_ios_iap
max_upload_size
id1227765
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
(extrawurst)

documentation

https://docs.rs/bevy_ios_iap

README

bevy_ios_iap

crates.io docs.rs discord

Provides access to iOS native StoreKit2 Swift API from inside Bevy Apps. It uses Swift-Bridge to auto-generate the glue code and transport datatypes.

demo

Demo from our game using this crate: zoolitaire.com

Features

  • fetch products
  • purchase products
  • listen to changes in transaction states
  • fetch list of all transactions (to restore old purchases of non-consumables)
  • supports subscriptions
  • convenient observer based API

Notes

  • does not return locally un-signed/un-verified transactions

Todo

  • allow access to signature for remote verification
  • support offers
  • support family sharing

Instructions

  1. Add to XCode: Add SPM (Swift Package Manager) dependency
  2. Add Rust dependency
  3. Setup Plugin

1. Add to XCode

  • Add StoreKit framework: gamekit

  • Go to File -> Add Package Dependencies and paste https://github.com/rustunit/bevy_ios_iap.git into the search bar on the top right: xcode

  • Don't forget to configure your purchases like for any other iOS app, this guide will not focus on that, as it is the same no matter what engine you use. this guide focuses on setting things up in a bevy project.

Note: The rust crate used must be exactly the same version as the Swift Package (for binary compatibility reasons). I suggest using a specific version (like 0.2.0 in the screenshot) to make sure to always use binary matching versions!

2. Add Rust dependency

cargo add bevy_ios_iap

or

# always pin to the same exact version you also of the Swift package
bevy_ios_iap = { version = "=0.2.1" }

3. Setup Plugin

Initialize Bevy Plugin:

// request initialisation right on startup
app.add_plugins(IosIapPlugin::new(true));
fn bevy_system() {
    // If you set the plugin to manual init, this will register the 
    // TranscactionObserver to listen to updates to any Transactions and trigger
    // `IosIapEvents::Transaction` accordingly.
    // Note: this will require the user to be logged in into their apple-id and popup a login dialog if not
    bevy_ios_iap::init();

    // request product details, product IDs have to be explicitly provided
    // this will lead to a response: `IosIapEvents::Products`
    bevy_ios_iap::get_products(vec!["com.rustunit.zoolitaire.levelunlock".into()]);

    // trigger a product purchase for a specific product ID
    // this will lead to a response: `IosIapEvents::Purchase`
    bevy_ios_iap::purchase("com.rustunit.zoolitaire.levelunlock".into());

    // request to restore active subscriptions and non-consumables
    // this will lead to a response: `IosIapEvents::CurrentEntitlements`
    bevy_ios_iap::current_entitlements();

    // marks a transaction as finished after the product was provided to the customer.
    // if this is not called a transaction will keep being triggered automatically on 
    // app start as iOS wants us to be sure we granted the user the purchased good.
    // this will lead to a response: `IosIapEvents::TransactionFinished`
    bevy_ios_iap::finish_transaction(42);
}

Process Response Events from iOS back to us in Rust:

fn process_iap_events(
    mut events: EventReader<IosIapEvents>,
) {
    for e in events.read() {
        match e {
            IosIapEvents::Products(_) => todo!(),
            IosIapEvents::Purchase(_) => todo!(),
            IosIapEvents::Transaction(_) => todo!(),
            IosIapEvents::TransactionFinished(_) => todo!(),
            IosIapEvents::AllTransactions(_) => todo!(),
            IosIapEvents::CurrentEntitlements(_) => todo!(),
        }
    }
}

Our Other Crates

Bevy version support

bevy bevy_ios_iap
0.15 0.5,main
0.14 0.3,0.4
0.13 0.2

License

All code in this repository is dual-licensed under either:

at your option. This means you can select the license you prefer.

Your contributions

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 74

cargo fmt