Getting started with Appwrite's Apple SDK and UIKit
Tags: ios, swift, tutorial, news
Published: 2021-10-19 12:10:29

One of the major highlights of Appwrite 0.11 is the official support for iOS, macOS, tvOS and watchOS. We've also released a brand-new Apple SDK to go alongside it! 😉

![Appwrite & Swift](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v5gk906j5ngsah46w3px.png)

In this tutorial, we'll learn to set up Appwrite's Apple SDK, interact with Appwrite's Accounts API and also learn to set up OAuth Logins in your App. Let's get started!

> We'll use UIKit in this tutorial, if you're using [SwiftUI](https://developer.apple.com/xcode/swiftui/), check out <a href="https://dev.to/appwrite/getting-started-with-appwrites-apple-sdk-and-swiftui-131h">this tutorial</a> instead.

## 📝 Prerequisites

At this stage, we assume that you already have an Appwrite instance up and running. If you do not have Appwrite setup yet, you can follow the super easy installation **step** over at [appwrite.io](https://appwrite.io/docs/installation). It's not a typo. There really is only 1 step!

You should have also set up an OAuth provider with Appwrite to be able to follow the OAuth section of this tutorial. You can learn to set up OAuth providers in Appwrite with [this tutorial](https://dev.to/appwrite/30daysofappwrite-oauth-providers-3jf6).

## 🛠️ Create a new App Project

Create a new **iOS > App** in Xcode, selecting **Storyboard** for **Interface** and **UIKit App Delegate** for **Life Cycle**.

![Create Project](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ms8klbqnyhfsxqrqbp8a.png) 

With the app created, now is also a good time to add our iOS, macOS, watchOS or tvOS app as a platform in the Appwrite Console. Head over to your project file and find your **Bundle Identifier**. It should look something like `io.appwrite.Appwrite-iOS`.

In your Appwrite console, click on **Add Platform** and select a **New Apple App**, then one of the iOS, macOS, watchOS or tvOS tabs. Give your app a name, add the bundle identifier and click **Register**.

Once this is complete, it's time to head back to our Xcode project add our dependencies.

## 👷 Setup Appwrite's Apple SDK

### Using Xcode

The Appwrite Apple SDK is available via Swift Package Manager. In order to use the Appwrite Apple SDK from **Xcode**, select File > Swift Packages > **Add Package Dependency**. In the dialog that appears, enter the Appwrite Apple SDK [package URL](https://github.com/appwrite/sdk-for-apple) and click **Next**.

Once the repository information is loaded, add your version rules and click **Next** again.

![Set Package Version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ymhv1va2x99ithkpxxb.png)

On the final screen, make sure `Appwrite` is selected to add to your target as a library.

![Link To Target](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/plu4hizc1o3bxd1p5j16.png)

### Using Swift Package Manager

Add the package to your `Package.swift` dependencies:

```swift
    dependencies: [
        .package(url: "https://github.com/appwrite/sdk-for-apple", from: "0.1.0"),
    ],
```

Then add it to your target:

```swift
    targets: [
        .target(
            name: "[YourAppTarget]",
            dependencies: [
                .product(name: "Appwrite", package: "sdk-for-apple")
            ]
        ),
```

Build your project and if there are no errors, we're ready to proceed!

## 🏗️ Create the ViewController

Create a new file `ViewController.swift` and add the following. This defines our controller for our storyboard and sets up our button click listeners that will call the `ViewModel`.

```swift
import Appwrite
import NIO
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var text: UITextView!
    @IBOutlet weak var register: UIButton!
    @IBOutlet weak var loginButton: UIButton!
    @IBOutlet weak var logOutButton: UIButton!
    @IBOutlet weak var getUserButton: UIButton!
    @IBOutlet weak var loginWithFacebook: UIButton!
    
    let client = Client()
        .setEndpoint("http://localhost/v1")
        .setProject("613b18dabf74a")
        .setSelfSigned()
    
    lazy var account = Account(client)
    
    var picker: ImagePicker?
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    @IBAction func register(_ sender: Any) {
        account.create(email: "tester@appwrite.io", password: "password") { result in
            var string: String = ""
            
            switch result {
            case .failure(let error): string = error.message
            case .success(let user): string = user.email
            }
            
            DispatchQueue.main.async {
                self.text.text = string
            }
        }
    }
    
    @IBAction func login(_ sender: Any) {
        account.createSession(email: "tester@appwrite.io", password: "password") { result in
            var string: String = ""
            
            switch result {
            case .failure(let error): string = error.message
            case .success(let session): string = session.userId
            }

            DispatchQueue.main.async {
                self.text.text = string
            }
        }
    }
    
    @IBAction func loginWithFacebook(_ sender: UIButton) {
        // To be added later.
    }
    
    @IBAction func getUser(_ sender: Any) {
        account.get { result in
            var string = ""
        
            switch result {
            case .failure(let error): string = error.message
            case .success(let user): string = user.email
            }
            
            DispatchQueue.main.async {
                self.text.text = string
            }
        }
    } 
    
    @IBAction func logOut(_ sender: Any) {
        account.deleteSession(sessionId: "current") { result in
            var string = ""
        
            switch result {
            case .failure(let error): string = error.message
            case .success(let success): string = String(describing: success)
            }
            
            DispatchQueue.main.async {
                self.text.text = string
            }
        }
    } 
}
```

Our ViewController has 4 state functions:

- register - click handler for the Signup Button
- login - click handler for the Login Button
- logout - click handler for the Logout Button
- getUser - click handler for the Get User Button

You should now be able to run your app and create users, login, logout and get information about the currently logged-in user!

## 🔐 Adding OAuth Support

You would have noticed that we have a **Login With Facebook** button in our UI, but it doesn't do anything yet; let's now add Facebook OAuth to our app!

The first step is to add a callback URL scheme to our `Info.plist` file.

```xml
<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>
```

Make sure you replace the Project ID in `appwrite-callback-[PROJECT-ID]` with your own.

Next we need to add a hook to save cookies when our app is opened by its callback URL. To do this, add the following function to your `SceneDelegate.swift`. If you have already defined this function, you can just add the contents below.

```swift
    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)
    }
```

The last step is to invoke the SDK function `createOAuth2Session` from `AccountViewController.swift` from our existing button's action.

```swift
    @IBAction func loginWithFacebook(_ sender: UIButton) {
        account.createOAuth2Session(
            provider:"facebook",
            success: "https://demo.appwrite.io/auth/oauth2/success",
            failure: "https://demo.appwrite.io/auth/oauth2/failure") { result in
                var string: String = ""
                
                switch result {
                case .failure(let error): string = error.message
                case .success(let response): string = response.description
                }

                DispatchQueue.main.async {
                    self.text.text = string
                }
            }
    }
```

Re-run your app and you should now be able to trigger your Facebook OAuth Flow! With that, you now know how to interact with Appwrite's Accounts API in your iOS, macOS, tvOS and watchOS apps!

We've built a complete app that interacts with all of Appwrite's APIs, which you can find over at our [Github Repo](https://github.com/appwrite/playground-for-apple-uikit). If you'd like to learn more about Appwrite or how Appwrite works under the hood, we've just curated all the resources for you during [30 Days of Appwrite](https://30days.appwrite.io/).

## ✨️ Credits

Hope you enjoyed this article! We love contributions and encourage you to take a look at our [open issues](https://github.com/appwrite/appwrite/issues) and [ongoing RFCs](https://github.com/appwrite/rfc/pulls).

If you get stuck anywhere, feel free to reach out to us on our [friendly support channels](https://appwrite.io/discord) run by humans 👩‍💻.

Here are some handy links for more information:

- [Appwrite Contribution Guide](https://github.com/appwrite/appwrite/blob/master/CONTRIBUTING.md)
- [Appwrite Github](https://github.com/appwrite)
- [Appwrite Documentation](https://appwrite.io/docs)