Skip to content

Commit af0d8b7

Browse files
authored
watchOS (#89)
1 parent e3e0c2d commit af0d8b7

File tree

6 files changed

+34
-10
lines changed

6 files changed

+34
-10
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ let package = Package(
99
.iOS(.v18),
1010
.macOS(.v15),
1111
.tvOS(.v18),
12-
.visionOS(.v2)
12+
.visionOS(.v2),
13+
.watchOS(.v11)
1314
],
1415
products: [
1516
.library(

README.md

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
![iOS 18.0+](https://img.shields.io/badge/iOS-18.0%2B-crimson.svg)
55
![macOS 15.0+](https://img.shields.io/badge/macOS-15.0%2B-skyblue.svg)
66
![tvOS 18.0+](https://img.shields.io/badge/tvOS-18.0%2B-blue.svg)
7-
![visionOS 2.0+](https://img.shields.io/badge/visionOS-2.0%2B-magenta.svg)
7+
![visionOS 2.0+](https://img.shields.io/badge/visionOS-2.0%2B-violet.svg)
8+
![watchOS 11.0+](https://img.shields.io/badge/watchOS-11.0%2B-magenta.svg)
89
[![License: MIT](https://img.shields.io/badge/License-MIT-indigo.svg)](https://opensource.org/licenses/MIT)
910
![Code Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/codefiesta/87655b6e3c89b9198287b2fefbfa641f/raw/oauthkit-coverage.json)
1011

@@ -16,7 +17,7 @@ OAuthKit is a contemporary, event-driven Swift Package that utilizes the [Observ
1617

1718
## OAuthKit Usage
1819

19-
The following is an example of the simplest usage of using OAuthKit in macOS:
20+
The following is an example of the simplest usage of using OAuthKit across multiple platforms (iOS, macOS, visionOS, tvOS, watchOS):
2021

2122
```swift
2223
import OAuthKit
@@ -36,19 +37,23 @@ struct OAuthApp: App {
3637
}
3738
.environment(\.oauth, oauth)
3839

40+
#if canImport(WebKit)
3941
WindowGroup(id: "oauth") {
4042
OAWebView(oauth: oauth)
4143
}
44+
#endif
4245
}
4346
}
4447

4548
struct ContentView: View {
4649

50+
#if canImport(WebKit)
4751
@Environment(\.openWindow)
4852
var openWindow
4953

5054
@Environment(\.dismissWindow)
5155
private var dismissWindow
56+
#endif
5257

5358
@Environment(\.oauth)
5459
var oauth: OAuth
@@ -70,7 +75,8 @@ struct ContentView: View {
7075
}
7176
case .receivedDeviceCode(_, let deviceCode):
7277
Text("To login, visit")
73-
Text(deviceCode.verificationUri).foregroundStyle(.blue)
78+
Text(.init("[\(deviceCode.verificationUri)](\(deviceCode.verificationUri))"))
79+
.foregroundStyle(.blue)
7480
Text("and enter the following code:")
7581
Text(deviceCode.userCode)
7682
.padding()
@@ -87,11 +93,24 @@ struct ContentView: View {
8793
var providerList: some View {
8894
List(oauth.providers) { provider in
8995
Button(provider.id) {
90-
// Start the default PKCE flow (.pkce)
91-
oauth.authorize(provider: provider)
96+
authorize(provider: provider)
9297
}
9398
}
9499
}
100+
101+
/// Starts the authorization process for the specified provider.
102+
/// - Parameter provider: the provider to begin authorization for
103+
private func authorize(provider: OAuth.Provider) {
104+
#if canImport(WebKit)
105+
// Use the PKCE grantType for iOS, macOS, visionOS
106+
let grantType: OAuth.GrantType = .pkce(.init())
107+
#else
108+
// Use the Device Code grantType for tvOS, watchOS
109+
let grantType: OAuth.GrantType = .deviceCode
110+
#endif
111+
// Start the authorization flow
112+
oauth.authorize(provider: provider, grantType: grantType)
113+
}
95114

96115
/// Reacts to oauth state changes by opening or closing authorization windows.
97116
/// - Parameter state: the published state change
@@ -100,9 +119,13 @@ struct ContentView: View {
100119
case .empty, .requestingAccessToken, .requestingDeviceCode:
101120
break
102121
case .authorizing, .receivedDeviceCode:
122+
#if canImport(WebKit)
103123
openWindow(id: "oauth")
124+
#endif
104125
case .authorized(_, _):
126+
#if canImport(WebKit)
105127
dismissWindow(id: "oauth")
128+
#endif
106129
}
107130
}
108131
}

Sources/OAuthKit/Views/OAWebView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Kevin McKee on 5/16/24.
66
//
77

8-
#if !os(tvOS)
8+
#if canImport(WebKit)
99
import SwiftUI
1010
import WebKit
1111

Sources/OAuthKit/Views/OAWebViewCoordinator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Kevin McKee on 5/16/24.
66
//
77

8-
#if !os(tvOS)
8+
#if canImport(WebKit)
99
import Combine
1010
import SwiftUI
1111
import WebKit

Tests/OAuthKitTests/OAWebViewTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Kevin McKee
66
//
77

8-
#if !os(tvOS)
8+
#if canImport(WebKit)
99
import Foundation
1010
@testable import OAuthKit
1111
import SwiftUI

Tests/OAuthKitTests/OAuthTestWKNavigationAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Kevin McKee
66
//
77

8-
#if !os(tvOS)
8+
#if canImport(WebKit)
99
import WebKit
1010

1111
/// OAuth Test WKNavigationAction that can be used for testing

0 commit comments

Comments
 (0)