A react-native wrapper for handling in-app purchases.
-
You need an Apple Developer account to use in-app purchases.
-
You have to set up your in-app purchases in iTunes Connect first. Follow this tutorial for an easy explanation.
-
You have to test your in-app purchases on a real device, in-app purchases will always fail on the Simulator.
-
Run
npm install react-native-in-app-utils --save
. -
Open your project in XCode, right click on
Libraries
, clickAdd Files to "Your Project Name"
and addInAppUtils.xcodeproj
. (situated innode_modules/react-native-in-app-utils
) (This then this, just with InAppUtils). -
Link
libInAppUtils.a
with your Libararies. To do that, click on your project folder, selectBuild Phases
in the top bar, scroll toLink Binary with Libraries
, press the+
at the very bottom and addlibInAppUtils.a
from thenode_modules/react-native-in-app-utils/InAppUtils
folder. (Screenshot). -
Whenever you want to use it within React code now you just have to do:
var InAppUtils = require('NativeModules').InAppUtils;
You have to load the products first to get the correctly internationalized name and price in the correct currency.
var products = [
'com.xyz.abc',
];
InAppUtils.loadProducts(products, (error, products) => {
//update store here.
});
var productIdentifier = 'com.xyz.abc';
InAppUtils.purchaseProduct(productIdentifier, (error, identifier) => {
if(identifier) {
//unlock store here.
}
});
InAppUtils.restorePurchases((error, products)=> {
if(error) {
AlertIOS.alert('itunes Error', 'Could not connect to itunes store.');
} else {
AlertIOS.alert('Restore Successful', 'Successfully restores all your purchases.');
//unlock store here again.
}
});
To test your in-app purchases, you have to run the app on an actual device. Using the iOS Simulator, they will always fail.
-
Set up a test account ("Sandbox Tester") in iTunes Connect. See the official documentation here.
-
Run your app on an actual iOS device. To do so, first run the react-native server on the local network instead of localhost. Then connect your iDevice to your Mac via USB and select it from the list of available devices and simulators in the very top bar. (Next to the build and stop buttons)
-
Open the app and buy something with your Sandbox Tester Apple Account!