首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ionic 4- In-app购买前无法展示产品信息

Ionic 4- In-app购买前无法展示产品信息
EN

Stack Overflow用户
提问于 2020-02-20 20:43:35
回答 1查看 941关注 0票数 3

我有谷歌和遵循在线教程的应用程序内购买。我已经在苹果的平台上完成了所有的设置,现在正在测试应用内购买功能。该函数似乎可以工作,但我现在正在与两个问题作斗争。

首先,我使用store.refresh()和store.get()命令从苹果的服务器获取数据。但是,我只能在按下“购买”按钮后才能看到获取的产品信息。从Xcode控制台,我可以看到已经加载的产品信息,比如价格等,但在我按下购买按钮之前无法在我的查看页面上显示。我错过什么了吗?

其次,如果我在应用内购买了多个产品,如何修改我的代码?我看过一些在线资源,但似乎没有离子和应用程序内购买2工作。

我已经在这个应用程序内购买工作了两个星期,特别是那些在苹果中耗时的设置。在上述方面的任何帮助都将是伟大的和高度感谢!

upgrade.ts文件

代码语言:javascript
复制
 import { NavController, Platform } from '@ionic/angular';
 import { InAppPurchase2, IAPProduct } from '@ionic-native/in-app-purchase-2/ngx';

 @Component({
   selector: 'app-upgrade',
   templateUrl: './upgrade.page.html',
   styleUrls: ['./upgrade.page.scss'],
 })

 export class UpgradePage {

   setupReady = 'Not yet';
   public prod: any = {};

   public product: any = {
       name: 'Upgrade to Premium 12 months',
       appleProductId: 'upgrade_12months',
       googleProductId: 'android.test.purchased'
   };

  constructor(private store: InAppPurchase2,
            public platform: Platform,
             ) {
    this.platform.ready().then(() => {
        this.configurePurchasing();
        this.setupReady = 'Ready';
    });
  }

  ionViewDidEnter() {
    this.prod = this.store.get('upgrade_12months');
  }

configurePurchasing() {
  if (!this.platform.is('cordova')) { return; }
  let productId;
  try {
    if (this.platform.is('ios')) {
      productId = this.product.appleProductId;
    } else if (this.platform.is('android')) {
      productId = this.product.googleProductId;
    }

    // Register Product
    // Set Debug High
    this.store.verbosity = this.store.DEBUG;

    // Register the product with the store
    this.store.register({
        id: 'upgrade_12months',
        alias: 'upgrade_12months',
        type: this.store.PAID_SUBSCRIPTION
    });

    this.registerHandlers(productId);
    this.store.refresh();
    this.prod = this.store.get(productId);

    InAppPurchase2.getPlugin().ready().then((status) => {
      console.log(JSON.stringify(this.store.get(productId)));
      console.log('Store is Ready: ' + JSON.stringify(status));
      console.log('Products: ' + JSON.stringify(this.store.products));
    });

    // Errors On The Specific Product
    this.store.when(productId).error( (error) => {
      alert('An Error Occured' + JSON.stringify(error));
    });
    // Refresh Always
    console.log('Refresh Store');
    this.store.refresh();
  } catch (err) {
    console.log('Error On Store Issues' + JSON.stringify(err));
  }
}

 registerHandlers(productId) {
  // Handlers
  this.store.when(productId).approved( (product: IAPProduct) => {
    alert('Approved!');
    // Purchase was approved
    product.finish();
  });

  this.store.when(productId).registered( (product: IAPProduct) => {
    console.log('Registered: ' + JSON.stringify(product));
    console.log(` Registered2 ${product.owned}`);
  });

  this.store.when(productId).updated( (product: IAPProduct) => {
    console.log('Loaded' + JSON.stringify(product));
  });

  this.store.when(productId).cancelled( (product) => {
    alert('Purchase was Cancelled');
  });

  // Overall Store Error
  this.store.error( (err) => {
    console.log('Store Error ' + JSON.stringify(err));
  });
 }


async purchase() {
  if (!this.platform.is('cordova')) { return; }
  let productId;

  if (this.platform.is('ios')) {
    productId = this.product.appleProductId;
  } else if (this.platform.is('android')) {
    productId = this.product.googleProductId;
  }

  this.registerHandlers(productId);
  try {
    const product = this.store.get(productId);
    console.log('Product Info: ' + JSON.stringify(product));
    this.store.order(productId).then((p) => {
      alert('Purchase Action Detected');
      this.registerHandlers(productId);
    }).catch((e: string) => {
      alert('Error Ordering From Store' + e);
    });
  } catch (err) {
    console.log('Error Ordering ' + JSON.stringify(err));
  }
}

restore() {
    this.store.refresh();
}

upgrade.html

代码语言:javascript
复制
 <ion-content padding>
   <ion-row text-center>
     <ion-col>Status:  <b>{{ this.setupReady }}</b></ion-col>
   </ion-row> 

 <br>
 {{ ' Description: '}} {{ this.prod.description }}
 <br>
 {{ 'Price:' }} {{ this.prod.price }}
 <br>
 <div margin-vertical text-center>

          {{ this.prod.title }}
     <ion-button (click)='purchase()' expand="block">

        {{ ' Buy now - ' }} 
        {{ this.prod.price }}

     </ion-button>
 </div>   

   <ion-button full icon-left color="secondary" (click)="restore()">
       <ion-icon name="refresh"></ion-icon>Restore Purchases
   </ion-button>
 </ion-content>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-19 13:17:23

我最终解决了这个问题,因为可能没有一个关于应用内购买的在线教程提到过这些关键点。

首先,您必须在应用程序启动时立即注册产品和处理程序,即在启动页面上,而不是在向客户显示应用内购买产品的页面上。

其次,只注册一次产品,并且永远不要再取消注册。请勿尝试在其他页面中再次注册产品。

这些应该可以解决你在应用内购买集成方面面临的许多问题。希望这对其他在应用内购买编码上苦苦挣扎的人有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60320247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档