Welcome to the documentation for the Contentful Persistence SDK for Objective-C!
Installation
To use the Contentful Persistence SDK in your Objective-C project, follow these steps:
- Open your Xcode project.
- Go to File > Swift Packages > Add Package Dependency.
- Enter the package repository URL:
https://github.com/contentful/contentful-persistence.objc.git
- Select the version you want to use and click Next.
- Choose the target where you want to add the package dependency and click Finish.
Usage
Once you have installed the SDK, you can start using it in your Objective-C project. Here’s how:
Step 1: Import the Required Modules
In the file where you want to use the Contentful Persistence SDK, import the necessary modules:
“`objc
#import
“`
Step 2: Setup the Persistence Store
Before using the SDK, you need to set up the persistence store. This is usually done in your app delegate or a dedicated setup class. Here’s an example:
“`objc
// Swift
// AppDelegate.swift
– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create a configuration object
CDAConfiguration *configuration = [CDAConfiguration defaultConfiguration];
// Create an instance of the persistence store
CDAPersistenceStore *store = [[CDADatabasePersistenceStore alloc] initWithConfiguration:configuration];
// Set the persistence store in the SyncManager
[CDASyncManager sharedInstance].persistenceStore = store;
return YES;
}
“`
Step 3: Fetch and Save Data
Once the persistence store is set up, you can fetch and save data from Contentful. Here’s an example:
“`objc
// Fetch entries
[[CDASyncedSpace sharedInstance].client fetchEntriesWithSuccess:^(NSArray
for (CDAResource *entry in entries) {
// Process the entry data
}
} failure:^(CDAResponse *response, NSError *error) {
// Handle the failure
}];
// Save entries
CDAClient* client = [CDAClient new];
[client fetchEntriesWithSuccess:^(CDAResponse *response, CDAArray *array) {
for (CDAResource *entry in array.items) {
// Modify and save the entry
}
} failure:^(CDAResponse *response, NSError *error) {
// Handle the failure
}];
“`
Step 4: Custom Data Mapping (Optional)
If you want to customize how data is mapped to your model objects, you can create your own mapping classes. Here’s an example:
“`objc
// Implement custom mapping class
@interface CustomEntryModelMapping : CDAMapping
@end
@implementation CustomEntryModelMapping
– (Class)targetClass {
return [CustomEntryModel class];
}
– (void)mapData:(NSDictionary *)data ontoPersistedObject:(CustomEntryModel *)object {
// Implement the mapping logic
}
@end
// Register the mapping class
[CDAPersistedEntry registerClass:[CustomEntryModelMapping class] forPersistedClassName:@”CustomEntryModel”];
“`
Step 5: Additional Functionality
The Contentful Persistence SDK for Objective-C provides additional functionality for caching, querying, and syncing data. You can explore these features in the official GitHub repository and the API reference documentation.
Conclusion
Congratulations! You have successfully set up and used the Contentful Persistence SDK for Objective-C in your project. If you have any questions or need further assistance, feel free to reach out to the Contentful support team.