Skip to content

Commit

Permalink
Reorganize fetching logic & interface
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytriigolovanov committed Nov 14, 2022
1 parent f3f475c commit 5002ec3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 136 deletions.
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ Library provides 3 variants of usage.

...

let userAgent = webView.userAgent
DispatchQueue.main.async {
let userAgent = webView.userAgent
}
}
```

2. With default `WKWebView`.

**example:**

```swift
let userAgent = WKUserAgent.default
WKUserAgent.fetchDefault { userAgent in

}
```

3. With `applicationName`.
Expand All @@ -75,9 +80,12 @@ Provided `overrideDefaultApplicationName` parameter for using/rewriting default
```swift
let applicationName = "EXAMPLE/1.0.0"

let userAgent = WKUserAgent.withApplicationName(
applicationName,
overrideDefaultApplicationName: true)
WKUserAgent.fetch(
withApplicationName: applicationName,
overrideDefaultApplicationName: true
) { userAgent in

}
```


Expand Down
8 changes: 0 additions & 8 deletions WebKitUserAgent/Sources/Extensions/WKWebView+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,4 @@ extension WKWebView {
public var userAgent: String! {
self.value(forKey: "userAgent") as? String
}

// MARK: <3.0.0 versions support

/// Fetching `User Agent` through `WKWebView`.
@available(*, deprecated, renamed: "userAgent")
public func fetchUserAgent(completion: @escaping (Result<String, Error>) -> Void) {
WKUserAgent.fetch(fromWebView: self, completion: completion)
}
}
64 changes: 19 additions & 45 deletions WebKitUserAgent/Sources/WKUserAgent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,31 @@ import WebKit

public final class WKUserAgent {
/// Fetching `User Agent` through default `WKWebView`.
public static var `default`: String! {
WKWebView().userAgent
public static func fetchDefault(completionHandler: @escaping (String?) -> Void) {
DispatchQueue.main.async {
let userAgent = WKWebView().userAgent
completionHandler(userAgent)
}
}

/// Fetching `User Agent` through default `WKWebView` with application name.
/// Overriding default `applicationName` value (default for `WKWebViewConfiguration`) ability provided.
public static func withApplicationName(
_ applicationName: String,
overrideDefaultApplicationName: Bool = false
) -> String! {
let configuration = WKWebViewConfiguration()
configuration.setApplicationNameForUserAgent(
applicationName,
overrideDefault: overrideDefaultApplicationName)

let webView = WKWebView(
frame: .zero,
configuration: configuration)
return webView.userAgent
}

// MARK: <3.0.0 versions support

/// Fetching `User Agent` through `WKWebView`.
@available(*, deprecated, message: "Use WKWebView's userAgent property instead.")
public static func fetch(
fromWebView webView: WKWebView,
completion: @escaping (Result<String, Error>) -> Void
) {
let userAgent = webView.userAgent
completion(.success(userAgent ?? ""))
}

/// Fetching `User Agent` through default `WKWebView`.
@available(*, deprecated, renamed: "default")
public static func fetch(completion: @escaping (Result<String, Error>) -> Void) {
let userAgent = self.default
completion(.success(userAgent ?? ""))
}

/// Fetching `User Agent` through default `WKWebView` with application name.
/// Rewriting default `applicationName` value (default for `WKWebViewConfiguration`) ability provided.
@available(*, deprecated, renamed: "withApplicationName")
public static func fetch(
withApplicationName applicationName: String,
rewriteDefaultApplicationName: Bool = false,
completion: @escaping (Result<String, Error>) -> Void
overrideDefaultApplicationName: Bool = false,
completionHandler: @escaping (String?) -> Void
) {
let userAgent = self.withApplicationName(
applicationName,
overrideDefaultApplicationName: rewriteDefaultApplicationName)
completion(.success(userAgent ?? ""))
DispatchQueue.main.async {
let configuration = WKWebViewConfiguration()
configuration.setApplicationNameForUserAgent(
applicationName,
overrideDefault: overrideDefaultApplicationName)

let webView = WKWebView(
frame: .zero,
configuration: configuration)
let userAgent = webView.userAgent
completionHandler(userAgent)
}
}
}
98 changes: 20 additions & 78 deletions WebKitUserAgent/Tests/WebKitUserAgentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ final class Tests: XCTestCase {
let applicationName: String = "TEST/0.0.0"

func testFromWebView() throws {
DispatchQueue.main.async {

}
let webView = WKWebView(frame: .zero)
guard let userAgent = webView.userAgent else {
return XCTFail("Nil User-Agent.")
Expand All @@ -22,107 +25,46 @@ final class Tests: XCTestCase {
}

func testDefault() throws {
guard let userAgent = WKUserAgent.default else {
return XCTFail("Nil User-Agent.")
}
XCTAssertFalse(userAgent.isEmpty, "User Agent is empty.")
}

func testWithApplicationName() throws {
guard let userAgent = WKUserAgent.withApplicationName(applicationName) else {
return XCTFail("Nil User-Agent.")
}
XCTAssertTrue(userAgent.contains(applicationName), "User Agent doesn't contains provided application name.")
XCTAssertFalse(userAgent.isEmpty, "User Agent is empty.")
}

func testWithApplicationNameOverridingDefault() throws {
guard
let userAgent = WKUserAgent.withApplicationName(
applicationName,
overrideDefaultApplicationName: true)
else {
return XCTFail("Nil User-Agent.")
}
XCTAssertTrue(userAgent.contains(applicationName), "User Agent doesn't contains provided application name.")
XCTAssertFalse(userAgent.isEmpty, "User Agent is empty.")
}

// MARK: <3.0.0 versions

func testDeprecatedFromWebView() throws {
let expectation = XCTestExpectation(description: "Fetch User Agent")

DispatchQueue.main.async {
let webView = WKWebView(frame: .zero)
webView.fetchUserAgent { result in
switch result {
case .success(let userAgent):
XCTAssertFalse(userAgent.isEmpty, "User Agent is empty.")
case .failure(let error):
XCTFail("Error: \(error.localizedDescription)")
}

expectation.fulfill()
}
}

wait(for: [expectation], timeout: 10.0)
}

func testDeprecatedDefault() throws {
let expectation = XCTestExpectation(description: "Fetch User Agent")

WKUserAgent.fetch { result in
switch result {
case .success(let userAgent):
XCTAssertFalse(userAgent.isEmpty, "User Agent is empty.")
case .failure(let error):
XCTFail("Error: \(error.localizedDescription)")
WKUserAgent.fetchDefault { userAgent in
guard let userAgent = userAgent else {
return XCTFail("Nil User-Agent.")
}

XCTAssertFalse(userAgent.isEmpty, "User Agent is empty.")
expectation.fulfill()
}

wait(for: [expectation], timeout: 10.0)
}

func testDeprecatedWithApplicationName() throws {
func testWithApplicationName() throws {
let expectation = XCTestExpectation(description: "Fetch User Agent")

WKUserAgent.fetch(
withApplicationName: applicationName,
rewriteDefaultApplicationName: true
) { result in
switch result {
case .success(let userAgent):
XCTAssertFalse(userAgent.isEmpty, "User Agent is empty.")
XCTAssertTrue(userAgent.contains(self.applicationName), "User Agent doesn't contains provided application name.")
case .failure(let error):
XCTFail("Error: \(error.localizedDescription)")
WKUserAgent.fetch(withApplicationName: applicationName) { userAgent in
guard let userAgent = userAgent else {
return XCTFail("Nil User-Agent.")
}

XCTAssertTrue(userAgent.contains(self.applicationName), "User Agent doesn't contains provided application name.")
XCTAssertFalse(userAgent.isEmpty, "User Agent is empty.")
expectation.fulfill()
}

wait(for: [expectation], timeout: 10.0)
}

func testDeprecatedWithApplicationNameOverridingDefault() throws {
func testWithApplicationNameOverridingDefault() throws {
let expectation = XCTestExpectation(description: "Fetch User Agent")

WKUserAgent.fetch(
withApplicationName: applicationName,
rewriteDefaultApplicationName: false
) { result in
switch result {
case .success(let userAgent):
XCTAssertFalse(userAgent.isEmpty, "User Agent is empty.")
XCTAssertTrue(userAgent.contains(self.applicationName), "User Agent doesn't contains provided application name.")
case .failure(let error):
XCTFail("Error: \(error.localizedDescription)")
overrideDefaultApplicationName: true
) { userAgent in
guard let userAgent = userAgent else {
return XCTFail("Nil User-Agent.")
}

XCTAssertTrue(userAgent.contains(self.applicationName), "User Agent doesn't contains provided application name.")
XCTAssertFalse(userAgent.isEmpty, "User Agent is empty.")
expectation.fulfill()
}

Expand Down

0 comments on commit 5002ec3

Please sign in to comment.