1 decade ago by Patrick
After 2 weeks of trial and error, I finally get the iAP working. I hit a lot of problems so I want people to avoid repeating my stupid mistakes. I am a 46-year-old programmer and I found myself getting a bit slow in everything. I believe you are all better than me, but I hope these should still help to save your precious time a bit. Here we go:
HOW TO MAKE IAP WORKING?
1. Please ensure your BUILD TARGET IOS version is iOS 6.1.
I made a big mistake and got unknown error if I put iOS 5.1
2. Please ensure you TEST your app on a REAL iPhone or iPad, not Simulator.
I spent lot of time troubleshooting but found out that IAP is not supported on iOS Simulator (the information I surfed, correct me if I am wrong)
3. On your TEST machine, ensure your own App Store account is LOGOUT! Otheriwse you will hit another strange error for no reason. Once you logout your own App Store account, you can use the IAP test account.
4. Do not put the IAP codes inside Class because it will crash. (sorry, I am not sure I am technically correct, Dom please correct me if I am wrong. ) I put the codes (basically a copy-and-change from the Ejecta IAP example) in another file, and include it in index.js.
5. As the IAP codes seem do not block, so I use some signal variables to check it status. Well, I think there could be better way to do it. Tell me and thanks a lot.
Here are my codes:
index.js
=============================
jecta.include('lib/game/iap.js');
// Load the game
ejecta.include('lib/impact/impact.js');
ejecta.include('lib/game/main.js');
iap.js
==============================
//
// iap.js - In-App Purchase functions
//
var iap = new Ejecta.IAPManager();
// these variables are only used by iapPurchase, do not write to them, read-only by external codes
var iapPurchaseStarted = false;
var iapPurchaseCompleted = false;
var iapPurchaseSuccess = false;
function iapPurchase( productId )
{
if (iapPurchaseStarted == true)
return;
// initialize the signal variables
iapPurchaseStarted = true;
iapPurchaseCompleted = false;
iapPurchaseSuccess = false;
iap.getProducts( [productId], function(error, products)
{
if( error )
{
console.log( error );
iapPurchaseCompleted = true;
iapPurchaseSuccess = false;
}
else
{
var selectedProduct = null;
for( var i = 0; i < products.length; i++ )
{
console.log(
products[i].id,
products[i].title,
products[i].description,
products[i].price
);
if( products[i].id == productId )
{
selectedProduct = products[i];
}
}
selectedProduct.purchase(1, function(error, transaction)
{
if( error )
{
console.log(error);
iapPurchaseCompleted = true;
iapPurchaseSuccess = false;
}
else
{
// Purchase successful; log some transaction info
console.log(
transaction.productId,
transaction.id,
transaction.receipt
);
iapPurchaseCompleted = true;
iapPurchaseSuccess = true;
}
});
}
});
iapPurchaseStarted = false;
}
main.js
=============================
Inside the main.js, I put the call iapPurchase( p ) inside a state machine of update() block, and make sure it will not be called twice once the user hit the buy button. Then the code will keep watching the signal variables until the iapPurhcaseComplete becomes true. Then the codes will check if iapPurchaseSuccess is true or not and then take necessary steps.
I have not finished my project yet. So I may hit new problems. But this is a big progress or myself after two weeks of trial and error.
Thank you for your attention.
HOW TO MAKE IAP WORKING?
1. Please ensure your BUILD TARGET IOS version is iOS 6.1.
I made a big mistake and got unknown error if I put iOS 5.1
2. Please ensure you TEST your app on a REAL iPhone or iPad, not Simulator.
I spent lot of time troubleshooting but found out that IAP is not supported on iOS Simulator (the information I surfed, correct me if I am wrong)
3. On your TEST machine, ensure your own App Store account is LOGOUT! Otheriwse you will hit another strange error for no reason. Once you logout your own App Store account, you can use the IAP test account.
4. Do not put the IAP codes inside Class because it will crash. (sorry, I am not sure I am technically correct, Dom please correct me if I am wrong. ) I put the codes (basically a copy-and-change from the Ejecta IAP example) in another file, and include it in index.js.
5. As the IAP codes seem do not block, so I use some signal variables to check it status. Well, I think there could be better way to do it. Tell me and thanks a lot.
Here are my codes:
index.js
=============================
jecta.include('lib/game/iap.js');
// Load the game
ejecta.include('lib/impact/impact.js');
ejecta.include('lib/game/main.js');
iap.js
==============================
//
// iap.js - In-App Purchase functions
//
var iap = new Ejecta.IAPManager();
// these variables are only used by iapPurchase, do not write to them, read-only by external codes
var iapPurchaseStarted = false;
var iapPurchaseCompleted = false;
var iapPurchaseSuccess = false;
function iapPurchase( productId )
{
if (iapPurchaseStarted == true)
return;
// initialize the signal variables
iapPurchaseStarted = true;
iapPurchaseCompleted = false;
iapPurchaseSuccess = false;
iap.getProducts( [productId], function(error, products)
{
if( error )
{
console.log( error );
iapPurchaseCompleted = true;
iapPurchaseSuccess = false;
}
else
{
var selectedProduct = null;
for( var i = 0; i < products.length; i++ )
{
console.log(
products[i].id,
products[i].title,
products[i].description,
products[i].price
);
if( products[i].id == productId )
{
selectedProduct = products[i];
}
}
selectedProduct.purchase(1, function(error, transaction)
{
if( error )
{
console.log(error);
iapPurchaseCompleted = true;
iapPurchaseSuccess = false;
}
else
{
// Purchase successful; log some transaction info
console.log(
transaction.productId,
transaction.id,
transaction.receipt
);
iapPurchaseCompleted = true;
iapPurchaseSuccess = true;
}
});
}
});
iapPurchaseStarted = false;
}
main.js
=============================
Inside the main.js, I put the call iapPurchase( p ) inside a state machine of update() block, and make sure it will not be called twice once the user hit the buy button. Then the code will keep watching the signal variables until the iapPurhcaseComplete becomes true. Then the codes will check if iapPurchaseSuccess is true or not and then take necessary steps.
I have not finished my project yet. So I may hit new problems. But this is a big progress or myself after two weeks of trial and error.
Thank you for your attention.