StrapiSwift is a Swift package that provides a simple and easy way to interact with Strapi APIs. It is built on top of the URLRequest
and Codable
protocols to provide a seamless experience for developers. StrapiSwift is designed to work with Strapi APIs that are built with the default REST API plugin.
You can add StrapiSwift to your Swift project using Swift Package Manager.
-
In Xcode, go to File -> Swift Packages -> Add Package Dependency.
-
Enter the repository URL:
https://github.com/meesakveld/strapi-swift.git
-
Choose the version or branch you want to use.
To configure StrapiSwift, you need to provide the base URL of your Strapi API and the authentication token (if needed).
import SwiftUI
import StrapiSwift
@main
struct SomeNameApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
// —— Configure StrapiSwift ——
Strapi.configure(
baseURL: "https://your-strapi-url.com",
token: "some-token"
)
return true
}
}
The configuration can always be overwritten, to for example use a jwt token after logging in.
After making an request with the token, the one time use token will be replaced with the original configured token.
Strapi.useTokenOnce(token: "some-token")
Filter, sort, populate, fields, pagination, locale and status parameters are supported.
-
// Get all documents try await Strapi.contentManager.collection("gifts").getDocuments(as: [Gift].self) // Get a single document try await Strapi.contentManager.collection("gifts").withDocumentId("a4fsdnjsdf42dsfd").getDocument(as: Gift.self)
-
let data = StrapiRequestBody([ "invitedUserEmail": .string(email), "event": .string(eventDocumentId) ]) try await Strapi.contentManager.collection("event-invites").postData(data, as: EventInvite.self)
-
let data = StrapiRequestBody([ "status": .string("accepted") ]) try await Strapi.contentManager.collection("event-invites").withDocumentId("a4fsdnjsdf42dsfd").putData(data, as: EventInvite.self)
-
try await Strapi.contentManager.collection("event-invites").withDocumentId("a4fsdnjsdf42dsfd").delete()
-
try await Strapi.authentication.local.register(username: "michaelscott", email: "michaelscott@dundermifflin.com", password: "password", as: User.self)
-
try await Strapi.authentication.local.login(identifier: "michaelscott", password: "password", as: User.self)
-
let data: StrapiRequestBody = StrapiRequestBody([ "firstname": .string("Michael"), "lastname": .string("Scott"), ]) try await Strapi.authentication.local.updateProfile(data, userId: 1, as: User.self)
-
// Get the current user try await Strapi.authentication.local.me(as: User.self) // Me with an extended url for custom endpoints let data: StrapiRequestBody = StrapiRequestBody([ "deviceToken": .string("dfsdg53gdfg532GFD6fgds") ]) try await Strapi.authentication.local.me(extendUrl: "/device-token", requestType: .PUT, data: data, as: User.self)
-
try await Strapi.authentication.local.changePassword(currentPassword: "currentPassword", newPassword: "newPassword", as: User.self)
-
try await Strapi.mediaLibrary.files.getFiles(as: [StrapiImage].self)
-
try await Strapi.mediaLibrary.files.withId(1).getFile(as: StrapiImage.self)
-
// Upload image from UIImage try await Strapi.mediaLibrary.files.uploadImage(image: image) // Upload image from URL try await Strapi.mediaLibrary.files.uploadImage(from: "https://picsum.photos/200/300")
-
try await Strapi.mediaLibrary.files.withId(1).delete(as: StrapiImage.self)
If you'd like to contribute, feel free to fork the repository, make your changes, and open a pull request.
StrapiSwift is licensed under the Apache License 2.0. See the LICENSE file for more details.