deandrehaijiel
deandrehaijiel

Reputation: 153

flutter buying in app subscription crashes iOS

I am trying to integrate in app subscriptions into my app, I am using the in_app_purchase package https://pub.dev/packages/in_app_purchase. I followed the tutorial here: https://www.youtube.com/watch?v=NWbkKH-2xcQ. Everything is working just fine in Android. However, every time I call the _buyProduct method in iOS, the app crashes but the payment sheet pops up in the home screen. Is anyone facing the same thing or does anyone have any advice? TIA.

  Future<void> _initialize() async {
    // Check availability of In App Purchases
    _available = await _iap.isAvailable();

    if (_available) {
      await _getProducts();
      await _getPastPurchases();
      _verifyPurchase();

      // Listen to new purchases
      _subscription = _iap.purchaseStream.listen((data) {
        // Create a temporary list to store the new purchases
        List<PurchaseDetails> newPurchases = List.from(data);

        // Update the UI after the iteration has completed
        setState(() {
          _purchases.addAll(newPurchases);
          _verifyPurchase();
        });
      });
    }
  }

  // Validate purchase

  // Get all products available for sale
  Future<void> _getProducts() async {
    Set<String> ids = _productIDs.toSet();
    ProductDetailsResponse response = await _iap.queryProductDetails(ids);

    setState(() {
      _products = response.productDetails;
    });
  }

  // Gets past purchases
  Future<void> _getPastPurchases() async {
    // Listen to purchase updates
    _iap.purchaseStream.listen((purchases) {
      List<PurchaseDetails> newPurchases = [];

      // Iterate over the purchases and filter out the iOS purchases if needed
      for (PurchaseDetails purchase in purchases) {
        if (Platform.isIOS) {
          _iap.completePurchase(purchase);
        }
        newPurchases.add(purchase); // Add the purchase to the temporary list
      }

      // Update the _purchases list after the iteration has completed
      setState(() {
        _purchases.addAll(newPurchases);
      });
    });
  }

  // Returns purchase of specific product ID
  PurchaseDetails? _hasPurchased(List<String> productIDs) {
    try {
      return _purchases
          .firstWhere((purchase) => productIDs.contains(purchase.productID));
    } catch (e) {
      return null;
    }
  }

  // Your own business logic to setup a consumable
  void _verifyPurchase() {
    PurchaseDetails? purchase = _hasPurchased(_productIDs);

    if (purchase != null && purchase.status == PurchaseStatus.purchased) {
      updateSubscriptionTypeData(_users, purchase.productID);
    } else {
      updateSubscriptionTypeData(_users, '');
    }
  }

  // Purchase a product
  void _buyProduct(ProductDetails prod) {
    final PurchaseParam purchaseParam = PurchaseParam(productDetails: prod);
    _iap.buyNonConsumable(purchaseParam: purchaseParam); // For subscriptions
  }

Relevant Problem Report

3   StoreKit                            0x7ff8175f6c7d -[SKPaymentQueue finishTransaction:] + 361
4   in_app_purchase_storekit               0x10eacdd5d -[FIAPaymentQueueHandler finishTransaction:] + 93 (FIAPaymentQueueHandler.m:151)
5   in_app_purchase_storekit               0x10eadfd83 InAppPurchasePlugin.finishTransactionFinishMap(_:error:) + 2659 (InAppPurchasePlugin.swift:248)
6   in_app_purchase_storekit               0x10eae00a2 @objc InAppPurchasePlugin.finishTransactionFinishMap(_:error:) + 98
7   in_app_purchase_storekit               0x10ead9926 __SetUpInAppPurchaseAPI_block_invoke.456 + 150 (messages.g.m:705)

Upvotes: 0

Views: 217

Answers (1)

Med Oussema Zaier
Med Oussema Zaier

Reputation: 51

I Solved this by:

  1. Updating in_app_purchase package
  2. running flutter clean
  3. running flutter build ios --release --no-tree-shake-icons

Upvotes: 0

Related Questions