Using SDKs

SDK Initialization

Initialize the Glassfy SDK"

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

  Glassfy.initialize(apiKey: "YOUR_API_KEY", watcherMode: false)

  [...]

  // optionally login your user
  Glassfy.login(user: "youruser")

}
[...]
ContentView()
    .onAppear() {
        Glassfy.initialize(apiKey: "YOUR_API_KEY", watcherMode: false)
  }
[...]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  [Glassfy initializeWithAPIKey:@"YOU_API_KEY" watcherMode:NO];

  [...]

  // optionally login your user
  [Glassfy loginUser:@"youruser" withCompletion:nil];
}
class App : Application() {
  override fun onCreate() {
    super.onCreate()
    
    Glassfy.initialize(this, "YOUR_API_KEY", false, null)
  }
}
public class App extends Application {
  @Override
  public void onCreate() {
    super.onCreate();
    Glassfy.initialize(this, "YOUR_API_KEY", false, null);
  }
}
try {

  await Glassfy.initialize('YOU_API_KEY',watcherMode: false);

} catch (e) {
  // error
  [...]
}
try {

  await Glassfy.initialize('YOU_API_KEY', false);

} catch (e) {
  // initialization error
}
try {

  await Glassfy.initialize({ apiKey: 'YOU_API_KEY', watcherMode: false });

} catch (e) {
  // initialization error
}

Fetch offerings

Fetch all offers previously created in the dashboard. The offerings completion block returns an Offerings object or an error.

Glassfy.offerings { (offerings, err) in
    if let offering = offerings?["premium"] {
        // display your offering's skus
        for sku in offering.skus {
            // sku.extravars
            // sku.product.localizedTitle
            // sku.product.localizedDescription
            // sku.product.price
        }
    }
}
[Glassfy offeringsWithCompletion:^(GYOfferings *offers, NSError *err) {
    GYOffering *offering = offers[@"premium"];
    if (offering) {
        // display your offering's skus
        for (GYSku *sku in offering.skus) {
            // sku.extravars
            // sku.product.localizedTitle
            // sku.product.localizedDescription
            // sku.product.price
        }
    }
}];
Glassfy.offerings() { offers, err ->
    offers?.all
        ?.firstOrNull { it.offeringId == "premium" }
        ?.also {
            // display your offering's skus
            for (sku in it.skus) {
                // sku.extravars
                // sku.product.title
                // sku.product.description
                // sku.product.price
            }
        }
}
Glassfy.offerings(new OfferingsCallback() {
    @Override
    public void onResult(@Nullable Offerings offers, @Nullable GlassfyError err) {
        Offering offering = null;
        if (offers != null) {
            for (Offering o : offers.getAll()) {
                if (o.getOfferingId().equals("premium")) {
                    offering = o;
                }
            }
        }
        if (offering != null) {
            // display your offering's skus
            for (Sku sku : offering.getSkus()) {
                // sku.getExtravars();
                // sku.getProduct().getTitle();
                // sku.getProduct().getDescription();
                // sku.getProduct().getPrice();
            }
        }
    }
});
try {
    var offerings = await Glassfy.offerings();
    var offering = offerings.all
        ?.singleWhere((offering) => offering.offeringId == 'premium');

    offering?.skus?.forEach((sku) {
        // sku.product.description
        // sku.product.price
    });
} catch (e) {
  // initialization error
  [...]
}
try {
    let offering = Glassfy.offerings.all.find((o) => o.identifier === 'premium');

    offering?.skus.forEach((sku) => {
      // sku.extravars
      // sku.product.description;
      // sku.product.price
    });
} catch (e) {
  [...]
}
try {
    let offering = Glassfy.offerings.all.find((o) => o.identifier === 'premium');

    offering?.skus.forEach((sku) => {
      // sku.extravars
      // sku.product.description;
      // sku.product.price
    });
} catch (e) {
  [...]
}

🚧

If the offering doesn't containing all the SKUs configured in the Glassfy Dashboard, it could be related with App Store or Play Store configuration issues.
Verify you have accepted all the contracts on App Store Connect and all of your products are in a "Ready For Review" state.

Fetch SKU

Fetch the SKU previously created in the dashboard. The sku completion block returns a Sku object or an error.

Glassfy.sku(id: "premium_weekly") { sku, err in
    if let sku = sku {
        // sku.extravars
        // sku.product.localizedTitle
        // sku.product.localizedDescription
        // sku.product.price
    }
}
[Glassfy skuWithId:@"premium_weekly" completion:^(GYSku *sku, NSError *err) {
    if (sku != nil) {
        // sku.extravars
        // sku.product.localizedTitle
        // sku.product.localizedDescription
        // sku.product.price
    }
}];
Glassfy.sku("premium_weekly") { sku, err ->
    sku?.also {
        // sku.extravars
        // sku.product.title
        // sku.product.description
        // sku.product.price
    }
}
Glassfy.sku("premium_weekly", new SkuCallback() {
    @Override
    public void onResult(@Nullable Sku sku, @Nullable GlassfyError err) {
        if (sku != null) {
            // sku.getExtravars();
            // sku.getProduct().getTitle();
            // sku.getProduct().getDescription();
            // sku.getProduct().getPrice();
        }
    }
});
try {
    var sku = await Glassfy.skuWithId('premium_weekly');
    // sku.product.description
    // sku.product.price
} catch (e) {
  // initialization error
  [...]
}
try {
    let sku = Glassfy.skuWithId('premium_weekly');
    // sku.extravars
    // sku.product.description;
    // sku.product.price
} catch (e) {
  [...]
}
try {
    let sku = Glassfy.skuWithId({ identifier: "premium_weekly" });
    // sku.extravars
    // sku.product.description;
    // sku.product.price
} catch (e) {
  [...]
}

