Skip to content

Commit 442a5c2

Browse files
authored
Support useragent override in the constructor (#26)
When initializing the `OptableSDK` constructor, it's now possible to override the otherwise internally assigned user-agent string, with one specified by the caller. The caller-specified useragent is of course optional and, if one is not assigned (or nil is passed as the useragent at initialization time) then the default is to continue to create an invisible WebView and extract the user-agent string from it. If a caller specifies a non-nil useragent string, then we use it instead. Closes #23 Documented in README
1 parent ee62a2c commit 442a5c2

5 files changed

Lines changed: 22 additions & 6 deletions

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ OPTABLE = OptableSDK(host: "sandbox.customer.com", app: "my-app", insecure: true
9191

9292
However, since production sandboxes only listen to TLS traffic, the above is really only useful for developers of `optable-sandbox` running the sandbox locally for testing.
9393

94+
By default, the SDK detects the application user agent by sniffing `navigator.userAgent` from a `WKWebView`. The resulting user agent string is sent to your sandbox for analytics purposes. To disable this behavior, you can provide an optional fourth string parameter, `useragent`, which allows you to set whatever user agent string you would like to send instead. For example:
95+
96+
```swift
97+
OPTABLE = OptableSDK(host: "sandbox.customer.com", app: "my-app", insecure: false, useragent: "custom-ua")
98+
```
99+
100+
The default value of `nil` for the `useragent` parameter enables the `WKWebView` auto-detection behavior.
101+
94102
### Identify API
95103

96104
To associate a user device with an authenticated identifier such as an Email address, or with other known IDs such as the Apple ID for Advertising (IDFA), or even your own vendor or app level `PPID`, you can call the `identify` API as follows:
@@ -312,7 +320,8 @@ OptableSDK *OPTABLE = nil;
312320
...
313321
OPTABLE = [[OptableSDK alloc] initWithHost: @"sandbox.optable.co"
314322
app: @"ios-sdk-demo"
315-
insecure: NO];
323+
insecure: NO
324+
useragent: nil];
316325
OptableSDKDelegate *delegate = [[OptableSDKDelegate alloc] init];
317326
OPTABLE.delegate = delegate;
318327
...
@@ -322,6 +331,8 @@ OptableSDK *OPTABLE = nil;
322331

323332
You can call various SDK APIs on the instance as shown in the examples below. It's also possible to configure multiple instances of `OptableSDK` in order to connect to other (e.g., partner) sandboxes and/or reference other configured application slug IDs. Note that the `insecure` flag should always be set to `NO` unless you are testing a local instance of the `optable-sandbox` yourself.
324333

334+
You can disable user agent `WKWebView` based auto-detection and provide your own value by setting the `useragent` parameter to a string value, similar to the Swift example.
335+
325336
### Identify API
326337

327338
To associate a user device with an authenticated identifier such as an Email address, or with other known IDs such as the Apple ID for Advertising (IDFA), or even your own vendor or app level `PPID`, you can call the `identify` API as follows:

Source/Config.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ struct Config {
1212
var host: String
1313
var app: String
1414
var insecure: Bool
15+
var useragent: String?
1516

1617
func edgeURL(_ path: String) -> URL? {
1718
var proto = "https://"

Source/Core/Client.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ class Client {
1616

1717
init(_ config: Config) {
1818
self.storage = LocalStorage(config)
19-
self.userAgent { (realUserAgent) in
20-
self.ua = realUserAgent
19+
if (config.useragent == nil) {
20+
self.userAgent { (realUserAgent) in
21+
self.ua = realUserAgent
22+
}
23+
} else {
24+
self.ua = config.useragent
2125
}
2226
}
2327

Source/OptableSDK.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ public class OptableSDK: NSObject {
6565
// OptableSDK(host, app) returns an instance of the SDK configured to talk to the sandbox specified by host & app:
6666
//
6767
@objc
68-
public init(host: String, app: String, insecure: Bool = false) {
69-
self.config = Config(host: host, app: app, insecure: insecure)
68+
public init(host: String, app: String, insecure: Bool = false, useragent: String? = nil) {
69+
self.config = Config(host: host, app: app, insecure: insecure, useragent: useragent)
7070
self.client = Client(self.config)
7171
}
7272

demo-ios-objc/demo-ios-objc/AppDelegate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ @implementation AppDelegate
2020
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
2121
// Override point for customization after application launch.
2222

23-
OPTABLE = [[OptableSDK alloc] initWithHost: @"sandbox.optable.co" app: @"ios-sdk-demo" insecure: NO];
23+
OPTABLE = [[OptableSDK alloc] initWithHost: @"sandbox.optable.co" app: @"ios-sdk-demo" insecure: NO useragent: nil];
2424
OptableSDKDelegate *delegate = [[OptableSDKDelegate alloc] init];
2525
OPTABLE.delegate = delegate;
2626

0 commit comments

Comments
 (0)