Skip to main content

Integrating the AI Gateway Module with macOS Swift application

This short guide aims to help you in successfully integrating Module to your project. In this tutorial we are using XCode IDE, but you can integrate using any IDE of your preferences.

info

Don't forget to generate your AI Gateway Module API Key and download Module from Developers dashboard first.

Installation

  1. Add AIGW_Module.xcframework file into your application target in the target settings -> General -> Frameworks, Libraries and Embedded content -> + -> Add Other... -> Add files -> Select AIGW_Module.xcframework -> Press Open.
  2. Once the AIGW_Module.xcframework is added to the Frameworks, Libraries and Embedded content section, set the Embed option to Embed & Sign.

Module setup

Starting the Module

First, add the AIGW_Module import to the top of your file. Then, initialize the Module with AIGW("YOUR_API_KEY") and pass your api key as an argument. After that, you need to get a legal consent from the user before starting Module (Module will not start without user consent). The simplest way to do that is to call requestConsent() method. After consent is provided, you can call start(). When consent screen closes you can check if consent was given in onAppear() function using isOptedIn() value.

ContentView.swift
import SwiftUI
import AIGW_Module

struct ContentView: View {
private let aigw = AIGW(apiKey: "YOUR_API_KEY")
var body: some View {
NavigationView {
...
}.onAppear {
Task {
if await aigw.isOptedIn() {
let startResult = await aigw.start()
} else {
await aigw.requestConsent()
}
}
}
}
}

Stopping Module

Call stop() in order to stop Module.

User should be able to opt out from using Module at any time. When user does so within your app, you should call optOut() function, which will immediately stop Module and will no longer start it unless user grants consent again.

If default Module consent window doesn't suit your needs, you can create a custom consent window. Make sure to call optIn() whenever user gives a consent and optOut() whenever user declines it.

warning

Custom consent screen must be approved by our team to maintain clarity for end user.

ContentView.swift
import SwiftUI
import AIGW_Module

struct ContentView: View {
private let aigw = AIGW(apiKey: "YOUR_API_KEY")
var body: some View {
NavigationView {
...
HStack {
Spacer()
Button("Agree") {
Task {
await aigw.optIn()
let startResult = await aigw.start()
}
}
Spacer()
Button("Decline") {
Task {
await aigw.optOut()
}
}
Spacer()
}
}.onAppear {
Task {
if await aigw.isOptedIn() {
let startResult = await aigw.start()
}
}
}
}
}

Logging

You can enable logging to debug console.

ConsentView.swift
import SwiftUI
import AIGW_Module

struct ContentView: View {
private let aigw = AIGW(apiKey: "YOUR_API_KEY")
var body: some View {
NavigationView {
...
}
.onAppear {
aigw.enableLogging(true)
Task {
if await aigw.isOptedIn() {
let startResult = await aigw.start()
} else {
await aigw.requestConsent()
}
}
}
}
}

Running in App Sandbox (Optional)

If you do decide to run your application in "App Sandbox", please make sure to enable incoming and outgoing network connections by:

  • In your application target settings selecting -> Signing & Capabilities;
  • In the App Sandbox section, enable Incoming Connections (Server) and Outgoing Connections (Client) checkboxes.

Module API

Full Module API documentation:


/**
* Initializes the Module.
* Pass [apiKey] that you received via dashboard for your app.
*/
init(apiKey: String)

/**
* Sets whether to enable logging to debug console
*/
func enableLogging(_ isLogging: Bool)

/**
* Checks whether Module has been started.
*/
func isRunning() -> Bool

/**
* Checks whether user has given the consent to start Module.
*/
func isOptedIn() async -> Bool

/**
* Starts Module if user has given the consent and returns `true`, otherwise returns `false`.
* Pass [apiKey] that you received via dashboard for your app.
*/
func start() async -> Bool

/**
* Stops Module if it's running.
*/
func stop() async

/**
* Withdraws user's consent and stops Module if it was running.
*/
func optOut() async

/**
* Informs Module that user gave consent. Used with custom consent requests when
* provided consent UI is insufficient.
* This does not start Module, [start()] still needs to be called.
*/
func optIn() async

/**
* Opens up a view displaying consent request with default settings.
* Returns a `Bool` value, indicating user's acceptance response.
*/
func requestConsent() async -> Bool

/*
* Returns device identifier
*/
func identify() -> String