Strapi iOS
此项目旨在使连接您的应用程序到 Strapi 后端更加容易,这是开源的无头 CMS,前端开发者都爱用,更多信息请见 https://strapi.io/
内容
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。有关使用和安装说明,请访问他们的网站。要将 Strapi-iOS 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它。
pod 'Strapi-iOS'
使用
导入 Strapi-iOS
要使用 Strapi-iOS,您只需导入该软件包
import Strapi_iOS
启动服务
要启动服务,您需要指定 Strapi 实例的 Strapi 主机。在一般情况下,您将在 AppDelegate 类中启动时添加此信息。
import UIKit
import Strapi_iOS
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Setup the shared Strapi instance
let strapi = Strapi.shared
strapi.scheme = "http"
strapi.host = "localhost"
strapi.port = 1337
return true
}
}
内容类型的默认路由
当您在 Strapi 中创建内容类型时,系统会自动为该内容类型生成一系列路由(REST API),如创建记录、读取记录、更新记录和删除记录。听起来耳熟吗?是的,它可以直接实现 CRUD 功能!这是 Strapi 实现的惊人功能之一。所以,Strapi-iOS 包含了所有这些请求,请参见下文。
假设我们有一个名为 Restaurant
的内容类型,其中包含 Name
和 Price
字段。以下是如何与之集成的几个示例。
创建请求 (POST)
为了开始或扩展我们的数据库,我们可以创建一些记录
let strapi = Strapi.shared
let request = CreateRequest(
contentType: "restaurant",
parameters: [
"name": "Super Pizza",
"price": 3
]
)
strapi.exec(request: request, needAuthentication: false) { response in
guard let record = response.data as? [String: Any],
let id = record["id"] as? Int
else {
return
}
print("Created record with id: \(id)")
}
计数请求 (GET)
假设我们想知道这个内容类型中有多少条记录,我们可以进行计数
let strapi = Strapi.shared
let request = CountRequest(contentType: "restaurant")
strapi.exec(request: request, needAuthentication: false) { response in
guard let count = response.data as? Int else {
return
}
print("Total records: \(count)")
}
查询请求(GET)
你现在想去搜索带有一些过滤条件和排序功能的全部餐厅
let strapi = Strapi.shared
let request = QueryRequest(contentType: "restaurant")
request.filter(by: "name", contains: "pizza")
request.filter(by: "price", greaterThanOrEqualTo: 3)
request.sort(by: "price")
strapi.exec(request: request, needAuthentication: false) { response in
guard let list = response.data as? [[String: Any]] else {
return
}
print("Records found: \(list)")
}
获取请求(GET)
有时候我们只有一个记录的 id
,需要获取所有相关信息。为此,我们可以通过id获取该记录
let strapi = Strapi.shared
let request = FetchRequest(
contentType: "restaurant",
id: 10
)
strapi.exec(request: request, needAuthentication: false) { response in
guard let record = response.data as? [String: Any] else {
return
}
print("Data retrieved: \(record)")
}
更新请求(PUT)
如果价格范围不正确怎么办?没问题,我们可以更新记录
let strapi = Strapi.shared
let request = UpdateRequest(
contentType: "restaurant",
id: 10,
parameters: [
"price": 5
]
)
strapi.exec(request: request, needAuthentication: false) { response in
guard let record = response.data as? [String: Any] else {
return
}
print("Updated record: \(record)")
}
销毁请求(DELETE)
哎呀,我真的很喜欢那家餐厅!我很抱歉你要销毁它,但这就是你可以这样做的方法
let strapi = Strapi.shared
let request = DestroyRequest(
contentType: "restaurant",
id: 10
)
strapi.exec(request: request, needAuthentication: false) { response in
guard let record = response.data as? [String: Any] else {
return
}
print("Destroyed record: \(record)")
}
自定义请求(?)
所以你想要创建一个自定义请求,因为你有一个自定义路由或只是想自己来做,这里是如何操作的
let strapi = Strapi.shared
let request = Request(
method: "GET",
contentType: "restaurants", // You can use any route here
parameters: [
"price_eq": 3
]
)
strapi.exec(request: request, needAuthentication: false) { response in
guard let list = response.data as? [[String: Any]] else {
return
}
print("Data retrieved: \(list)")
}
用户 - 权限
Strapi包含一些出色的插件,其中之一是管理用户和权限。这里有我们针对其的一些酷炫方法。
用户注册
目前,Strapi-iOS仅支持本地提供者进行用户注册。要这样做,您可以直接从Strapi实例调用它
let strapi = Strapi.shared
strapi.register(
username: "My name",
email: "[email protected]",
password: "VeryStrongPassword@2020") { response in
guard let record = response.data as? [String: Any] else {
return
}
print("New user: \(record)")
}
登录
在用户验证完成后,您可以轻松登录
let strapi = Strapi.shared
strapi.login(
identifier: "[email protected]",
password: "VeryStrongPassword@2020") { response in
guard let record = response.data as? [String: Any] else {
return
}
print("Logged in user: \(record)")
}
忘记密码
如果您忘记了密码,您可以按照以下步骤请求重置密码的电子邮件
let strapi = Strapi.shared
strapi.forgotPassword(email: "[email protected]") { response in
guard let record = response.data as? [String: Any] else {
return
}
print("Some data: \(record)")
}
重置密码
收到带有重置密码代码的电子邮件后,您可以按照以下步骤重置密码
let strapi = Strapi.shared
strapi.resetPassword(
code: "somerandomcode",
password: "EvenStrongerPassword@2020",
passwordConfirmation: "EvenStrongerPassword@2020") { response in
guard let record = response.data as? [String: Any] else {
return
}
print("Some data: \(record)")
}
发送电子邮件确认
哎,没有收到电子邮件确认?您可以再次使用这个来请求
let strapi = Strapi.shared
strapi.sendEmailConfirmation(email: "[email protected]") { response in
guard let record = response.data as? [String: Any] else {
return
}
print("Some data: \(record)")
}
个人
现在只是想检索自己的数据(用户),同样很简单
let strapi = Strapi.shared
strapi.me { response in
guard let record = response.data as? [String: Any] else {
return
}
print("My data: \(record)")
}
用户会话
当您使用login
方法时,它将自动在Strapi上添加检索到的令牌,并且它将添加到每个设置exec
方法参数为true
的needAuthentication
的请求中。
如果您有一个持久化会话,您可能已经将令牌保存在某处了,所以您只需要将Strapi
实例的token
属性设置,以在每次请求中添加它并设置needAuthentication = true
。
let strapi = Strapi.shared
strapi.token = "Some_Token_Received_On_Login"
上传
另一个很棒的插件是“上传”,您可以将其文件上传到Strapi服务器,并将它们与记录建立关系。例如,上传聊天应用中消息的音频文件真的很简单。
数据上传
让我们上传一个记录的文本文件
let strapi = Strapi.shared
let text = "..."
strapi.upload(
contentType: "about",
id: 1,
field: "terms",
filename: "terms.txt",
mimeType: "text/plain",
fileData: text.data(using: .utf8)!,
needAuthentication: false) { response in
guard let record = response.data as? [String: Any] else {
return
}
print("Some data: \(record)")
}
图片上传
因为我们经常需要进行图片上传,如更新个人资料图片,所以我们为此提供了一个便利的方法
let strapi = Strapi.shared
let image = UIImage(...)
strapi.upload(
contentType: "about",
id: 1,
field: "terms",
image: image,
compressionQuality: 90,
needAuthentication: false) { response in
guard let record = response.data as? [String: Any] else {
return
}
print("Some data: \(record)")
}
错误处理
是的,不幸的是,错误可能发生,但我们可以处理其中的一些。当发生某些非内容错误时,例如服务器返回的500状态
,response
对象将设置一个error
属性。您只需进行检查即可
strapi.exec(request: request, needAuthentication: false) { response in
if let error = response.error {
// Oh no, something went wrong :(
return
}
// Cool, no errors!
}
许可协议
Strapi iOS在MIT许可下发布。查看详细信息请参阅LICENSE
文件。