iOS

Integrating Eartho into Your iOS App (Swift)

This section describes how to integrate Eartho into an iOS app using Swift.

1. Install the SDK

You can install the Eartho SDK using Swift Package Manager or CocoaPods.

Using Swift Package Manager

  1. Open your Xcode project.

  2. Go to File > Add Packages....

  3. Enter the Eartho SDK URL: https://github.com/eartho-group/one-client-swift.

  4. Choose the version and add the package to your project.

Using CocoaPods

  1. Add the following line to your Podfile:

    pod 'EarthoOne', '~> 1.0.4'
  2. Run pod install to install the SDK.

2. Retrieve Eartho Client ID, Client Secret, and Access ID

Go to Eartho Creator and copy your Eartho client ID, client secret, and access ID from the "Developers Integration" section.

3. Configure URL Scheme

  1. Open your Xcode project.

  2. Go to the project settings and select your app target.

  3. Go to the Info tab and add a new URL type:

    • Identifier: eartho

    • URL Schemes: $(PRODUCT_BUNDLE_IDENTIFIER).one.eartho.world

Example:

Identifier: eartho
URL Schemes: $(PRODUCT_BUNDLE_IDENTIFIER).one.eartho.world

4. Initialize and Configure the SDK

Integrate Eartho into your iOS app by configuring the SDK and handling authentication.

Example Code:

AppDelegate.swift

Add the following code to handle the URL scheme in your AppDelegate.swift:

import UIKit
import EarthoOne

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ app: UIApplication,
                     open url: URL,
                     options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
        return WebAuthentication.resume(with: url)
    }
}

ContentView.swift (SwiftUI)

Add the following code to your SwiftUI view to handle authentication:

import SwiftUI
import EarthoOne

struct ContentView: View {
    @State private var earthoOne: EarthoOne?
    @State private var user: UserProfile?

    var body: some View {
        VStack {
            if let user = user {
                Text("Hello, \(user.displayName)")
                Button("Log out", action: logout)
            } else {
                Button("Log in", action: login)
            }
        }
        .onAppear(perform: setupEartho)
        .onOpenURL(perform: { url in
            WebAuthentication.resume(with: url)
        })
    }

    private func setupEartho() {
        earthoOne = EarthoOne(
            clientId: "YOUR_EARTHO_CLIENT_ID",
            clientSecret: "YOUR_EARTHO_CLIENT_SECRET"
        )
    }

    private func login() {
        let accessId = "YOUR_EARTHO_ACCESS_ID"
        earthoOne?.connectWithPopup(accessId: accessId, onSuccess: { credentials in
            self.user = self.earthoOne?.getUser()
        }, onFailure: { error in
            print("Error: \(error.localizedDescription)")
        })
    }

    private func logout() {
        earthoOne?.logout()
        user = nil
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

UIKit Example

Add the following code to your view controller to handle authentication:

import UIKit
import EarthoOne

class ViewController: UIViewController {

    private var earthoOne: EarthoOne?

    override func viewDidLoad() {
        super.viewDidLoad()
        setupEartho()
    }

    private func setupEartho() {
        earthoOne = EarthoOne(
            clientId: "YOUR_EARTHO_CLIENT_ID",
            clientSecret: "YOUR_EARTHO_CLIENT_SECRET"
        )
    }

    @IBAction func login(_ sender: UIButton) {
        let accessId = "YOUR_EARTHO_ACCESS_ID"
        earthoOne?.connectWithPopup(accessId: accessId, onSuccess: { credentials in
            // Handle the authenticated user's profile
            if let user = self.earthoOne?.getUser() {
                print("User ID: \(user.uid)")
                print("User Name: \(user.displayName)")
            }
        }, onFailure: { error in
            // Handle the error
            print("Error: \(error.localizedDescription)")
        })
    }

    @IBAction func logout(_ sender: UIButton) {
        earthoOne?.logout()
    }
}

Additional Resources

For more detailed examples and usage, visit the Eartho Swift Example Repository.

Last updated