diff --git a/OAuthSample.xcodeproj/project.pbxproj b/OAuthSample.xcodeproj/project.pbxproj index df8c44a..da4819a 100644 --- a/OAuthSample.xcodeproj/project.pbxproj +++ b/OAuthSample.xcodeproj/project.pbxproj @@ -414,7 +414,7 @@ repositoryURL = "https://github.com/codefiesta/OAuthKit.git"; requirement = { kind = upToNextMajorVersion; - minimumVersion = 1.2.2; + minimumVersion = 1.3.0; }; }; /* End XCRemoteSwiftPackageReference section */ diff --git a/OAuthSample/Classes/ContentView.swift b/OAuthSample/Classes/ContentView.swift index 26e9e8b..1110441 100644 --- a/OAuthSample/Classes/ContentView.swift +++ b/OAuthSample/Classes/ContentView.swift @@ -10,7 +10,7 @@ import SwiftUI struct ContentView: View { - #if !os(tvOS) + #if canImport(WebKit) @Environment(\.openWindow) var openWindow @@ -46,7 +46,8 @@ struct ContentView: View { } case .receivedDeviceCode(_, let deviceCode): Text("To login, visit") - Text(deviceCode.verificationUri).foregroundStyle(.blue) + Text(.init("[\(deviceCode.verificationUri)](\(deviceCode.verificationUri))")) + .foregroundStyle(.blue) Text("and enter the following code:") Text(deviceCode.userCode) .padding() @@ -63,13 +64,25 @@ struct ContentView: View { var providerList: some View { List(oauth.providers) { provider in Button(provider.id) { - // Start the authorization flow (use .deviceCode for tvOS) - let grantType: OAuth.GrantType = .pkce(.init()) - oauth.authorize(provider: provider, grantType: grantType) + authorize(provider: provider) } } } - + + /// Starts the authorization process for the specified provider. + /// - Parameter provider: the provider to begin authorization for + private func authorize(provider: OAuth.Provider) { + #if canImport(WebKit) + // Use the PKCE grantType for iOS, macOS, visionOS + let grantType: OAuth.GrantType = .pkce(.init()) + #else + // Use the Device Code grantType for tvOS, watchOS + let grantType: OAuth.GrantType = .deviceCode + #endif + // Start the authorization flow + oauth.authorize(provider: provider, grantType: grantType) + } + /// Reacts to oauth state changes by opening or closing authorization windows. /// - Parameter state: the published state change private func handle(state: OAuth.State) { @@ -91,13 +104,13 @@ struct ContentView: View { } private func openWebView() { - #if !os(tvOS) + #if canImport(WebKit) openWindow(id: .oauth) #endif } private func dismissWebView() { - #if !os(tvOS) + #if canImport(WebKit) dismissWindow(id: .oauth) #endif } diff --git a/OAuthSample/Classes/Extensions/OAuth+Window.swift b/OAuthSample/Classes/Extensions/OAuth+Window.swift index 1ea4c80..c404c8c 100644 --- a/OAuthSample/Classes/Extensions/OAuth+Window.swift +++ b/OAuthSample/Classes/Extensions/OAuth+Window.swift @@ -8,6 +8,8 @@ import OAuthKit import SwiftUI +#if canImport(WebKit) + extension OAuth { /// Provides a convenience enum that identifies application scene windows that can be opened or dismissed. @@ -47,3 +49,5 @@ public extension DismissWindowAction { callAsFunction(id: id.rawValue) } } + +#endif diff --git a/OAuthSample/Classes/OAuthSampleApp.swift b/OAuthSample/Classes/OAuthSampleApp.swift index 6a55e9f..9ef714d 100644 --- a/OAuthSample/Classes/OAuthSampleApp.swift +++ b/OAuthSample/Classes/OAuthSampleApp.swift @@ -20,7 +20,7 @@ struct OAuthSampleApp: App { ContentView() } - #if !os(tvOS) + #if canImport(WebKit) WindowGroup(id: .oauth) { OAWebView(oauth: oauth) } diff --git a/README.md b/README.md index f0c3852..a3aad3c 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ ![iOS 18.0+](https://img.shields.io/badge/iOS-18.0%2B-crimson.svg) ![macOS 15.0+](https://img.shields.io/badge/macOS-15.0%2B-skyblue.svg) ![tvOS 18.0+](https://img.shields.io/badge/tvOS-18.0%2B-blue.svg) -![visionOS 2.0+](https://img.shields.io/badge/visionOS-2.0%2B-magenta.svg) +![visionOS 2.0+](https://img.shields.io/badge/visionOS-2.0%2B-violet.svg) +![watchOS 11.0+](https://img.shields.io/badge/watchOS-11.0%2B-magenta.svg) [![License: MIT](https://img.shields.io/badge/License-MIT-indigo.svg)](https://opensource.org/licenses/MIT) # OAuthKit Sample