Kentico Kontent Delivery SDK
概述
The Kentico Kontent Delivery SDK is a library used for retrieving content. You can use the SDK as a CocoaPod package or add it manually.
示例应用
该存储库包含一个示例应用,展示了 SDK 的基本用法。它显示从示例项目的内容,该示例项目演示了 Kentico Kontent 的功能和最佳实践。该项目包含 Dancing Goat – 一个假设的咖啡连锁店的营销内容。如果您没有自己的示例项目,任何 Kentico Kontent 订阅的管理员都可以生成一个。
快速入门
1. 添加 pod
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!
target '<Your Target Name>' do
pod 'KenticoKontentDelivery'
end
$ pod install
2. 创建类型对象 - 在本例中,类型对象为 Article
。它代表在 Kentico Kontent 中的内容类型,检索到的内容项基于此类型。此内容类型有三个元素,以下是其 codename: title
(文本元素)、teaser_image
(资产元素)和 post_date
(DateTime 元素)。
import ObjectMapper
import KenticoKontentDelivery
class Article: Mappable {
var title: TextElement?
var asset: AssetElement?
var postDate: DateTimeElement?
required init?(map: Map){
let mapper = MapElement.init(map: map)
title = mapper.map(elementName: "title", elementType: TextElement.self)
asset = mapper.map(elementName: "teaser_image", elementType: AssetElement.self)
postDate = mapper.map(elementName: "post_date", elementType: DateTimeElement.self)
}
func mapping(map: Map) {
}
}
3. 准备 Delivery 客户端
import KenticoKontentDelivery
let client = DeliveryClient.init(projectId: "YOUR_PROJECT_ID")
4. 准备查询
let articlesQueryParameters = QueryBuilder.params().type(article).language("es-ES")
5. 获取并使用内容项
client.getItems(modelType: Article.self, queryParameters: articleQueryParameters) { (isSuccess, itemsResponse, error) in
if isSuccess {
if let articles = itemsResponse?.items {
// use your articles here
}
} else {
if let error = error {
print(error)
}
}
}
安装
CocoaPods
CocoaPods 是用于 Cocoa 项目的依赖项管理器。您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 Kentico Kontent 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'KenticoKontentDelivery'
end
然后,运行以下命令
$ pod install
使用 DeliveryClient
DeliveryClient
类是 SDK 的主要类,用于获取内容。使用此类,您可以从未发布的 Kentico Kontent 项目中检索内容。
要创建类的实例,您需要提供一个 项目 ID
// Initializes an instance of the DeliveryClient client
let client = DeliveryClient.init("975bf280-fd91-488c-994c-2f04416e5ee3")
一旦创建了 DeliveryClient
,您就可以通过在客户端实例上调用方法来开始查询您的项目存储库。详细信息请参阅 基本查询。
预览未发布的内容
要检索未发布的内容,您需要创建一个同时具有项目 ID 和预览 API 键的 DeliveryClient
。每个 Kentico Kontent 项目都有自己的预览 API 键。
import KenticoKontentDelivery
let client = DeliveryClient.init(projectId: "YOUR_PROJECT_ID", previewApiKey:"PREVIEW_API_KEY")
有关更多信息,请参阅 使用 Delivery API 预览未发布的内容。
从受保护项目中获取内容
要从受保护的项目中获取内容,您需要创建一个包含项目 ID 和安全 API 密钥的 DeliveryClient
。
import KenticoKontentDelivery
let client = DeliveryClient.init(projectId: "YOUR_PROJECT_ID", secureApiKey:"SECURE_API_KEY")
有关详细信息,请参阅 保护 Delivery API。
配置重试策略
客户端在遇到错误后可以重试获取项目。默认情况下,重试策略已启用,最大重试次数为 5。您可以在创建 DeliveryClient
时配置最大重试次数。
import KenticoKontentDelivery
let client = DeliveryClient.init(projectId: "YOUR_PROJECT_ID", isRetryEnabled: true, maxRetryAttempts: CUSTOM_MAX_ATTEMPTS_NUMBER)
您还可以禁用重试策略。
import KenticoKontentDelivery
let client = DeliveryClient.init(projectId: "YOUR_PROJECT_ID", isRetryEnabled: false)
获取项目
使用强类型模型
为了接收强类型项目,您需要实现您的项目模型。必须遵守 Mappable
协议并实现映射功能。您可以使用自己的映射或我们提供的强类型元素类型。
- 元素类型映射
import ObjectMapper
class Article: Mappable {
var title: TextElement?
var asset: AssetElement?
var postDate: DateTimeElement?
required init?(map: Map){
let mapper = MapElement.init(map: map)
title = mapper.map(elementName: "title", elementType: TextElement.self)
asset = mapper.map(elementName: "teaser_image", elementType: AssetElement.self)
postDate = mapper.map(elementName: "post_date", elementType: DateTimeElement.self)
}
func mapping(map: Map) {
}
- 自定义映射
import ObjectMapper
public class Cafe: Mappable {
public var city: String?
public var street: String?
public var country: String?
public required init?(map: Map){
}
public func mapping(map: Map) {
city <- map["elements.city.value"]
street <- map["elements.street.value"]
country <- map["elements.country.value"]
}
基本项目查询
一旦您有了 DeliveryClient
实例,您就可以通过在该实例上调用方法来开始查询您的项目存储库。您需要传递您的项目模型和查询。您可以通过两种方式创建查询来列出内容
- 创建自定义字符串查询
let customQuery = "items?system.type=article&order=elements.post_date[desc]"
client.getItems(modelType: Article.self, customQuery: customQuery) { (isSuccess, itemsResponse, error) in ...
- 使用查询参数数组
let coffeesQueryParameters = QueryBuilder.params().type(contentType).language("es-ES")
client.getItems(modelType: Coffee.self, queryParameters: coffeesQueryParameters) { (isSuccess, itemsResponse, error) in ...
然后您可以在完成处理程序中使用您获得的项目
// Retrieves a list of all content items of certain type
let coffeesQueryParameters = QueryBuilder.params().type("coffee")
client.getItems(modelType: Coffee.self, queryParameters: coffeesQueryParameters) { (isSuccess, itemsResponse, error) in
if isSuccess {
if let coffees = itemsResponse?.items {
// Use your items here
}
} else {
if let error = error {
print(error)
}
}
您还可以检索单个项目
// Retrieves a single content item
let client = DeliveryClient.init(projectId: "YOUR_PROJECT_ID")
client.getItem(modelType: Cafe.self, itemName: "boston") { (isSuccess, deliveryItem, error) in
if isSuccess {
if let cafe = deliveryItem.item {
// Use your item here
}
} else {
if let error = error {
print(error)
}
}
}
获取关联项
您可以从 itemResponse
或 itemsResponse
对象中获取关联内容项
let client = DeliveryClient.init(projectId: "YOUR_PROJECT_ID")
client.getItem(modelType: Article.self, itemName: "on_roasts", completionHandler: { (isSuccess, itemResponse, error) in
if isSuccess {
for articleCodeName in (itemResponse?.item?.relatedArticles?.value)! {
let relatedArticle = itemResponse?.getLinkedItems(codename: articleCodeName, type: Article.self)
}
}
}
获取内容类型
获取单个内容类型
client.getContentType(name: "coffee", completionHandler: { (isSuccess, contentType, error) in
if !isSuccess {
fail("Response is not successful. Error: \(String(describing: error))")
}
if let type = contentType {
// use content type here
}
})
获取多个内容类型
client.getContentTypes(skip: 2, limit: 4, completionHandler: { (isSuccess, contentTypesResponse, error) in
if !isSuccess {
fail("Response is not successful. Error: \(String(describing: error))")
}
if let response = contentTypesResponse {
// use content types here
}
})
获取分类体系
获取分类组
let client = DeliveryClient.init(projectId: "YOUR_PROJECT_ID")
client.getTaxonomyGroup(taxonomyGroupName: "personas", completionHandler: { (isSuccess, deliveryItem, error) in
if isSuccess {
if let taxonomyGroup = deliveryItems.item {
// use your taxonomy group here
}
} else {
if let error = error {
print(error)
}
}
})
获取所有分类体系
let client = DeliveryClient.init(projectId: "YOUR_PROJECT_ID")
client.getTaxonomies(completionHandler: { (isSuccess, deliveryItems, error) in
if isSuccess {
if let taxonomies = deliveryItems?.items {
// use your taxonomies here
}
} else {
if let error = error {
print(error)
}
}
})
使用图像转换
Kentico Kontent 支持通过使用 URL 参数进行图像转换。提供了一个助手类以方便地创建 URL。
但是,在使用助手类之前,请参阅我们的图像转换文档以了解参数的限制,因为助手类不验证输入参数。
以下是一个示例用法
let originalUrl = "https://example.com/sample-image.jpg"
let transformedUrl = ImageUrlBuilder(baseUrl: originalUrl)
.withWidth(300)
.withHeight(500)
.withFitMode(.Clip)
.withDpr(3)
.withBackGroundColor(rgbColor: 0x330FAA)
.withFormat(.png)
.url
// transformedUrl = https://example.com/sample-image.jpg?w=300&h=500&fit=clip&dpr=3.0&bg=330FAA&fm=png
本地开发
要在本地上运行带有示例应用程序的 SDK,请按以下步骤操作。
- 下载仓库。
- 在终端导航到 /Example。
- 运行
pod install
(必须安装 Cocoapods)。 - 在 XCode 中打开
KenticoKontentDelivery.xcworkspace
。 - 运行。
调试
如果您想从客户端查看调试信息,则设置客户端的 enableDebugLogging
属性。
let deliveryClient = DeliveryClient.init(projectId: "YOUR_PROJECT_ID", enableDebugLogging = true)
发布 Cocoapod 包的新版本
- 更新跟踪头
X-KC-SDKID
。 - 在
.podspec
中更新s.version
。 - 使用新标签创建新版本。
- GitHub Actions 自动构建和发布 COCOAPods 的新版本,当添加新标签时。
文档
您可以在这里找到完整的 API 参考文档。
更新生成的文档
我们使用Jazzy,这是一个用于生成Swift文档的命令行工具。要更新文档,请执行以下步骤:
- 安装Jazzy
[sudo] gem install jazzy
- 从存储库的根目录运行
jazzy
。 - 提交来自/Docs目录的更改。
更多信息
要获取更多开发资源,请访问Kentico Kontent教程,网址为https://docs.kontent.ai/tutorials/develop-apps。
反馈与贡献
查看贡献页面,了解提交问题、开始讨论和开始贡献的最佳位置。
许可
Kontento Delivery SDK以MIT许可提供。有关更多信息,请参阅LICENSE文件。