Skip to content

Commit 7746100

Browse files
authored
watchOS (#10)
1 parent 689212f commit 7746100

5 files changed

Lines changed: 29 additions & 11 deletions

File tree

OAuthSample.xcodeproj/project.pbxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@
414414
repositoryURL = "https://github.com/codefiesta/OAuthKit.git";
415415
requirement = {
416416
kind = upToNextMajorVersion;
417-
minimumVersion = 1.2.2;
417+
minimumVersion = 1.3.0;
418418
};
419419
};
420420
/* End XCRemoteSwiftPackageReference section */

OAuthSample/Classes/ContentView.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import SwiftUI
1010

1111
struct ContentView: View {
1212

13-
#if !os(tvOS)
13+
#if canImport(WebKit)
1414
@Environment(\.openWindow)
1515
var openWindow
1616

@@ -46,7 +46,8 @@ struct ContentView: View {
4646
}
4747
case .receivedDeviceCode(_, let deviceCode):
4848
Text("To login, visit")
49-
Text(deviceCode.verificationUri).foregroundStyle(.blue)
49+
Text(.init("[\(deviceCode.verificationUri)](\(deviceCode.verificationUri))"))
50+
.foregroundStyle(.blue)
5051
Text("and enter the following code:")
5152
Text(deviceCode.userCode)
5253
.padding()
@@ -63,13 +64,25 @@ struct ContentView: View {
6364
var providerList: some View {
6465
List(oauth.providers) { provider in
6566
Button(provider.id) {
66-
// Start the authorization flow (use .deviceCode for tvOS)
67-
let grantType: OAuth.GrantType = .pkce(.init())
68-
oauth.authorize(provider: provider, grantType: grantType)
67+
authorize(provider: provider)
6968
}
7069
}
7170
}
72-
71+
72+
/// Starts the authorization process for the specified provider.
73+
/// - Parameter provider: the provider to begin authorization for
74+
private func authorize(provider: OAuth.Provider) {
75+
#if canImport(WebKit)
76+
// Use the PKCE grantType for iOS, macOS, visionOS
77+
let grantType: OAuth.GrantType = .pkce(.init())
78+
#else
79+
// Use the Device Code grantType for tvOS, watchOS
80+
let grantType: OAuth.GrantType = .deviceCode
81+
#endif
82+
// Start the authorization flow
83+
oauth.authorize(provider: provider, grantType: grantType)
84+
}
85+
7386
/// Reacts to oauth state changes by opening or closing authorization windows.
7487
/// - Parameter state: the published state change
7588
private func handle(state: OAuth.State) {
@@ -91,13 +104,13 @@ struct ContentView: View {
91104
}
92105

93106
private func openWebView() {
94-
#if !os(tvOS)
107+
#if canImport(WebKit)
95108
openWindow(id: .oauth)
96109
#endif
97110
}
98111

99112
private func dismissWebView() {
100-
#if !os(tvOS)
113+
#if canImport(WebKit)
101114
dismissWindow(id: .oauth)
102115
#endif
103116
}

OAuthSample/Classes/Extensions/OAuth+Window.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import OAuthKit
99
import SwiftUI
1010

11+
#if canImport(WebKit)
12+
1113
extension OAuth {
1214

1315
/// Provides a convenience enum that identifies application scene windows that can be opened or dismissed.
@@ -47,3 +49,5 @@ public extension DismissWindowAction {
4749
callAsFunction(id: id.rawValue)
4850
}
4951
}
52+
53+
#endif

OAuthSample/Classes/OAuthSampleApp.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct OAuthSampleApp: App {
2020
ContentView()
2121
}
2222

23-
#if !os(tvOS)
23+
#if canImport(WebKit)
2424
WindowGroup(id: .oauth) {
2525
OAWebView(oauth: oauth)
2626
}

README.md

Lines changed: 2 additions & 1 deletion
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

1011
# OAuthKit Sample

0 commit comments

Comments
 (0)