Make purchases

To start the purchase process, you need to get a Sku reference using an offer or directly from the identifier. The purchase completion block returns a Transaction or an error.

Glassfy.purchase(sku: premiumSku) { (transaction, e) in
    // update app status accondingly
    if let p = transaction?.permissions["aPermission"] {
        if p.isValid {
            // unlock aFeature
        } else {
            // lock aFeature
        }
    }
}
[Glassfy purchaseSku:premiumSku completion:^(GYTransaction *transaction, NSError *err) {
    GYPermissions *permissions = transaction.permissions;
    if (permissions) {
        GYPermission *p = permissions[@"aPermission"];
        if (p.isValid) {
            // unlock aFeature
        }
        else {
            // lock aFeature
        }
    }
}];
Glassfy.purchase(activity, sku) { transaction, err ->
    // update app status accordingly
    transaction?.permissions
        ?.all
        ?.firstOrNull { it.permissionId == "aPermission" }
        ?.also {
            if (it.isValid) {
                // unlock aFeature
            } else {
                // lock aFeature
            }
        }
}
Glassfy.purchase(activity, sku, new PurchaseCallback() {
    @Override
    public void onResult(@Nullable Transaction t, @Nullable GlassfyError err) {
        // update app status accordingly
        Permission permission = null;
        if (t != null) {
            for (Permission p : t.getPermissions().getAll()) {
                if (p.getPermissionId().equals("aPermission")) {
                    permission = p;
                }
            }
        }
        if (permission != null) {
            if (permission.isValid()) {
              // unlock aFeature
            } else {
              // lock aFeature
            }
        }
    }
});
try {
    var transaction = await Glassfy.purchaseSku(sku);

    var p = transaction.permissions?.all?.singleWhere((permission) => permission.permissionId == 'premium');
    if (p?.isValid==true) {
        // unlock aFeature
    }
    else {
        // lock aFeature
    }
} catch (e) {
  // initialization error
  [...]
}
try {
    const transaction = await Glassfy.purchaseSku(premiumSku );
    const permission = transaction.permissions.all.find((p) => p.permissionId === "aPermission");
    if (permission && permission.isValid) {
        // unlock aFeature
    }
} catch (e) {
  // initialization error
  [...]
}
try {
    const transaction = await Glassfy.purchaseSku({ sku: premiumSku });
    const permission = transaction.permissions.all.find((p) => p.permissionId === "aPermission");
    if (permission && permission.isValid) {
        // unlock aFeature
    }

} catch (e) {
  // initialization error
  [...]
}

Upgrade subscription on Android

To start the purchase process, you need to get a Sku reference using an offer or directly from the identifier and the active Sku using the permissions activeSkus.

val updateSku = SubscriptionUpdate(currentActiveSku)
Glassfy.purchase(activity, upgradeSku, updateSku) { result, error -> 
  [...]
}
var subscriptionUpgrade = GlassfySubscriptionUpdate(currentSkuToUpgrade);
await Glassfy.purchaseSku(newSubscriptionSku,subscriptionUpgrade);

Check permissions

Check all the current user's permission and lock or unlock your app feature accordingly. The permissions completion block returns a permissions object or an error.

