iOS SDK

Our SDK helps you to have easier access to the Pelcro platform. Written for Swift 4.2, for now.

Installation

We recommend using CocoaPods to integrate the Pelcro SDK with your project.

Add a Podfile (if you don't have one already), by running the following command: pod init.

Step 1

Add the Pelcro pod:

pod 'Pelcro'

Step 2

Run pod install

Troubleshooting

In case Cocoapods doesn't find the Pelcro specs (or it finds older specs) try updating with pod update. After updating, run pod install.

Usage

Import the SDK in your code:

import Pelcro

Initialize the Pelcro with your parameters:

Pelcro.shared.siteID = "Your site ID"
Pelcro.shared.accountID = "Your account ID"
Pelcro.shared.appSharedSecret = "Your Apple app shared secret" 
Pelcro.shared.isSandbox = false // false - for Production, isSandbox by default true - for Development/Staging (in app purchases)
Pelcro.shared.isStaging = false // false - for Production, isStaging by default true - for Development/Staging (endpoints base URL)

Pelcro.shared.isStaging should be configured before initialization of any manager.

Link "How to get app shared secret" - https://www.appypie.com/faqs/how-can-i-get-shared-secret-key-for-in-app-purchase

Site Manager

To get a site please use the following API:

let siteManager = PelcroSiteManager()
siteManager.getSite(with: "Your site ID") { (result: PelcroResult) in
    print(result.data ?? "No data returned")
    print(result.error ?? "No error returned")
}

// Then, you can read Site from the same PelcroSiteManager instance, but keep in mind that getSite calls asynchronously 
let site = siteManager.site

User Manager

To register a user please use the following API:

let userManager = PelcroUserManager()
userManager.register(with: "[email protected]", password: "Your user's password") { (result: PelcroResult) in
    print(result.data ?? "No data returned")
    print(result.error ?? "No error returned")
}

// Then you can read User from the same PelcroUserManager instance, but keep in mind that register calls asynchronously 
let user = userManager.user

To login a user please use the following API:

let userManager = PelcroUserManager()
userManager.login(with: "[email protected]", password: "Your user's password") { (result: PelcroResult) in
    print(result.data ?? "No data returned")
    print(result.error ?? "No error returned")
}

// Then, you can read User from the same PelcroUserManager instance, but keep in mind that login calls asynchronously 
let user = userManager.user

To refresh a user please use the following API:

let userManager = PelcroUserManager()
userManager.refresh(with: "[email protected]", password: "Your user's password") { (result: PelcroResult) in
    print(result.data ?? "No data returned")
    print(result.error ?? "No error returned")
}

// Then, you can read User from the same PelcroUserManager instance. But, keep in mind that refresh calls asynchronously 
let user = userManager.user

To logout a user please use the following API:

let userManager = PelcroUserManager()
userManager.logout()

// Then you can check auth token, it should be nil 
let authToken = Pelcro.shared.authToken

Subscription Manager

To use PelcroSubscriptionManager functionality you should have already set up purchases (subscriptions) in App Store Connect
Please use follow links to tutorials:
https://help.apple.com/app-store-connect/#/devb57be10e7
https://help.apple.com/app-store-connect/#/devae49fb316
https://help.apple.com/app-store-connect/#/dev7e89e149d
https://help.apple.com/app-store-connect/#/dev75708c031
https://help.apple.com/app-store-connect/#/dev06f89ce98
https://www.raywenderlich.com/5456-in-app-purchase-tutorial-getting-started

You should configure Subscriptions in App Store Connect with the same parameters (price, duration, etc) as you have for the Plan in Pelcro.
When you have productID on hand, you should insert it for the appropriate Plan in Pelcro.

To create a subscription please use the following API:

let subscriptionManager = PelcroSubscriptionManager(productsIdentifiers: ["<YOUR APPLE PRODUCT ID>"])
subscriptionManager.createSubscription(with: "<YOUR PLAN ID>", appleProductID: "<YOUR APPLE PRODUCT ID>", couponCode: "<YOUE COUPON CODE IF EXIST>", completion: { (result: PelcroSubscriptionResult) in
    print(result.data ?? "No data returned")
    print(result.error ?? "No error returned")
 })

To cancel a subscription please use the following API (It cancels subscription only on Pelcro side):

let subscriptionManager = PelcroSubscriptionManager(productsIdentifiers: ["<YOUR APPLE PRODUCT ID>"])
subscriptionManager.cancelSubscription(with: "<YOUR SUBSCRIPTION ID>", completion: { (result: PelcroSubscriptionResult) in
    print(result.data ?? "No data returned")
    print(result.error ?? "No error returned")
 })

To reactivate a subscription please use the following API (It reactivates subscription only on Pelcro side):

let subscriptionManager = PelcroSubscriptionManager(productsIdentifiers: ["<YOUR APPLE PRODUCT ID>"])
subscriptionManager.reactivateSubscription(with: "<YOUR SUBSCRIPTION ID>", completion: { (result: PelcroSubscriptionResult) in
    print(result.data ?? "No data returned")
    print(result.error ?? "No error returned")
 })

To check if user subscribed to site please use the following API (It checks we have at least one active subscription on Pelcro side and checks existance of invalid subscriptions from App Store, if yes - cancel them on Pelcro side):

let subscriptionManager = PelcroSubscriptionManager(productsIdentifiers: ["<YOUR APPLE PRODUCT ID>"])
subscriptionManager.isSubscribedToSite(user: "<YOUR USER DATA>", completion: { (result: Bool) in
    print(result)
})

License

The Pelcro iOS SDK is open source and available under the MIT license. See the LICENSE file for more info.


FAQs

Are there social logins in the iOS SDK?
Unfortunately, social logins are not directly supported in the iOS SDKs, but this is available through our Open API endpoint. More information can be found here.

Can a user create an account on the website and use its login in the app? And vice-versa?

Yes, a user can create an account on the website and use its login in the app, and vice-versa. The authentication system is synchronized across the platform, allowing for seamless login experiences between the website and mobile apps.

After an In-App Purchase, How Are the Subscriptions Managed?

To manage subscriptions on Pelcro after an in-app purchase, you should configure subscriptions in App Store Connect with the same parameters (price, duration, etc.) as you have for the plan in Pelcro and use our PelcroSubscriptionManager functionalities to manage the subscription.

Do We See the Subscription in the Pelcro Dashboard?

Yes, if you manage to create a subscription in the App Store with the same plan parameters as used in Pelcro's subscription manager, you will be able to view it in the Pelcro dashboard.

Can We Cancel an in-app subscription in Pelcro Dashboard?

You can cancel subscriptions through the Pelcro dashboard. However, it is essential to note that this action only affects the Pelcro side. The cancellation or reactivation on the App Store must be handled separately.

I canceled my Pelcro subscription through the app, but is it also canceled in the App Store?
No, canceling a subscription through the Pelcro app (using the SDK) only affects your Pelcro account. You'll need to take separate steps to cancel the subscription in the App Store itself. This ensures you don't get charged again by Apple.

Can I see invoices for in-app purchases on the Pelcro dashboard?
Yes, but only if you meet two conditions:

  • The in-app purchase plan matches a Pelcro subscription plan.
  • You create the subscription in Pelcro's Core API.

Do we support Coupon Codes with in-app purchases?

Yes, both our Open API and Core API can be leveraged to create subscriptions with a valid coupon.

  • Open API: The end-user needs to be authenticated first, as this endpoint is protected and requires a valid authentication token.
  • Core API: You will need to know the Customer ID to create the subscription.

Is it best practice to execute Core API requests through a mobile app for validating user existence?
It's generally recommended to use the Open API instead if customers can directly obtain user data on sign-ups.

Can I tell if a subscription came from the app, website, or in-app purchase?
Yes, but you need to set up a process using Pelcro's Core API. Here's how:

  • Listen to the charge.succeeded webhook.
  • Check the origin field to see if the payment came from an Apple in-app purchase (origin will be set to 3).
  • Update the subscription's metadata with the origin information (website, app, etc.).

Why shouldn't I use the Core API directly in my mobile app?

Exposing the Core API key in your app creates a security risk for fraud activities.

How long is a login token valid for in the app?

Login tokens typically last for 60 days after you log in or sign up.

Does the token refresh automatically if I use the app?

No, the token itself doesn't refresh automatically by you using the app. However, there's a separate endpoint called "Refresh customer" that can be used to get a new token.

How can I test login flows with valid and invalid tokens?

  • Valid Token: Use a real token obtained after login.
  • Invalid Token: You can manipulate a valid token (e.g., change some data) to simulate an invalid one.