アプリ内購入用に次のコードを試しました。CurrentAppSimulator
テスト目的で使用しています。
private async void history_Click(object sender, RoutedEventArgs e)
{
bool OK = true;
// Get the license info
// The next line is commented out for testing.
// licenseInformation = CurrentApp.LicenseInformation;
// The next line is commented out for production/release.
LicenseInformation licenseInformation = CurrentAppSimulator.LicenseInformation;
if (!licenseInformation.ProductLicenses["PremiumFeatures"].IsActive)
{
try
{
// The customer doesn't own this feature, so
// show the purchase dialog.
await CurrentAppSimulator.RequestProductPurchaseAsync("PremiumFeatures", false);
// the in-app purchase was successful
OK = true;
}
catch (Exception)
{
// The in-app purchase was not completed because
// an error occurred.
OK = false;
}
}
else
{
// The customer already owns this feature.
OK = true;
}
if (OK)
{
Frame.Navigate(typeof(HistoryPage));
}
}
history_Click
ただし、 S_OKを選択しても、実行するたびにダイアログがポップアップし続けます。をlicenseInformation.ProductLicenses["PremiumFeatures"].IsActive
購入した後、true に変更されるはずPremiumFeatures
です。
IsActive
おそらく、アプリ内購入が成功したときに、フラグをオンにして、購入ダイアログが再度表示されないようにする必要があると思います。
// the in-app purchase was successful
OK = true;
// Compile error!!!
licenseInformation.ProductLicenses["PremiumFeatures"].IsActive = true;
わかった。IsActive
読み取り専用フィールドのようです。では、購入が成功した場合の正しい対処法を教えてください。
アップデート :
試用版アプリとアプリ内購入のサンプルを見た後、購入が成功した後、次のコードがからに自動的IsActive
に変更されている可能性があることに気付きました。(しかし、なぜそれが機能するのですか?)false
true
private async Task LoadInAppPurchaseProxyFileAsync()
{
StorageFolder proxyDataFolder = await Package.Current.InstalledLocation.GetFolderAsync("data");
StorageFile proxyFile = await proxyDataFolder.GetFileAsync("in-app-purchase.xml");
licenseChangeHandler = new LicenseChangedEventHandler(InAppPurchaseRefreshScenario);
CurrentAppSimulator.LicenseInformation.LicenseChanged += licenseChangeHandler;
await CurrentAppSimulator.ReloadSimulatorAsync(proxyFile);
// setup application upsell message
ListingInformation listing = await CurrentAppSimulator.LoadListingInformationAsync();
var product1 = listing.ProductListings["product1"];
var product2 = listing.ProductListings["product2"];
Product1SellMessage.Text = "You can buy " + product1.Name + " for: " + product1.FormattedPrice + ".";
Product2SellMessage.Text = "You can buy " + product2.Name + " for: " + product2.FormattedPrice + ".";
}
ただし、「購入済み」の状態は永続ではありません。アプリを閉じて再度起動すると、「プレミアム機能」は「まだ購入されていません」と見なされます。
この動作を永続化するにはどうすればよいでしょうか?