1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/website/docs/reference/sdks/ios-proxy.md
Thomas Heartman d5fbd0b743
refactor: move docs into new structure / fix links for SEO (#2416)
## What

This (admittedly massive) PR updates the "physical" documentation
structure and fixes url inconsistencies and SEO problems reported by
marketing. The main points are:

- remove or move directories : advanced, user_guide, deploy, api
- move the files contained within to the appropriate one of topics,
how-to, tutorials, or reference
- update internal doc links and product links to the content
- create client-side redirects for all the urls that have changed.

A number of the files have been renamed in small ways to better match
their url and to make them easier to find. Additionally, the top-level
api directory has been moved to /reference/api/legacy/unleash (see the
discussion points section for more on this).

## Why

When moving our doc structure to diataxis a while back, we left the
"physical' files lying where they were, because it didn't matter much to
the new structure. However, that did introduce some inconsistencies with
where you place docs and how we organize them.

There's also the discrepancies in whether urls us underscores or hyphens
(which isn't necessarily the same as their file name), which has been
annoying me for a while, but now has also been raised by marketing as an
issue in terms of SEO.

## Discussion points

The old, hand-written API docs have been moved from /api to
/reference/api/legacy/unleash. There _is_ a /reference/api/unleash
directory, but this is being populated by the OpenAPI plugin, and mixing
those could only cause trouble. However, I'm unsure about putting
/legacy/ in the title, because the API isn't legacy, the docs are. Maybe
we could use another path? Like /old-docs/ or something? I'd appreciate
some input on this.
2022-11-22 09:05:30 +00:00

4.4 KiB

title
iOS Proxy SDK

In this guide we explain how to use feature toggles in Swift applications via the Unleash Proxy. You can also check out the source code for the iOS Proxy SDK.

Introduction

The unleash-proxy-client-swift makes it easy for native applications and other swift platforms to connect to the unleash proxy. The proxy will evaluate a feature toggle for a given context and return a list of feature flags relevant for the provided context.

The unleash-proxy-client-swift will then cache these toggles in a map in memory and refresh the configuration at a configurable interval, making queries against the toggle configuration extremely fast.

Requirements

  • MacOS: 12.15
  • iOS: 13

Installation

Follow the following steps in order to install the unleash-proxy-client-swift:

  1. In your Xcode project go to File -> Swift Packages -> Add Package Dependency
  2. Supply the link to this repository
  3. Set the appropriate package constraints (typically up to next major version)
  4. Let Xcode find and install the necessary packages

Once you're done, you should see SwiftEventBus and UnleashProxyClientSwift listed as dependencies in the file explorer of your project.

Usage

In order to get started you need to import and instantiate the unleash client:

import SwiftUI
// Import UnleashProxyClientSwift
import UnleashProxyClientSwift

// Setup Unleash in the context where it makes most sense

var unleash = UnleashProxyClientSwift.UnleashClient(
    unleashUrl: "https://app.unleash-hosted.com/hosted/api/proxy",
    clientKey: "PROXY_KEY",
    refreshInterval: 15,
    appName: "test",
    environment: "dev")

unleash.start()

In the example above we import the UnleashProxyClientSwift and instantiate the client. You need to provide the following parameters:

  • unleashUrl (String)

    The full URL to your proxy instance.

  • clientKey (String)

    One of the configured proxy keys / proxy secrets.

  • refreshInterval (Int)

    The polling interval in seconds.

  • appName (String)

    The application name; only used to identify your application.

  • environment (String)

    The application environment. This corresponds to the environment field in the Unleash Context. Note that this is separate from the newer Environments feature.

Running unleash.start() will make the first request against the proxy and retrieve the feature toggle configuration, and set up the polling interval in the background.

NOTE: While waiting to boot up the configuration may not be available, which means that asking for a feature toggle may result in a false if the configuration has not loaded. In the event that you need to be certain that the configuration is loaded we emit an event you can subscribe to, once the configuration is loaded. See more in the Events section.

Once the configuration is loaded you can ask against the cache for a given feature toggle:

if unleash.isEnabled(name: "ios") {
    // do something
} else {
   // do something else
}

You can also set up variants and use them in a similar fashion:

var variant = unleash.getVariant(name: "ios")
if variant.enabled {
    // do something
} else {
   // do something else
}

Update context

In order to update the context you can use the following method:

var context: [String: String] = [:]
context["userId"] = "c3b155b0-5ebe-4a20-8386-e0cab160051e"
unleash.updateContext(context: context)

This will stop and start the polling interval in order to renew polling with new context values.

Events

The proxy client emits two different events you can subscribe to:

  • "ready"
  • "update"

Usage them in the following manner:

func handleReady() {
    // do this when unleash is ready
}

unleash.subscribe(name: "ready", callback: handleReady)

func handleUpdate() {
    // do this when unleash is updated
}

unleash.subscribe(name: "update", callback: handleUpdate)

The ready event is fired once the client has received it's first set of feature toggles and cached it in memory. Every subsequent event will be an update event that is triggered if there is a change in the feature toggle configuration.