<?php
$platforms = $this->getParam('platforms');
$versions = [];

foreach($platforms['client']['sdks'] as $lang) {
    $key = $lang['key'] ?? '';
    $version = $lang['version'] ?? '';

    if(!empty($key)) {
        $versions[$key] = $version;
    }
}

$appleVersion = $versions['apple'] ?? '';
?>
<p>Appwrite is a development platform that provides a powerful API and management console to get your next project up and running quickly.</p>

<p><a target="_blank" rel="noopener" href="https://cloud.appwrite.io/">Create your first project now</a> and start building on Appwrite Cloud.</p>

<h2><a href="/docs/getting-started-for-apple#addProject" id="addProject">Add your Apple Platform</a></h2>

<p>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 <b>'Add Platform'</b> button. Only API requests initiated from platforms added to your Appwrite project will be accepted. This prevents unauthorized apps from accessing your Appwrite project.</p>

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

<h2><a href="/docs/getting-started-for-apple#getSDK" id="getSDK">Get Appwrite Apple SDK</a></h2>

<h3>Using Xcode</h3>

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

<h3>Using Swift Packages</h3>

<p>Add the following to your Package.swift file: </p>

<div class="ide" data-lang="swift" data-lang-label="Swift">
  <pre class="line-numbers"><code class="prism language-swift" data-prism>dependencies: [
    .package(
        name: "Appwrite",
        url: "https://github.com/appwrite/sdk-for-apple",
        .exact("<?php echo $this->escape($appleVersion); ?>")
    )
]')</code></pre>
</div>

<p>Then add the dependency to your target:</p>

<div class="ide" data-lang="swift" data-lang-label="Swift">
  <pre class="line-numbers"><code class="prism language-swift" data-prism>targets: [
    .target(
        name: "[YOUR_TARGET]",
        dependencies: [
            "Appwrite"
        ]
    )
]</code></pre>
</div>

<h3><a href="/docs/getting-started-for-apple#OAuthCallback" id="OAuthCallback">OAuth Callback</a></h3>

<p>In order to capture the Appwrite OAuth callback url, the following URL scheme needs to added to your <code>Info.plist</code>.</p>

<div class="ide" data-lang="html" data-lang-label="XML">
    <pre class="line-numbers"><code class="prism language-xml" data-prism><?php echo $this->escape('<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>
');?></code></pre>
</div>

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

<div class="ide" data-lang="swift" data-lang-label="Swift">
  <pre class="line-numbers"><code class="prism language-swift" data-prism><?php echo $this->escape('
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)
}
');?></code></pre>
</div>

<h2><a href="/docs/getting-started-for-apple#initSDK" id="initSDK">Init your SDK</a></h2>

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

<div class="ide" data-lang="swift" data-lang-label="Apple SDK">
    <pre class="line-numbers"><code class="prism language-swift" data-prism>import Appwrite
import AppwriteModels

let client = Client()
    .setEndpoint("https://cloud.appwrite.io/v1") // Your Appwrite Endpoint
    .setProject("[PROJECT_ID]")                // Your project ID</code></pre>
</div>

<p>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.</p>

<h2><a href="/docs/getting-started-for-apple#makeRequest" id="makeRequest">Make Your First Request</a></h2>

<p>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.</p>

<div class="ide" data-lang="swift" data-lang-label="Apple SDK">
                    <pre class="line-numbers"><code class="prism language-swift" data-prism>// Register User
let account = Account(client)

let user = try await account.create(
    userId: ID.unique(),
    email: "email@example.com",
    password: "password"
)</code></pre>
</div>

<h2><a href="/docs/getting-started-for-apple#listenToChanges" id="listenToChanges">Listen to Changes</a></h2>

<p>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 <a href="/docs/realtime">here</a>.</p>

<div class="ide" data-lang="swift" data-lang-label="Apple SDK">
  <pre class="line-numbers"><code class="prism language-swift" data-prism>// 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))
    }
}</code></pre>
</div>

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

<h2><a href="/docs/getting-started-for-apple#fullExample" id="fullExample">Full Example</a></h2>
<div class="ide" data-lang="swift" data-lang-label="Apple SDK">
                    <pre class="line-numbers"><code class="prism language-swift" data-prism>import Appwrite
import AppwriteModels

let client = Client()
  .setEndpoint("https://cloud.appwrite.io/v1") // Your Appwrite Endpoint
  .setProject("[PROJECT_ID]")                // Your project ID

// 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))
    }
}</code></pre>
</div>

<h2><a href="/docs/getting-started-for-apple#nextSteps" id="nextSteps">Next Steps</a></h2>

<p>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.</p>