PredictionIO Swift SDK
Swift SDK 为你的 iOS 和 macOS 应用提供了一个便捷的 API,用于在 PredictionIO 事件服务器上记录用户行为,并从 PredictionIO 引擎中检索预测。
要求
- iOS 10+ 或 macOS 10.10+
- Xcode 11+
- Swift 5+
- PredictionIO 0.12.0+
安装
Cocoapods
请安装CocoaPods,这是 Cocoa 项目的依赖管理工具。
$ gem install cocoapods
为了集成 PredictionIO,将以下行添加到你的 Podfile
。
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your target name>' do
pod 'PredictionIO', '~> 3.0'
end
然后运行以下命令。
$ pod install
最后,在使用 SDK 之前将其导入到你的 Swift 文件中。
import PredictionIO
用法
EngineClient
使用 EngineClient
从PredictionIO引擎查询预测。
// Response format of a Recommendation engine.
struct RecommendationResponse: Decodable {
struct ItemScore: Decodable {
let item: String
let score: Double
}
let itemScores: [ItemScore]
}
let engineClient = EngineClient(baseURL: "https://:8000")
let query = [
"user": "1",
"num": 2
]
engineClient.sendQuery(query, responseType: RecommendationResponse.self) { result in
guard let response = result.value else { return }
print(response.itemScores)
}
EventClient
使用 EventClient
向PredictionIO事件服务器发送信息。
let eventClient = EventClient(accessKey: "Access key of the app", baseURL: "https://:7070")
let event = Event(
event: "rate",
entityType: "user",
entityID: "1",
targetEntity: (type: "item", id: "9"),
properties: [
"rating": 5
]
)
eventClient.createEvent(event) { result in
guard let response = result.value else { return }
print(response.eventID)
}
还有其他一些方便的方法来管理用户和物品实体类型。有关详细信息,请参阅API文档。
文档
文档由jazzy生成。要构建文档,请运行
$ jazzy
最新的API文档可在http://minhtule.github.io/PredictionIO-Swift-SDK/index.html找到。
iOS示例应用
请遵循此快速入门指南在本地机器上启动事件服务器并设置推荐引擎。
你还需要
- 在
RatingViewController.swift
中包含你应用的操作密钥。 - 按照第4步中的说明使用脚本导入一些数据;或者您可以使用示例应用程序记录新的评分事件;但是,记得在查询推荐之前重新训练和部署引擎。
- 运行模拟器!
示例应用中有2个屏幕
- 评分:对应快速入门指南中的第4步 收集数据。
- 推荐:对应快速入门指南中的第6步 使用引擎。
Tapster iOS 演示
此外,还可以查看 Tapster iOS,这是一个漫画推荐应用,以查看更广泛的 SDK 集成。
许可证
PredictionIO Swift SDK 采用 Apache License 2.0 许可发布。请参阅LICENSE以获取详细信息。