Glassfy.permissions { permission, _ in
    guard let permissions = permission?.all else { return }
    
    if let currentPermission = permissions.first(where: { permission in permission.isValid && permission.permissionId == "premium" }),
       let currentSku = currentPermission.accountableSkus.first
    {
        Glassfy.sku(id: currentSku.skuId, store: currentSku.store) { sku, _ in
            if let sku = sku {
                if let sku = sku as? Glassfy.Sku {
                    // you can get iOS product info including the
                    // price and the title.
                    price = sku.product.price
                    description = sku.product.localizedDescription
                    priceLocale = sku.product.priceLocale

                } else if let sku = sku as? Glassfy.SkuPaddle {
                    // you can get Paddle product info including the
                    // price and the title.
                    description = sku.name
                    price = sku.recurringPrice
                    priceLocale = sku.recurringPriceLocale
                }
            }
        }
    }
}
[Glassfy permissionsWithCompletion:^(GYPermissions *permission, NSError *err) {
    NSArray<GYPermission*> *permissions = permission.all;
    if (permissions) {
        for (GYPermission *p in permissions) {
            switch (p.permissionId) {
            case @"aPermission":
                if (p.isValid) {
                    // unlock aFeature
                }
                else {
                    // lock aFeature
                }
                break;
            default:
                NSLog(@"Permission not handled");
                break;
            }
        }
    }
}];
Glassfy.permissions { permission, err ->
    // update app status accordingly
    permission?.all?.forEach {
        when (it.permissionId) {
            "aPermission" ->
                if (it.isValid) {
                    // unlock aFeature
                } else {
                    // lock aFeature
                }
            else -> println("Permission not handled");
        }
    }
}
Glassfy.permissions(new PermissionsCallback() {
    @Override
    public void onResult(@Nullable Permissions permission, @Nullable GlassfyError error) {
        // update app status accondingly
        if (permission != null) {
            for (Permission p: permission.getAll()) {
                switch (p.getPermissionId()) {
                case "aPermission":
                    if (p.isValid()) {
                        // unlock aFeature
                    } else {
                        // lock aFeature
                    }
                    break;
                default:
                    Log.d(TAG, "Permission not handled");
                }
            }
        }
    }
});
try {
var permission = await Glassfy.permissions();
      permission.all?.forEach((p)=> {
        if (p.permissionId == "aPremission") {
          // unlock aFeature
        }
      });
} catch (e) {
  // initialization error
  [...]
}
try {
    const permissions = Glassfy.permissions();
    const permission = transaction.permissions.all.find((p) => p.permissionId === "aPermission");

    if (permission && permission.isValid) {
        // unlock aFeature
    }
} catch (e) {
  // initialization error
  [...]
}
try {
    const permissions = Glassfy.permissions();
    const permission = transaction.permissions.all.find((p) => p.permissionId === "aPermission");
    if (permission && permission.isValid) {
        // unlock aFeature
    }

} catch (e) {
  // initialization error
  [...]
}

Restore purchases

Restoring purchases is a way for a user to restore their in-app purchases and subscriptions.

Glassfy automatically keeps track of purchases but it's still recommended to have a button in your application to invoke the restore purchases code. The restorePurchases completion block returns a Permissions object or an error.

Glassfy.restorePurchases { permissions, error
// update app status accondingly
    if let permissions = permission?.all {
        for p in permissions {
            switch p.permissionId {
            case "aPermission":
                if p.isValid {
                    // unlock aFeature
                } else {
                    // lock aFeature
                }
            default:
                print("Permission not handled");
            }
        }
    }                          
}
[Glassfy restorePurchasesWithCompletion:^(GYPermissions *permission, NSError *err) {
    NSArray<GYPermission*> *permissions = permission.all;
    if (permissions) {
        for (GYPermission *p in permissions) {
            switch (p.permissionId) {
            case @"aPermission":
                if (p.isValid) {
                    // unlock aFeature
                }
                else {
                    // lock aFeature
                }
                break;
            default:
                NSLog(@"Permission not handled");
                break;
            }
        }
    }
}];
Glassfy.restorePurchases()

User properties

Save and retrieve user properties. Read the UserProperties object documentation for more details.

// Save push notification device token
Glassfy.setDeviceToken(remoteDeviceToken) { err in
    // handle err
}

// Save user email
Glassfy.setUserProperty(email: userEmail) { err in
    // handle err
}

// Save extra user properties
Glassfy.setUserProperty(extra: extra) { err in
    // handle err
}

// Fetch user properties
Glassfy.userProperties { properties, err in
    // handle err
    if let p = properties {
        // p.email
        // p.token
        // p.extra
    }
}
// Save push notification device token
[Glassfy setDeviceToken:remoteDeviceToken completion:^(NSError *err) {
    // handle err
}];

// Save user email
[Glassfy setEmailUserProperty:userEmail completion:^(NSError *err) {
    // handle err
}];

// Save extra user properties
[Glassfy setExtraUserProperty:extra completion:^(NSError *err) {
    // handle err
}];

// Fetch user properties
[Glassfy getUserProperties:^(GYUserProperties *p, NSError *err) {
    // handle err
    if (p != nil) {
        // p.email
        // p.token
        // p.extra
    }
}];
// Save push notification device token
await Glassfy.setDeviceToken("token");

// Save user email
await Glassfy.setEmailUserProperty("[email protected]");
Map<String, dynamic> extraProp = {
"property1": "value1",
"property2": "value2",
};

// Save extra user properties
await Glassfy.setExtraUserProperty(extraProp);

// Fetch user properties
var extraProperties = await Glassfy.getExtraUserProperty();
// Save push notification device token
Glassfy.setDeviceToken("mytoken");

// Save extra user properties
Glassfy.setExtraUserProperty(extraProperty);

// Fetch user properties
let extraProperty = Glassfy.getUserProperty();
// Save push notification device token
Glassfy.setDeviceToken({ token: "mytoken" });

// Save extra user properties
Glassfy.setExtraUserProperty({ extra: extraProperty });

// Fetch user properties
let extraProperty = Glassfy.getUserProperty();

Did this page help you?