Contentstack iOS Persistence Library
Contentstack提供iOS Persistence Library,允许您的应用程序在设备的本地存储中存储数据。以下是关于我们iOS Persistence Library的详细指南和有用的资源,以帮助您入门。
先决条件
- 最新的Xcode和Mac OS X
设置和初始化库
您可以使用iOS Persistence Library与CoreData和Realm数据库一起使用。让我们了解如何为您的项目设置这些。
针对CoreData
要为此库设置CoreData,请按照以下步骤操作。
使用 CocoaPods 设置
- 将以下行添加到您的 Podfile 中
pod 'ContentstackPersistenceCoreData'
- 运行 pod install,现在应该已经有了库的最新版本。
导入头文件/模块
#import <ContentstackPersistenceCoreData/ContentstackPersistenceCoreData.h>;
您也可以将其作为模块导入
- Objective-C
@import ContentstackPersistenceCoreData
- Swift
import ContentstackPersistenceCoreData
初始化库
要开始在您的应用程序中使用库,您需要通过提供您的栈详情来初始化它
- Objective-C
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 })
为 Realm
要为此库设置 Realm,请按照以下步骤操作。
使用 CocoaPods 设置
- 将以下行添加到您的 Podfile 中
pod 'ContentstackPersistenceRealm'
- 运行 pod install,现在应该已经有了库的最新版本。
导入头文件/模块
您可以将头文件导入 Objective-C 项目中,如下所示
#import <ContentstackPersistenceRealm/ContentstackPersistenceRealm.h>;
您也可以将其作为模块导入
- Objective-C
@import ContentstackPersistenceRealm
- Swift
import ContentstackPersistenceRealm
初始化库
要在应用程序中使用库,您需要通过提供堆栈详情进行初始化。
- Objective- C
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 })
我们使用iOS持久库创建了一个示例应用程序,它将数据存储在设备的本地存储中。阅读教程,开始使用该示例应用程序。