Skip to content

Add support for SPM (Swift Package Manager) #4

@ptmkenny

Description

@ptmkenny

Thank you for this great plugin. I tried to test it in the iOS Simulator. I have subscriptions configured in the App Store, so I wrote a hook like this to fetch them:

/**
 * Copyright 2025 Edo Sensei LLC. All rights reserved.
 */
import { ProductId } from '@enums/ProductId';
import { useQuery } from '@tanstack/react-query';
import {
  Subscriptions,
  type ProductDetailsResponse,
} from '@squareetlabs/capacitor-subscriptions';

type SubscriptionProductDetails = {
  monthly: ProductDetailsResponse;
  annual: ProductDetailsResponse;
};

export function useSubscriptionProductDetails() {
  return useQuery<SubscriptionProductDetails | string>({
    queryKey: ['subscriptionProductDetails'],
    queryFn: async (): Promise<SubscriptionProductDetails | string> => {
      try {
        const [monthlyResult, annualResult] = await Promise.allSettled([
          Subscriptions.getProductDetails({
            productIdentifier: ProductId.SubMonthly,
          }),
          Subscriptions.getProductDetails({
            productIdentifier: ProductId.SubAnnual,
          }),
        ]);

        const monthly =
          monthlyResult.status === 'fulfilled' ? monthlyResult.value : null;
        const annual =
          annualResult.status === 'fulfilled' ? annualResult.value : null;

        console.log('monthly', monthlyResult);
        console.log('annual', annualResult);
        // Check for errors in the responses
        if (monthly?.responseCode !== 0 || annual?.responseCode !== 0) {
          const errors = [];
          if (monthly?.responseCode !== 0) {
            errors.push(
              `Monthly: ${monthly?.responseMessage ?? 'Unknown error'}`,
            );
          }
          if (annual?.responseCode !== 0) {
            errors.push(
              `Annual: ${annual?.responseMessage ?? 'Unknown error'}`,
            );
          }
          const noTranslateErrorMessage = `Failed to fetch products. ${errors.join(' ')}`;
          return noTranslateErrorMessage;
        }

        return {
          monthly,
          annual,
        };
      } catch (error) {
        const noTranslateErrorMessage = `Error fetching subscription products: ${error instanceof Error ? error.message : String(error)}`;
        return noTranslateErrorMessage;
      }
    },
  });
}

I then built my app and tested it in iOS Simulator (iOS 26, iPhone Pro).

console.log() debug says iOS is not supported:

reason: Error: "Subscriptions" plugin is not implemented on ios

code: "UNIMPLEMENTED"

column: 195

data: undefined

line: 1

message: "\"Subscriptions\" plugin is not implemented on ios"

sourceURL: "capacitor://localhost/cap.DpLzyGxs.js"

stack: "C@capacitor://localhost/cap.DpLzyGxs.js:1:195↵X@capacitor://localhost/cap.DpLzyGxs.js:1:1349↵@capacitor://localhost/cap.DpLzyGxs.js:1:1471"

C Prototype

status: "rejected"

Does this mean the simulator is not supported, or am I doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions