Contentstack provides iOS Persistence Library that lets your application store data on the device's local storage. This helps you build apps that can work offline too. Given below is a detailed guide and helpful resources to get started with our iOS Persistence Library.
- Latest Xcode and Mac OS X
You can use the iOS Persistence Library with CoreData and Realm databases. Let's understand how to set these up for your project.
To set up this library for CoreData, follow the steps given below.
- Add the following line to your Podfile:
pod 'ContentstackPersistenceCoreData'
- Run pod install, and you should now have the latest version of the library.
#import <ContentstackPersistenceCoreData/ContentstackPersistenceCoreData.h>;
You can also import as a Module:
//Objc
@import ContentstackPersistenceCoreData
//Swift
import ContentstackPersistenceCoreData
To start using the library in your application, you will need to initialize it by providing your stack details:
//Objc
Config *config = [[Config alloc] init];
config.host = @"customcontentstack.io";
Stack *stack = [Contentstack stackWithAPIKey:<APIKey> accessToken:<AccessToken> environmentName:<EnvironmentName> config:config];
CoreDataStore *coreDataStore = [[CoreDataStore alloc] initWithContenxt: <NSManageObjectContext>];
SyncManager *syncManager = [[SyncManager alloc] initWithStack:stack persistance:coreDataStore]
[syncManager sync:{ (percentageComplete, isSyncCompleted, error) in
}];
//Swift
let config = Config()
config.host = @"customcontentstack.io";
let stack : Stack = Contentstack.stack(withAPIKey: <APIKey>, accessToken: <AccessToken>, environmentName: <EnvironmentName>, config:config)
var coreDataStore = CoreDataStore(contenxt: <NSManageObjectContext>)
var syncManager : SyncManager = SyncManager(stack: stack, persistance: coreDataStore)
syncManager.sync({ (percentage, isSynccompleted, error) in
})
To set up this library for Realm, follow the steps given below.
- Add the following line to your Podfile:
pod 'ContentstackPersistenceRealm'
- Run pod install, and you should now have the latest version of the library.
You can import header file in Objective-C project as:
#import <ContentstackPersistenceRealm/ContentstackPersistenceRealm.h>;
You can also import as a Module:
//Objc
@import ContentstackPersistenceRealm
//Swift
import ContentstackPersistenceRealm
To start using the library in your application, you will need to initialize it by providing the stack details:
//Objc
Config *config = [[Config alloc] init];
config.host = @"customcontentstack.io";
Stack *stack = [Contentstack stackWithAPIKey:<APIKey> accessToken:<AccessToken> environmentName:<EnvironmentName> config:config];
RealmStore *realmStore = [[RealmStore alloc] initWithRealm:[[RLMRealm alloc] init]];
SyncManager *syncManager = [[SyncManager alloc] initWithStack:stack persistance:realmStore]
[syncManager sync:{ (percentageComplete, isSyncCompleted, error) in
}];
//Swift
let config = Config()
config.host = @"customcontentstack.io";
let stack : Stack = Contentstack.stack(withAPIKey: <APIKey>, accessToken: <AccessToken>, environmentName: <EnvironmentName>, config:config)
var realmStore = RealmStore(realm: RLMRealm())
var syncManager : SyncManager = SyncManager(stack: stack, persistance: realmStore)
syncManager.sync({ (percentage, isSynccompleted, error) in
})
We have created an example app using iOS Persistence Library that stores data on the device's local storage. Read the tutorial to get started with the example app.