Skip to content

Commit

Permalink
ios: allow example app users to configure API URL and key (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
Augustyniak authored Oct 24, 2024
1 parent 4dbd21a commit d2f5dd9
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
4 changes: 2 additions & 2 deletions examples/swift/hello_world/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ios_application(
"ipad",
],
infoplists = ["Info.plist"],
minimum_os_version = "13.0",
minimum_os_version = "16.0",
provisioning_profile = select({
"//bazel:ios_device_build": "//bazel/ios:ios_provisioning_profile",
"//conditions:default": None,
Expand Down Expand Up @@ -71,7 +71,7 @@ ios_application(
"ipad",
],
infoplists = ["Info.plist"],
minimum_os_version = "13.0",
minimum_os_version = "16.0",
provisioning_profile = select({
"//bazel:ios_device_build": "//bazel/ios:ios_provisioning_profile",
"//conditions:default": None,
Expand Down
38 changes: 38 additions & 0 deletions examples/swift/hello_world/Configuration.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// capture-sdk - bitdrift's client SDK
// Copyright Bitdrift, Inc. All rights reserved.
//
// Use of this source code is governed by a source available license that can be found in the
// LICENSE file or at:
// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt

import Combine
import Foundation

final class Configuration: ObservableObject {
@Published var apiURL: String
@Published var apiKey: String

private var subscriptions = Set<AnyCancellable>()

static var storedAPIURL: String {
get { UserDefaults.standard.string(forKey: "apiURL") ?? "https://api.bitdrift.io" }
set { UserDefaults.standard.setValue(newValue, forKey: "apiURL") }
}

static var storedAPIKey: String? {
get { UserDefaults.standard.string(forKey: "apiKey") }
set { UserDefaults.standard.setValue(newValue, forKey: "apiKey") }
}

init() {
self.apiURL = Self.storedAPIURL
self.apiKey = Self.storedAPIKey ?? ""

$apiURL
.sink(receiveValue: { Self.storedAPIURL = $0 })
.store(in: &self.subscriptions)
$apiKey
.sink(receiveValue: { Self.storedAPIKey = $0 })
.store(in: &self.subscriptions)
}
}
28 changes: 28 additions & 0 deletions examples/swift/hello_world/ConfigurationView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// capture-sdk - bitdrift's client SDK
// Copyright Bitdrift, Inc. All rights reserved.
//
// Use of this source code is governed by a source available license that can be found in the
// LICENSE file or at:
// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt

import SwiftUI

struct ConfigurationView: View {
@StateObject var configuration = Configuration()

var body: some View {
Text("API URL").frame(maxWidth: .infinity)
TextField(text: $configuration.apiURL) { Text("Enter API URL") }
.autocapitalization(.none)

Text("API Key").frame(maxWidth: .infinity)
TextField(text: $configuration.apiKey, axis: .vertical) { Text("Enter API Key") }
.autocapitalization(.none)

Spacer()

Text("The app needs to be restarted for any configuration change to take effect.")
.font(.caption2)
.padding(EdgeInsets(top: 10, leading: 10, bottom: 20, trailing: 10))
}
}
1 change: 1 addition & 0 deletions examples/swift/hello_world/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct ContentView: View {

var body: some View {
VStack {
NavigationLink("Configuration") { ConfigurationView() }
Spacer()
VStack {
Text("ACTIONS")
Expand Down
16 changes: 9 additions & 7 deletions examples/swift/hello_world/LoggerCustomer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import Capture
import Foundation
import MetricKit

private let kBitdriftAPIKey = "<YOUR API KEY GOES HERE>"
// swiftlint:disable:next force_unwrapping use_static_string_url_init
private let kBitdriftURL = URL(string: "https://api.bitdrift.io")!
private let kDeviceId = "ios-helloworld"

private struct EncodableExampleStruct: Encodable {
Expand Down Expand Up @@ -83,21 +80,26 @@ final class LoggerCustomer: NSObject, URLSessionDelegate {
override init() {
self.appStartTime = Date()

super.init()

guard let apiURL = URL(string: Configuration.storedAPIURL) else {
print("failed to initialize logger due to invalid API URL: \(Configuration.storedAPIURL)")
return
}

Logger
.start(
withAPIKey: kBitdriftAPIKey,
withAPIKey: Configuration.storedAPIKey ?? "",
sessionStrategy: .fixed(),
configuration: .init(),
fieldProviders: [CustomFieldProvider()],
apiURL: kBitdriftURL
apiURL: apiURL
)?
.enableIntegrations([.urlSession()], disableSwizzling: true)

Logger.addField(withKey: "field_container_field_key", value: "field_container_value")
Logger.logInfo("App launched. Logger configured.")

super.init()

MXMetricManager.shared.add(self)
}

Expand Down

0 comments on commit d2f5dd9

Please sign in to comment.