ProMotion-iap
ProMotion-iap is in-app purchase notification support for the popular RubyMotion gem ProMotion. It also works as a stand-alone gem if you are not using ProMotion.
Read the introductory blog post here: http://jamonholmgren.com/iap/
ProMotion-iap was created by Infinite Red, a web and mobile development company based in Portland, OR and San Francisco, CA. While you're welcome to use it, please note that we rely on the community to maintain it. We are happy to merge pull requests and release new versions but are no longer driving primary development.
Installation
gem 'ProMotion-iap'
Usage
PM::IAP::Product Class
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, data|
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
data[:error].localizedDescription # => error message
end
end
product.restore do |status, data|
if status == :restored
# Update your UI, notify the user
# data is a hash with :product_id, :error, and :transaction
else
# Tell the user it failed to restore
end
end
end
end
Product.new(product_id)
Stores the product_id for use in the instance methods.
retrieve(&callback)
Retrieves the product.
purchase(&callback)
Begins a purchase of the product.
restore(&callback)
Begins a restoration of the previously purchased product.
IAP Module
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_iaps [ "productid" ], username: User.current.username 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" ], username: User.current.username do |status, data|
if status == :restored
# Update your UI, notify the user
# This is called once for each product you're trying to restore.
data[:product_id] # => "productid"
elsif status == :error
# Update your UI to show that there was an error.
# This will only be called once, no matter how many products you are trying to restore.
# You'll get an NSError in your `data` hash.
data[:error].localizedDescription # => description of error
end
end
end
end
retrieve_iaps(*
product_ids, &callback)
Retrieves in-app purchase products in an array of mapped hashes. The callback method should accept products
and error
.
purchase_iaps(*
product_ids, &callback)
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.
restore_iaps(*
product_ids, &callback)
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.
Important Note
In order for this library to work correctly, you must provide your, app.identifier
to the Rakefile.
Find the Product ID here:
Authors
| Contributor | Twitter | | Jamon Holmgren | @jamonholmgren | | Kevin VanGelder | @kevinvangelder |
Inspired By
Contributing
- 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