-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Update to new standard format (#43)
- Loading branch information
Showing
7 changed files
with
56 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Repository Maintainers | ||
* @launchdarkly/team-sdk-swift |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
### LaunchDarkly Sample macOS Application ### | ||
# LaunchDarkly sample macOS application | ||
|
||
[](https://github.com/launchdarkly/hello-macos/actions/workflows/ci.yml) | ||
We've built a simple mobile application that demonstrates how LaunchDarkly's SDK works. | ||
|
||
We've built a simple macOS application that demonstrates how LaunchDarkly's SDK works. Below, you'll find the basic build procedure, but for more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/). | ||
Below, you'll find the build procedure. For more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/) or the [iOS reference guide](https://docs.launchdarkly.com/sdk/client-side/ios). | ||
|
||
##### Build instructions ##### | ||
## Build instructions | ||
|
||
1. Make sure you have [XCode](https://itunes.apple.com/us/app/xcode/id497799835?ls=1&mt=12) installed | ||
2. Open `hello-macos.xcworkspace` in XCode | ||
3. Copy the mobile key from your account settings page and the feature flag key from your LaunchDarkly dashboard into `AppDelegate.swift` | ||
4. Run your application through XCode | ||
1. Make sure you have [Xcode](https://itunes.apple.com/us/app/xcode/id497799835?ls=1&mt=12) installed | ||
1. Open `hello-macos.xcworkspace` in XCode | ||
1. Set the value of `sdkKey` in `AppDelegate.swift` to your LaunchDarkly mobile key. If there is an existing boolean feature flag in your LaunchDarkly project that you want to evaluate, set `featureFlagKey` in `ViewController.swift` to the flag key. | ||
|
||
```swift | ||
// AppDelegate.swift | ||
let sdkKey = "" | ||
|
||
// ViewController.swift | ||
let featureFlagKey = "sample-feature" | ||
``` | ||
|
||
You should see the message "The <flagKey> feature flag evaluates to <flagValue>.", the application will run continuously and react to the flag changes in LaunchDarkly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,31 @@ | ||
// | ||
// AppDelegate.swift | ||
// hello-macos | ||
// | ||
// Copyright © 2017 LaunchDarkly. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
import LaunchDarkly | ||
|
||
@NSApplicationMain | ||
class AppDelegate: NSObject, NSApplicationDelegate { | ||
|
||
let launchDarklyMobileKey = "" | ||
// Set sdkKey to your LaunchDarkly mobile key. | ||
let sdkKey = "" | ||
|
||
func applicationDidFinishLaunching(_ aNotification: Notification) { | ||
// Insert code here to initialize your application | ||
|
||
setupLDClient() | ||
if let viewController = NSApp.windows.first?.contentViewController as? ViewController { | ||
viewController.onApplicationStarted() | ||
} | ||
} | ||
|
||
func setupLDClient() { | ||
var contextBuilder = LDContextBuilder(key: "test@email.com") | ||
contextBuilder.trySetValue("firstName", .string("Bob")) | ||
contextBuilder.trySetValue("lastName", .string("Loblaw")) | ||
contextBuilder.trySetValue("groups", .array([.string("beta_testers")])) | ||
// Set up the evaluation context. This context should appear on your | ||
// LaunchDarkly contexts dashboard soon after you run the demo. | ||
var contextBuilder = LDContextBuilder(key: "example-user-key") | ||
contextBuilder.kind("user") | ||
contextBuilder.name("Sandy") | ||
|
||
guard case .success(let context) = contextBuilder.build() | ||
else { return } | ||
|
||
var config = LDConfig(mobileKey: launchDarklyMobileKey, autoEnvAttributes: .enabled) | ||
config.flagPollingInterval = 30.0 | ||
config.enableBackgroundUpdates = true | ||
config.eventFlushInterval = 30.0 | ||
var config = LDConfig(mobileKey: sdkKey, autoEnvAttributes: .enabled) | ||
|
||
LDClient.start(config: config, context: context) | ||
} | ||
|
||
func applicationWillTerminate(_ aNotification: Notification) { | ||
// Insert code here to tear down your application | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,33 @@ | ||
// | ||
// ViewController.swift | ||
// hello-macos | ||
// | ||
// Copyright © 2017 LaunchDarkly. All rights reserved. | ||
// | ||
|
||
import Cocoa | ||
import LaunchDarkly | ||
|
||
class ViewController: NSViewController { | ||
|
||
let flagKey = "test-flag" | ||
// Set featureFlagKey to the feature flag key you want to evaluate. | ||
let featureFlagKey = "sample-feature" | ||
|
||
@IBOutlet weak var valueLabel: NSTextField! | ||
|
||
func onApplicationStarted() { | ||
registerLDClientObservers() | ||
checkFeatureValue() | ||
} | ||
if let ld = LDClient.get() { | ||
ld.observe(key: featureFlagKey, owner: self) { [weak self] changedFlag in | ||
guard let me = self else { return } | ||
guard case .bool(let booleanValue) = changedFlag.newValue else { return } | ||
|
||
func registerLDClientObservers() { | ||
LDClient.get()!.observe(key: flagKey, owner: self) { [weak self] changedFlag in | ||
self?.featureFlagDidUpdate(changedFlag.key) | ||
me.updateUi(flagKey: changedFlag.key, result: booleanValue) | ||
} | ||
let result = ld.boolVariation(forKey: featureFlagKey, defaultValue: false) | ||
updateUi(flagKey: featureFlagKey, result: result) | ||
} | ||
} | ||
|
||
func checkFeatureValue() { | ||
let showFeature = LDClient.get()!.boolVariation(forKey: flagKey, defaultValue: false) | ||
updateLabel(value: "\(showFeature)") | ||
} | ||
|
||
func updateLabel(value: String){ | ||
valueLabel.stringValue = "Flag value: \(value)" | ||
} | ||
|
||
//MARK: - ClientDelegate Methods | ||
|
||
func featureFlagDidUpdate(_ key: String!) { | ||
if key == flagKey { | ||
checkFeatureValue() | ||
} | ||
|
||
func updateUi(flagKey: String, result: Bool) { | ||
self.valueLabel.stringValue = "The \(flagKey) feature flag evaluates to \(result)" | ||
|
||
let toggleOn = NSColor(red: 0, green: 0.52, blue: 0.29, alpha: 1).cgColor | ||
let toggleOff = NSColor(red: 0.22, green: 0.22, blue: 0.25, alpha: 1).cgColor | ||
|
||
self.view.wantsLayer = true | ||
self.view.layer?.backgroundColor = result ? toggleOn : toggleOff | ||
} | ||
|
||
} | ||
|