Fetching Products
Before fetching products from appmate, ensure that you created products by following Creating Products page.
#
Fetching Products from appmateThis page describes how to fetch the products. With the getProducts method of appmate, you will get the active products synced with the App Store Connect.
If you have created some products for your app for the first time, it may take time to get synced products, as Apple has a time delay in activating the products.
After you called getProducts method, the products will be cached in the sdk for 10 minutes. So you will be able to get the response faster than getting directly from the network. If you add a new product and want to see it in your app for testing purposes, you can delete your app's cache. The products will be fetched from the network the next time you open your app. getProducts method returns products in the form of [Product]
. If failed to fetch products resturns a GenericError
.
You can access the list of all products, list of some spesific products or the list of products filtered by product types.
To handle the result of the asynchronous operation, you must specify a completion handler which provides the ([Product]?, GenericError?) -> Void
. Completion handler is called when the query finishes successfully or with failure, as shown in the following code:
#
1. Fetching all products:#
RequestParameter Name | Type |
---|---|
completion | ([Product]?, GenericError?) -> Void |
#
ResponseParameter Name | Type |
---|---|
products | [Product]? |
error | GenericError? |
- Swift
- Objective-C
PurchaseClient.shared.getProducts { products, error in if let products = products { print(products) } else { print(error) }}
[[PurchaseClient shared] getProductsWithCompletion:^(NSArray<Product *> * products, GenericError * error) { if (products != nil) { NSLog(@"%@", products); } else { NSLog(@"%@", error); }}];
#
2. Fetching products by specifying a list of some productIds:When calling getProductsWithIdList, pass a productIdList that specifies a list of product ID strings created in the Creating Products page of our web console.
#
RequestParameter Name | Type |
---|---|
idList | [String] |
completion | ([Product]?, GenericError?) -> Void |
#
ResponseParameter Name | Type |
---|---|
products | [Product]? |
error | GenericError? |
- Swift
- Objective-C
PurchaseClient.shared.getProductsWithIdList(["test_id1", "test_id2"]) { products, error in if let products = products { print(products) } else { print(error) }}
[[PurchaseClient shared] getProductsWithIdList:[NSArray arrayWithObjects:@"test_id1", @"test_id2", nil] completion:^(NSArray<Product *> * products, GenericError * error) { if (products != nil) { NSLog(@"%@", products); } else { NSLog(@"%@", error); }}];
#
3. Fetching products filtered by product types:When calling getProductsWithType, pass a type that specifies the type of products created in the Creating Products page of our web console.
#
RequestParameter Name | Type |
---|---|
type | String |
completion | ([Product]?, GenericError?) -> Void |
#
ResponseParameter Name | Type |
---|---|
products | [Product]? |
error | GenericError? |
- Swift
- Objective-C
PurchaseClient.shared.getProductsWithType("0") { products, error in if let products = products { print(products) } else { print(error) }}
[[PurchaseClient shared] getProductsWithType:@"0" completion:^(NSArray<Product *> * products, GenericError * error) { if (products != nil) { NSLog(@"%@", products); } else { NSLog(@"%@", error); }}];
#
Displaying ProductsYou can download the code of the sample client application from github and examine it.
