We're having lots of fun on Discord! Come and join us! 💬
Docs

Getting Started for Apple

Appwrite is a development platform that provides a powerful API and management console to get your next project up and running quickly.

This tutorial helps you start using Appwrite with your project. Before starting, make sure you have followed the Appwrite installation guide, and you have an Appwrite server instance up and running on your host machine or server.

Create Your First Appwrite Project

Go to your new Appwrite console, and click the icon in the top navigation header or on the 'Create Project' button on your console homepage. Choose a name for your project and click create to get started.

Add your Apple Platform

To init your SDK and start interacting with Appwrite services, you need to add a new Apple platform to your project. To add a new platform, go to your Appwrite console, choose the project you created in the step before, and click the 'Add Platform' button. Only API requests initiated from platforms added to your Appwrite project will be accepted. This prevents unauthorized apps from accessing your Appwrite project.

From the options, choose to add a new Apple platform, select the iOS, macOS, watchOS or tvOS tab and add your app name and bundle identifier, Your bundle identifier can be found at the top of the General tab in your project settings, or in your Info.plist file. By registering your new app platform, you are allowing your app to communicate with the Appwrite API.

Get Appwrite Apple SDK

Using Xcode

  1. Select File > Add Packages
  2. Search for the Appwrite SDK with the URL https://github.com/appwrite/sdk-for-apple
  3. In the right panel, select your target project and add your desired version rules
  4. Select Add Package and wait for package resolution to complete
  5. Make sure the Appwrite package product is checked and select Add Package again

Using Swift Packages

Add the following to your Package.swift file:

dependencies: [
    .package(
        name: "Appwrite",
        url: "https://github.com/appwrite/sdk-for-swift",
        .exact("1.2.0")
    )
]')

Then add the dependency to your target:

targets: [
    .target(
        name: "[YOUR_TARGET]",
        dependencies: [
            "Appwrite"
        ]
    )
]

OAuth Callback

In order to capture the Appwrite OAuth callback url, the following URL scheme needs to added to your `Info.plist`

<key>CFBundleURLTypes</key>
<array>
<dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string>io.appwrite</string>
    <key>CFBundleURLSchemes</key>
    <array>
        <string>appwrite-callback-[PROJECT_ID]</string>
    </array>
</dict>
</array>

If you're using UIKit, you'll also need to add a hook to your SceneDelegate.swift file to ensure cookies work correctly.


func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    guard let url = URLContexts.first?.url,
        url.absoluteString.contains("appwrite-callback") else {
        return
    }
    WebAuthComponent.handleIncomingCookie(from: url)
}

Init your SDK

Initialize your SDK code with your project ID, which can be found in your project settings page.

import Appwrite
import AppwriteModels

let client = Client()
    .setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
    .setProject("5df5acd0d48c2")                // Your project ID
    .setSelfSigned(status: true)                // For self signed certificates, only use for development

Before sending any API calls to your new Appwrite project, make sure your device or emulator has network access to your Appwrite project's hostname or IP address.

When connecting to a local self-hosted Appwrite project from an emulator or a mobile device, you should use the private IP of the device running your Appwrite project as the hostname of the endpoint instead of localhost. You can also use a service like ngrok to proxy the Appwrite server.

Make Your First Request

After your SDK configuration is set, access any of the Appwrite services and choose any request to send. Complete documentation for any service method you would like to use can be found in your SDK documentation or in the API References section.

// Register User
let account = Account(client)

let user = try await account.create(
    userId: ID.unique(),
    email: "email@example.com",
    password: "password"
)

Listen to Changes

To listen to changes in realtime from Appwrite, subscribe to a variety of channels and receive updates within milliseconds. Full documentation for Realtime can be found here.

// Subscribe to files channel
let realtime = Realtime(client)

let subscription = realtime.subscribe(channels: ["files"]) { message in
    if (message.events!.contains("buckets.*.files.*.create")) {
        // Log when a new file is uploaded
        print(String(describing: message.payload))
    }
}

If you're using macOS, for realtime to function correctly you need to ensure you have both "Outgoing Connections (Client)" and "Incoming Connections (Server)" enabled in your App Sandbox settings in your project file.

Full Example

import Appwrite
import AppwriteModels

let client = Client()
  .setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
  .setProject("5df5acd0d48c2")                // Your project ID
  .setSelfSigned(status: true)                // For self signed certificates, only use for development

// Register User
let account = Account(client)

let user = try await account.create(
    userId: ID.unique(),
    email: "email@example.com",
    password: "password"
)

// Subscribe to files channel
let realtime = Realtime(client)

let subscription = realtime.subscribe(channels: ["files"]) { message in
    if (message.events!.contains("buckets.*.files.*.create")) {
        // Log when a new file is uploaded
        print(String(describing: message.payload))
    }
}

Next Steps

Appwrite has many services and tools to help improve your app and speed up your development. The best way to learn how you can take advantage of them is to explore the different API references docs.