In-App Billing: Inventory not correct; makes user purchase again

笑着哭i 提交于 2019-12-04 11:27:01

By closing your application you delete all variables, including mIsPremium.

Therefor you need to save the value of mIsPremium somewhere and load it when you again open the application.

I suggest you to use SharedPreferences for this task.

That's how you could do this:

In the IabHelper.OnIabPurchaseFinishedListener you need to save your preference:

if (purchase.getSku().equals(PREM_SKU)) {
        // bought the premium upgrade!
        Log.d(TAG, "Purchase is premium upgrade. Congratulating user.");
        alert("Thank you for upgrading to premium!");
        mIsPremium = true;
        mIsUserPremium = true;
        searchAllowed = true;
        SharedPreferences prefs = this.getBaseContext().getSharedPreferences(
                "com.example.yourapp", 0);
        prefs.edit().putBoolean("premium", true).apply;
}

Here you save the premium state "true" to the shared preference "premium" after the user has sucessfully purchased premium.

In the onCreate() method you need to load the preference:

SharedPreferences prefs = this.getBaseContext().getSharedPreferences(
        "com.example.yourapp", 0);
mIsPremium = prefs.getBoolean("premium", false);

Here you load the preference "premium", if this prefernce does not exist yet, it will take "false" als default value.

While the answers provided by both @Philip Sheard and @Al0x were excellent my problem was solved by a review of my code. I entirely removed any reference to the "consume" process. It was a simple fix but I also incorporated the SharedPreferences and all is well. Thanks to all the suggestions - You led me to the correct places!

Another solution is to add a button to your in-app store, that allows the user to restore all his purchases. This procedure is described in the IAB v3 docs, and Apple mandates such an approach.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!