ProMotion-iap is in-app purchase notification support for the popular RubyMotion gem ProMotion.
gem 'ProMotion-iap'The Product class is an abstraction layer that provides a simpler interface when working with a single IAP product.
If you are dealing with multiple products you will want to use the IAP Module directly (documented below).
class PurchaseScreen < PM::Screen
def on_load
product = PM::IAP::Product.new("productid")
product.retrieve do |product, error|
# product looks something like the following
{
product_id: "productid1",
title: "title",
description: "description",
price: <BigDecimal 0.99>,
formatted_price: "$0.99",
price_locale: <NSLocale>,
downloadable: false,
download_content_lengths: <?>, # TODO: ?
download_content_version: <?>, # TODO: ?
product: <SKProduct>
}
end
product.purchase do |status, transaction|
case status
when :in_progress
# Usually do nothing, maybe a spinner
when :deferred
# Waiting on a prompt to the user
when :purchased
# Notify the user, update any affected UI elements
when :canceled
# They just canceled, no big deal.
when :error
# Failed to purchase
transaction.error.localizedDescription # => error message
end
end
product.restore do |status, product|
if status == :restored
# Update your UI, notify the user
end
end
end
endStores the product_id for use in the instance methods.
Retrieves the product.
Begins a purchase of the product.
Begins a restoration of the previously purchased product.
Include PM::IAP to add some in-app purchase methods to a screen, app delegate, or other class.
# app/screens/purchase_screen.rb
class PurchaseScreen < PM::Screen
include PM::IAP
def on_load
retrieve_iaps [ "productid1", "productid2" ] do |products, error|
# products looks something like the following
[{
product_id: "productid1",
title: "title",
description: "description",
price: <BigDecimal 0.99>,
formatted_price: "$0.99",
price_locale: <NSLocale>,
downloadable: false,
download_content_lengths: <?>, # TODO: ?
download_content_version: <?>, # TODO: ?
product: <SKProduct>
}, {...}]
end
purchase_iap "productid" do |status, transaction|
case status
when :in_progress
# Usually do nothing, maybe a spinner
when :deferred
# Waiting on a prompt to the user
when :purchased
# Notify the user, update any affected UI elements
when :canceled
# They just canceled, no big deal.
when :error
# Failed to purchase
transaction.error.localizedDescription # => error message
end
end
restore_iaps "productid" do |status, products|
if status == :restored
# Update your UI, notify the user
end
end
end
endRetrieves in-app purchase products in an array of mapped hashes. The callback method should accept products and error.
Prompts the user to login to their Apple ID and complete the purchase. The callback method should accept status and transaction.
The callback method will be called several times with the various statuses in the process. If more than one product_id is provided
the callback method will be called several times per product with the applicable transaction.
Restores a previously purchased IAP to the user (for example if they have upgraded their device). This relies on the Apple ID the user enters at the prompt. Unfortunately, if there is no purchase to restore for the signed-in account, no error message is generated and will fail silently.
Find the Product ID here:
| Contributor | Twitter | | Jamon Holmgren | @jamonholmgren | | Kevin VanGelder | @kevinvangelder |
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Make some specs pass
- Push to the branch (
git iap origin my-new-feature) - Create new Pull Request