Created
February 28, 2013 18:31
-
-
Save fengyuanyang/5058956 to your computer and use it in GitHub Desktop.
IOS 商店使用範例
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local widget = require ("widget") -- build #1034 之前適用 | |
local store = require("store") -- build #261 後適用 | |
local validProducts, invalidProducts = {}, {} -- 接收有效和無效產品用 | |
------------------------------------------------------------------------------- | |
-- 處理從商店取得的產品資訊,對每個產品建立按鈕 | |
------------------------------------------------------------------------------- | |
function unpackValidProducts() | |
-- 建立購買按鈕 | |
function newBuyButton (index) | |
local buyThis = function ( product ) | |
-- 判斷是否能夠購買 | |
if store.canMakePurchases then | |
store.purchase( {product} ) | |
else | |
native.showAlert("無法購買,稍後再試!", | |
{ "OK" } ) | |
end | |
end | |
-- myButton購買呼叫 | |
function buyThis_closure ( index ) | |
return function ( event ) | |
buyThis (validProducts[index].productIdentifier) | |
return true | |
end | |
end | |
local myButton = widget.newButton{ | |
left = display.contentWidth*.8, | |
top = display.contentHeight*.5, | |
yOffset = -1, | |
label = validProducts[index].title .. " " ..string.format("%.2f", validProducts[index].price), | |
default = "buttonBuy.png", | |
over = "buttonBuyDown.png", | |
fontSize = 14, | |
width = display.contentWidth*.4, height = display.contentWidth*.1, | |
onRelease = buyThis_closure (index) | |
} | |
return myButton | |
end | |
if not validProducts then | |
native.showAlert( "In App features not available", "initStore() failed", { "OK" } ) | |
else | |
-- 列出有效產品按鈕 | |
for i=1, #validProducts do | |
local myButton = newBuyButton(i) | |
myButton.x = display.contentWidth - myButton.width - 5 | |
myButton.y = i * 5 + (2 * i - 1) * myButton.height / 2 | |
end | |
-- 列出無效產品 | |
for i=1, #invalidProducts do | |
native.showAlert( "Item " .. invalidProducts[i] .. " is invalid.", | |
{ "OK" } ) | |
end | |
end | |
end | |
------------------------------------------------------------------------------- | |
-- 取得產品資訊,呼叫 store.loadProducts() 後被喚醒 | |
------------------------------------------------------------------------------- | |
function loadProductsCallback( event ) | |
validProducts = event.products | |
invalidProducts = event.invalidProducts | |
unpackValidProducts () | |
end | |
------------------------------------------------------------------------------- | |
-- 專門處理交易的事件,在store.init()裡做設置 | |
------------------------------------------------------------------------------- | |
function transactionCallback( event ) | |
if event.transaction.state == "purchased" then | |
native.showAlert("state",event.transaction.state,{"ok"}) | |
native.showAlert("",event.transaction.productIdentifier,{"ok"}) | |
elseif event.transaction.state == "restored" then | |
native.showAlert("restore","restored",{"ok"}) | |
elseif event.transaction.state == "cancelled" then | |
native.showAlert("cancelled","Transaction cancelled by user.",{"ok"}) | |
elseif event.transaction.state == "failed" then | |
native.showAlert("failed","Transaction failed, type: ", | |
event.transaction.errorType, event.transaction.errorString,{"ok"}) | |
else | |
native.showAlert("Unknown event","Unknown event",{"ok"}) | |
end | |
-- 告訴商店已完成交易,若有提供下載的資料,下載完後再呼叫 | |
store.finishTransaction( event.transaction ) | |
end | |
-- 產品列表,有列出的才會做讀取 | |
local listOfProducts = | |
{ | |
"coin5", | |
} | |
-- 連結到商店,需設定CallBack | |
store.init (transactionCallback ) | |
store.loadProducts( listOfProducts, loadProductsCallback ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment