SwiftUI view testin Gulliver
GulliverTestScreen.swift
1//
2// ContentView.swift
3// GulliverTest
4//
5// Created by Bailey Townsend on 1/20/26.
6//
7
8import SwiftUI
9import Gulliver
10import Pulse
11import PulseProxy
12import PulseUI
13import AuthenticationServices
14
15
16
17struct ContentView: View {
18 @State private var authSession: ASWebAuthenticationSession?
19 @State private var contextProvider = AuthSessionContextProvider()
20
21 var body: some View {
22 NavigationStack{
23 VStack {
24 Image(systemName: "globe")
25 .imageScale(.large)
26 .foregroundStyle(.tint)
27 Text("Hello, world!")
28 NavigationLink(destination: ConsoleView()) {
29 Text("Console")
30 }
31
32 }
33 }
34 .task {
35 do {
36
37 #if DEBUG
38 NetworkLogger.enableProxy()
39 #endif
40
41 let client = OAuthClient(clientId: "https://pace.social/oauth-client-metadata.json", redirectUri: "social.pace:/auth/callback")
42 let url = try await client.createAuthorizationURL(identifier: "baileytownsend.dev")
43 await MainActor.run {
44 presentAuthSession(url: url)
45 }
46 } catch {
47 print("Error creating authorization URL: \(error)")
48 }
49 }
50 .padding()
51 }
52
53
54 private func presentAuthSession(url: URL) {
55 // Create authentication session with callback URL scheme
56 let session = ASWebAuthenticationSession(
57 url: url,
58 callbackURLScheme: "social.pace"
59 ) { callbackURL, error in
60 Task {
61 print("Callback URL: \(String(describing: callbackURL)), Error: \(String(describing: error))")
62// await handleAuthCompletion(callbackURL: callbackURL, error: error)
63 }
64 }
65
66 // Configure the session
67 session.presentationContextProvider = contextProvider
68 session.prefersEphemeralWebBrowserSession = false
69
70 // Store and start the session
71 authSession = session
72 session.start()
73 }
74}
75
76
77class AuthSessionContextProvider: NSObject, ASWebAuthenticationPresentationContextProviding {
78 func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
79 // Return the key window
80 if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
81 let window = windowScene.windows.first(where: { $0.isKeyWindow }) {
82
83 return window
84 }
85 // Fallback: find any window scene and create a window
86 if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
87 if let window = windowScene.windows.first {
88 return window
89 }
90 // Create a new window with the window scene
91 return ASPresentationAnchor(windowScene: windowScene)
92 }
93 // Final fallback (should not reach here in normal circumstances)
94 fatalError("No window scene available for authentication presentation")
95 }
96}
97
98
99#Preview {
100 ContentView()
101}