contentful-management.objc

Objective-C SDK for Contentful's Content Management API.

Contentful is a content management platform for web applications, mobile apps and connected devices. It allows you to create, edit & manage content in the cloud and publish it anywhere via powerful API. Contentful offers tools for managing editorial teams and enabling cooperation between organizations.

Usage

Client

In the beginning the API client instance should be created:

CMAClient* client = [[CMAClient alloc] initWithAccessToken:@"access-token"];

The access token can easily be obtained through the management API documentation.

Spaces

Retrieving all spaces:

[client fetchAllSpacesWithSuccess:^(CDAResponse *response, CDAArray *array) {
    NSLog(@"%@", array);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Retrieving one space by ID:

[client fetchSpaceWithIdentifier:@"blog-space-id" 
success:^(CDAResponse *response, CMASpace *space) {
    NSLog(@"%@", space);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Deleting a space:

[space deleteWithSuccess:^{
    NSLog(@"Delete successful.");
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Creating a space:

[client createSpaceWithName:@"MySpace"
success:^(CDAResponse *response, CMASpace *space) {
    NSLog(@"%@", space);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

or in the context of the organization (if you have multiple organizations within your account):

[client createSpaceWithName:@"MySpace"
inOrganization:organization
success:^(CDAResponse *response, CMASpace *space) {
    NSLog(@"%@", space);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

To retrieve all organizations in your account:

[client fetchOrganizationsWithSuccess:^(CDAResponse *response, CDAArray *array) {
    NSLog(@"%@", array);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Updating a space:

space.name = @"new name";

[space updateWithSuccess:^{
    NSLog(@"%@", space);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Content Types

Retrieving all content types from a space:

[space fetchContentTypesWithSuccess:^(CDAResponse *response, CDAArray *array) {
    NSLog(@"%@", array);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Retrieving one content type by ID from a space:

[space fetchContentTypeWithIdentifier:@"some identifier"
success:^(CDAResponse *response, CMAContentType *type) {
    NSLog(@"%@", type);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Creating a field for a content type:

CMAField* field = [CMAField fieldWithName:@"tags" type:CDAFieldTypeArray];
field.itemType = CDAFieldTypeSymbol;

[contentType addField:field];

or

[contentType addFieldWithName:@"anotherField" type:CDAFieldTypeNumber];

Deleting a field from the content type:

[contentType deleteFieldWithIdentifier:@"some identifier"];

Creating a content type:

[space createContentTypeWithName:@"foobar"
fields:@[ [CMAField fieldWithName:@"field1" type:CDAFieldTypeText],
          [CMAField fieldWithName:@"field2" type:CDAFieldTypeNumber],
          [CMAField fieldWithName:@"field3" type:CDAFieldTypeObject] ]
success:^(CDAResponse *response, CMAContentType *contentType) {
    NSLog(@"%@", contentType);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Deleting a content type:

[contentType deleteWithSuccess:^{
    NSLog(@"Delete successful.");
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Activating or deactivating a content type:

[contentType publishWithSuccess:^{
    NSLog(@"Published successfully.");
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

[contentType unpublishWithSuccess:nil failure:nil];         

Checking if a content type is active:

BOOL result = contentType.isPublished;

Updating a content type:

contentType.name = @"new name";
[contentType updateWithSuccess:^{
    NSLog(@"Updated successfully.");
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Assets

Retrieving all assets from the space:

[space fetchAssetsWithSuccess:^(CDAResponse* response, CDAArray* assets) {
    NSLog(@"%@", assets);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Retrieving an asset by ID:

[space fetchAssetWithIdentifier:@"some identifier" 
success:^(CDAResponse* response, CMAAsset* asset) {
    NSLog(@"%@", asset);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Creating an asset:

[space createAssetWithTitle:@{ @"en-US": @"My Image" }
description:@{ @"en-US": @"My Image Description" }
fileToUpload:@{ @"en-US": @"http://www.example.com/example.jpg" }
success:^(CDAResponse *response, CMAAsset *asset) {
    NSLog(@"%@", asset);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Updating an asset:

asset.title = @"bar";

[asset updateWithSuccess:^{
    NSLog(@"Update successful");
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Deleting an asset:

[asset deleteWithSuccess:^{
    NSLog(@"Delete successful.");
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Archiving or unarchiving an asset:

[asset archiveWithSuccess:nil failure:nil];
[asset unarchiveWithSuccess:nil failure:nil];

Checking if an asset is archived:

BOOL result = asset.isArchived;

Publishing or unpublishing an asset:

[asset publishWithSuccess:nil failure:nil];
[asset unpublishWithSuccess:nil failure:nil];

Checking if an asset is published:

BOOL result = asset.isPublished;

Entries

Retrieving all entries from the space:

[space fetchEntriesWithSuccess:^(CDAResponse* response, CDAArray* entries) {
    NSLog(@"%@", entries);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Retrieving an entry by ID:

[space fetchEntryWithIdentifier:@"some identifier"
success:^(CDAResponse* response, CDAEntry* entry) {
    NSLog(@"%@", entry);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Creating an entry:

[space createEntryOfContentType:contentType
withFields:@{ @"title": @{ @"en-US": @"Mr. President" } }
success:^(CDAResponse *response, CDAEntry *entry) {
    NSLog(@"%@", entry);
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Updating an entry:

[entry setValue:@"bar" forFieldWithName:@"title"];
[entry updateWithSuccess:^{
    NSLog(@"Updated successfully");
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Deleting an entry:

[entry deleteWithSuccess:^{
    NSLog(@"Delete successful.");
} failure:^(CDAResponse *response, NSError *error) {
    NSLog(@"Error: %@", error);
}];

Archiving or unarchiving the entry:

[entry archiveWithSuccess:nil failure:nil];
[entry unarchiveWithSuccess:nil failure:nil];

Checking if the entry is archived:

BOOL result = entry.isArchived;

Publishing or unpublishing the entry:

[entry publishWithSuccess:nil failure:nil];
[entry unpublishWithSuccess:nil failure:nil];

Checking if the entry is published:

BOOL result = entry.isPublish;

Installation

CocoaPods is the dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like the Contentful Delivery API in your projects.

platform :ios, '7.0'
pod 'ContentfulManagementAPI'

This is the easiest way to keep your copy of the Contentful Management API updated.

Unit Tests

The Contentful Management API is fully unit tested. They are using the API token from the environment variable CONTENTFUL_MANAGEMENT_API_ACCESS_TOKEN so you have to provide that.

The tests can be run either from inside Xcode or using cocoapods-testing from the commandline:

$ gem install cocoapods-testing
$ pod lib testing

License

Copyright (c) 2014 Contentful GmbH. See LICENSE for further details.