Asset
Asset
Assets are the media files uploaded to a stack, such as images, videos, PDFs and audio files. Use the Asset class to
- read one published asset by its UID
- add fallback-locale content, metadata or the branch to the response
- read the file name, size, type, URL and other fields of the asset as properties
To list assets instead of reading one, call [stack assetLibrary], which returns an AssetLibrary.
| Name | Type | Description |
|---|---|---|
| fileName | NSString * | Read-only. File name of the asset, from the filename key. |
| fileSize | unsigned int | Read-only. File size in bytes, from the file_size key. Known issue: fileSize can hold a wrong value for files of 2 GB or more. Read properties[@"file_size"] and convert it with longLongValue instead. |
| fileType | NSString * | Read-only. MIME type of the file, such as image/png, from the content_type key. |
| uid | NSString * | Read-only. UID of the asset, from the uid key or the UID passed to assetWithUID:. |
| url | NSString * | Read-only. URL of the file, from the url key. |
| tags | NSArray<NSString *> * | Read-only. Tags of the asset, from the tags key. |
| createdAt | NSDate * | Read-only. Creation time. The SDK parses it from the ISO 8601 created_at string. |
| createdBy | NSString * | Read-only. UID of the user who created the asset, from the created_by key. |
| updatedAt | NSDate * | Read-only. Last update time. The SDK parses it from the ISO 8601 updated_at string. |
| updatedBy | NSString * | Read-only. UID of the user who last updated the asset, from the updated_by key. |
| deletedAt | NSDate * | Read-only. Deletion time. Known issue: the SDK fills deletedAt from the deleted_by key instead of deleted_at, so it does not hold the deletion time. Read properties[@"deleted_at"] instead. |
| deletedBy | NSString * | Read-only. UID of the user who deleted the asset, from the deleted_by key. |
| properties | NSDictionary<NSString *, id> * | Read-only. Every key and value of the asset object in the response, including keys that have no typed property. |
| cachePolicy | CachePolicy | CachePolicy that fetch: uses. Known issue: only NETWORK_ONLY and NETWORK_ELSE_CACHE behave as named. CACHE_ONLY, CACHE_ELSE_NETWORK and CACHE_THEN_NETWORK never read the cache. Use NETWORK_ELSE_CACHE for an offline fallback. |
addHeadersWithDictionary:
addHeadersWithDictionary: accepts several request headers for the asset, but stores nothing in this version of the SDK.
| Name | Type | Description |
|---|---|---|
| headers (required) | NSDictionary<NSString *, NSString *> * | Header names mapped to the values to send. |
Returns: void. Nothing.
Validation
- There is no client-side validation.
- addHeadersWithDictionary: cannot cause an API error, because fetch: does not send what it receives.
Behavior
- Has no effect on an Asset. The asset keeps no header store, so addHeadersWithDictionary: stores nothing and fetch: sends no header from it. See the Known issue on the Asset class page.
- The call raises nothing, even with an empty dictionary.
- To send headers with asset requests, call the addHeadersWithDictionary: method of Stack, or its setHeader:forKey: method as the second example below does.
Example
Basic usage: the call stores nothing (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
// Has no effect on an Asset. See the Known issue on the Asset class page.
[asset addHeadersWithDictionary:@{@"branch": @"BRANCH_UID"}];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) {
NSLog(@"Error %ld: %@", (long)error.code, error.userInfo[@"error_message"]);
return;
}
NSLog(@"%@", asset.url);
}];Basic usage: the call stores nothing (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset(withUID: "ASSET_UID")
// Has no effect on an Asset. See the Known issue on the Asset class page.
asset.addHeaders(with: ["branch": "BRANCH_UID"])
asset.fetch { (type, error) in
if let error = error as NSError? {
print("Error \(error.code): \(error.userInfo["error_message"] ?? "")")
return
}
print(asset.url)
}Send a custom header with an asset request (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
// The Stack sends this header with every request it makes
[stack setHeader:@"BRANCH_UID" forKey:@"branch"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) { return; }
NSLog(@"%@", asset.url);
}];Send a custom header with an asset request (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
// The Stack sends this header with every request it makes
stack.setHeader("BRANCH_UID", forKey: "branch")
let asset = stack.asset(withUID: "ASSET_UID")
asset.fetch { (type, error) in
guard error == nil else { return }
print(asset.url)
}addParamKey:andValue:
addParamKey:andValue: stores one query parameter that the next fetch: sends with the asset request.
| Name | Type | Description |
|---|---|---|
| key (required) | NSString * | Query parameter name, such as include_dimension. |
| value (required) | NSString * | Query parameter value, sent as a string. |
Returns: void. Nothing. The asset keeps the setting for its next fetch.
Validation
- There is no client-side validation of the key or the value. An unknown key reaches the API unchanged.
- A nil key makes the call do nothing. A nil value with a non-nil key raises NSInvalidArgumentException, because the SDK stores the pair in a dictionary that cannot hold nil.
- An invalid value surfaces in the error argument of the completion block of the next fetch: call.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. addParamKey:andValue: makes no request. The fetch: call that follows sends the pair in the query string.
- A second call with the same key replaces the earlier value.
- fetch: sets environment from the Stack on every request, so it overwrites any value you store under environment.
- Use it for parameters of Get a single asset that Asset has no method for, such as include_dimension, which adds the height and width of an image.
Example
Basic usage: add image dimensions to the response (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
[asset addParamKey:@"include_dimension" andValue:@"true"];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) {
NSLog(@"Error %ld: %@", (long)error.code, error.userInfo[@"error_message"]);
return;
}
NSLog(@"%@", asset.properties);
}];Basic usage: add image dimensions to the response (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset(withUID: "ASSET_UID")
asset.addParamKey("include_dimension", andValue: "true")
asset.fetch { (type, error) in
if let error = error as NSError? {
print("Error \(error.code): \(error.userInfo["error_message"] ?? "")")
return
}
print(asset.properties)
}configureWithDictionary:
configureWithDictionary: fills the properties of the asset from a dictionary shaped like the asset object of a Delivery API response.
| Name | Type | Description |
|---|---|---|
| dictionary (required) | NSDictionary<NSString *, id> * | Asset fields keyed by their API names, such as uid and filename. |
Returns: void. Nothing. The asset holds the values from the dictionary.
Validation
- There is no client-side validation. Keys the SDK does not map to a typed property stay only in properties.
- The SDK does not check the type of each value. Pass the types the Delivery API returns, for example a string under filename and an array of strings under tags.
Behavior
- Client-side only. configureWithDictionary: makes no request.
- properties takes every key and value of the dictionary, and drops keys from an earlier call that the new dictionary lacks.
- Each typed property reads one key: uid, filename, file_size, content_type, url, tags, created_at, created_by, updated_at, updated_by and deleted_by. The Properties table on the class page lists the mapping.
- A typed property whose key the new dictionary lacks keeps the value an earlier call gave it.
- The SDK parses created_at and updated_at as ISO 8601 date strings.
- A uid key sets the UID of the asset, so a later fetch: requests that asset. An asset from [stack asset] gets its UID this way.
- fetch: calls configureWithDictionary: with the asset object of the response, so you do not need to call it after a fetch.
Example
Basic usage: build an asset from stored JSON (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack asset];
[asset configureWithDictionary:@{
@"uid": @"ASSET_UID",
@"filename": @"logo.png",
@"content_type": @"image/png",
@"url": @"https://images.contentstack.io/v3/assets/API_KEY/ASSET_UID/logo.png"
}];
NSLog(@"%@ %@ %@", asset.uid, asset.fileName, asset.fileType);Basic usage: build an asset from stored JSON (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset()
asset.configure(with: [
"uid": "ASSET_UID",
"filename": "logo.png",
"content_type": "image/png",
"url": "https://images.contentstack.io/v3/assets/API_KEY/ASSET_UID/logo.png"
])
print(asset.uid, asset.fileName, asset.fileType)Refresh a configured asset from the API (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack asset];
[asset configureWithDictionary:@{@"uid": @"ASSET_UID"}];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) { return; }
NSLog(@"%@", asset.url);
}];Refresh a configured asset from the API (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset()
asset.configure(with: ["uid": "ASSET_UID"])
asset.fetch { (type, error) in
guard error == nil else { return }
print(asset.url)
}fetch:
fetch: reads one published asset by its UID and fills the properties of the asset from the response.
| Name | Type | Description |
|---|---|---|
| callback (required) | void (^)(ResponseType, NSError *) | Runs when the request ends. Receives the ResponseType and an NSError, or nil on success. |
Returns: void. Nothing. The completion block receives the result.
Validation
- There is no client-side validation.
- When the API rejects the request, the completion block receives an NSError whose code is the HTTP status and whose userInfo holds error_message, error_code and errors.
- An asset with no UID, from [stack asset] or assetWithUID:@"", sends (null) in place of the UID in the request path.
- The completion block then receives an error from the API.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- fetch: sends one GET request to /v3/assets/{asset_uid}, the Get a single asset request.
- The request carries the environment of the Stack and every query parameter that an earlier includeFallback, includeMetadata, includeBranch or addParamKey:andValue: call stored on the asset.
- On success, fetch: reads the asset object of the response into the properties of the asset before it runs the completion block. The block itself receives no asset, so read the properties of the object you called fetch: on.
- On failure, the properties keep the values they held before the call.
- The cachePolicy property of the asset decides whether fetch: reads the cache. The default is NETWORK_ONLY.
Example
Basic usage: read an asset by UID (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) {
NSLog(@"Error %ld: %@", (long)error.code, error.userInfo[@"error_message"]);
return;
}
NSLog(@"%@ %u bytes %@", asset.fileName, asset.fileSize, asset.url);
}];Basic usage: read an asset by UID (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset(withUID: "ASSET_UID")
asset.fetch { (type, error) in
if let error = error as NSError? {
print("Error \(error.code): \(error.userInfo["error_message"] ?? "")")
return
}
print("\(asset.fileName) \(asset.fileSize) bytes \(asset.url)")
}Offline fallback: read from the cache when the network fails (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
asset.cachePolicy = NETWORK_ELSE_CACHE;
[asset fetch:^(ResponseType type, NSError *error) {
if (error) { return; }
NSLog(@"%@ from %@", asset.url, type == CACHE ? @"cache" : @"network");
}];Offline fallback: read from the cache when the network fails (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset(withUID: "ASSET_UID")
asset.cachePolicy = .NETWORK_ELSE_CACHE
asset.fetch { (type, error) in
guard error == nil else { return }
print("\(asset.url) from \(type == .CACHE ? "cache" : "network")")
}includeBranch
includeBranch asks the API to add the _branch key, which names the branch that holds the asset, to the response.
Returns: void. Nothing. The asset keeps the setting for its next fetch.
Validation
- There is no client-side validation. includeBranch takes no input.
- Errors that do occur surface in the error argument of the completion block of the next fetch: call.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. includeBranch stores include_branch=true on the asset and makes no request. The fetch: call that follows sends it.
- The API adds the _branch key, which holds the unique ID of the branch. See include_branch in Get a single asset.
- includeBranch does not choose the branch. The branch property of Config sets the branch header that selects it.
- Calling includeBranch twice changes nothing, because both calls write the same value under the same key.
Example
Basic usage: read an asset with its branch (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
[asset includeBranch];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) {
NSLog(@"Error %ld: %@", (long)error.code, error.userInfo[@"error_message"]);
return;
}
NSLog(@"%@", asset.properties);
}];Basic usage: read an asset with its branch (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset(withUID: "ASSET_UID")
asset.includeBranch()
asset.fetch { (type, error) in
if let error = error as NSError? {
print("Error \(error.code): \(error.userInfo["error_message"] ?? "")")
return
}
print(asset.properties)
}Read an asset from a specific branch (Objective-C)
#import <Contentstack/Contentstack.h>
Config *config = [[Config alloc] init];
config.branch = @"BRANCH_UID";
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT" config:config];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
[asset includeBranch];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) { return; }
NSLog(@"%@", asset.properties);
}];Read an asset from a specific branch (Swift)
import Contentstack
let config = Config()
config.branch = "BRANCH_UID"
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT", config: config)
let asset = stack.asset(withUID: "ASSET_UID")
asset.includeBranch()
asset.fetch { (type, error) in
guard error == nil else { return }
print(asset.properties)
}includeFallback
includeFallback asks the API to return the asset from the fallback locale when the requested locale has no published version.
Returns: void. Nothing. The asset keeps the setting for its next fetch.
Validation
- There is no client-side validation. includeFallback takes no input.
- Errors that do occur surface in the error argument of the completion block of the next fetch: call.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. includeFallback stores include_fallback=true on the asset and makes no request. The fetch: call that follows sends it.
- The API then returns the published asset details from the fallback locale when the requested locale has none. See include_fallback in Get a single asset.
- Asset has no locale method. Pass the locale through the addParamKey:andValue: method, as the example below does.
- Calling includeFallback twice changes nothing, because both calls write the same value under the same key.
Example
Basic usage: read an asset with fallback-locale content (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
[asset addParamKey:@"locale" andValue:@"fr-fr"];
[asset includeFallback];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) {
NSLog(@"Error %ld: %@", (long)error.code, error.userInfo[@"error_message"]);
return;
}
NSLog(@"%@ %@", asset.fileName, asset.url);
}];Basic usage: read an asset with fallback-locale content (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset(withUID: "ASSET_UID")
asset.addParamKey("locale", andValue: "fr-fr")
asset.includeFallback()
asset.fetch { (type, error) in
if let error = error as NSError? {
print("Error \(error.code): \(error.userInfo["error_message"] ?? "")")
return
}
print(asset.fileName, asset.url)
}includeMetadata
includeMetadata asks the API to add the metadata of the asset to the response.
Returns: void. Nothing. The asset keeps the setting for its next fetch.
Validation
- There is no client-side validation. includeMetadata takes no input.
- Errors that do occur surface in the error argument of the completion block of the next fetch: call.
Additional Resource Refer to Delivery API Errors for the full error code list.
Behavior
- Client-side only. includeMetadata stores include_metadata=true on the asset and makes no request. The fetch: call that follows sends it.
- The API returns the metadata under the _metadata key, with one entry per extension UID. See include_metadata in Get a single asset.
- No typed property holds the metadata. fetch: copies every key of the asset object into the properties dictionary, so read the metadata there.
- Calling includeMetadata twice changes nothing, because both calls write the same value under the same key.
Example
Basic usage: read an asset with its metadata (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
[asset includeMetadata];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) {
NSLog(@"Error %ld: %@", (long)error.code, error.userInfo[@"error_message"]);
return;
}
NSLog(@"%@", asset.properties);
}];Basic usage: read an asset with its metadata (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset(withUID: "ASSET_UID")
asset.includeMetadata()
asset.fetch { (type, error) in
if let error = error as NSError? {
print("Error \(error.code): \(error.userInfo["error_message"] ?? "")")
return
}
print(asset.properties)
}removeHeaderForKey:
removeHeaderForKey: accepts the name of a request header to remove from the asset, but changes nothing in this version of the SDK.
| Name | Type | Description |
|---|---|---|
| headerKey (required) | NSString * | Name of the header to remove. |
Returns: void. Nothing.
Validation
- There is no client-side validation.
- removeHeaderForKey: cannot cause an API error, because it changes nothing that fetch: sends.
Behavior
- Has no effect on an Asset. The asset keeps no header store, so there is nothing for removeHeaderForKey: to remove. See the Known issue on the Asset class page.
- removeHeaderForKey: does not remove a header from the Stack. To stop sending a header that you set on the Stack, call the removeHeaderForKey: method of Stack.
- The call raises nothing, even for a header name you never set.
Example
Basic usage: the call removes nothing (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
// Has no effect on an Asset. See the Known issue on the Asset class page.
[asset removeHeaderForKey:@"branch"];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) {
NSLog(@"Error %ld: %@", (long)error.code, error.userInfo[@"error_message"]);
return;
}
NSLog(@"%@", asset.url);
}];Basic usage: the call removes nothing (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset(withUID: "ASSET_UID")
// Has no effect on an Asset. See the Known issue on the Asset class page.
asset.removeHeader(forKey: "branch")
asset.fetch { (type, error) in
if let error = error as NSError? {
print("Error \(error.code): \(error.userInfo["error_message"] ?? "")")
return
}
print(asset.url)
}Stop sending a header set on the Stack (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
[stack setHeader:@"BRANCH_UID" forKey:@"branch"];
// Remove the header from the Stack, which every asset request reads
[stack removeHeaderForKey:@"branch"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) { return; }
NSLog(@"%@", asset.url);
}];Stop sending a header set on the Stack (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
stack.setHeader("BRANCH_UID", forKey: "branch")
// Remove the header from the Stack, which every asset request reads
stack.removeHeader(forKey: "branch")
let asset = stack.asset(withUID: "ASSET_UID")
asset.fetch { (type, error) in
guard error == nil else { return }
print(asset.url)
}setHeader:forKey:
setHeader:forKey: accepts one request header for the asset, but stores nothing in this version of the SDK.
| Name | Type | Description |
|---|---|---|
| headerValue (required) | NSString * | Header value to send. |
| headerKey (required) | NSString * | Header name, such as branch. |
Returns: void. Nothing.
Validation
- There is no client-side validation.
- setHeader:forKey: cannot cause an API error, because fetch: does not send what it receives.
Behavior
- Has no effect on an Asset. The asset keeps no header store, so setHeader:forKey: stores nothing and fetch: sends no header from it. See the Known issue on the Asset class page.
- The call raises nothing, even with a nil value.
- To send a header with asset requests, call the setHeader:forKey: method of Stack, as the second example below does.
Example
Basic usage: the call stores nothing (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
// Has no effect on an Asset. See the Known issue on the Asset class page.
[asset setHeader:@"BRANCH_UID" forKey:@"branch"];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) {
NSLog(@"Error %ld: %@", (long)error.code, error.userInfo[@"error_message"]);
return;
}
NSLog(@"%@", asset.url);
}];Basic usage: the call stores nothing (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
let asset = stack.asset(withUID: "ASSET_UID")
// Has no effect on an Asset. See the Known issue on the Asset class page.
asset.setHeader("BRANCH_UID", forKey: "branch")
asset.fetch { (type, error) in
if let error = error as NSError? {
print("Error \(error.code): \(error.userInfo["error_message"] ?? "")")
return
}
print(asset.url)
}Send a custom header with an asset request (Objective-C)
#import <Contentstack/Contentstack.h>
Stack *stack = [Contentstack stackWithAPIKey:@"API_KEY" accessToken:@"DELIVERY_TOKEN" environmentName:@"ENVIRONMENT"];
// The Stack sends this header with every request it makes
[stack setHeader:@"BRANCH_UID" forKey:@"branch"];
Asset *asset = [stack assetWithUID:@"ASSET_UID"];
[asset fetch:^(ResponseType type, NSError *error) {
if (error) { return; }
NSLog(@"%@", asset.url);
}];Send a custom header with an asset request (Swift)
import Contentstack
let stack = Contentstack.stack(withAPIKey: "API_KEY", accessToken: "DELIVERY_TOKEN", environmentName: "ENVIRONMENT")
// The Stack sends this header with every request it makes
stack.setHeader("BRANCH_UID", forKey: "branch")
let asset = stack.asset(withUID: "ASSET_UID")
asset.fetch { (type, error) in
guard error == nil else { return }
print(asset.url)
}