-
I am not using pods but rather Swift Package Manager for Firebase.
-
Here is my app file:
import SwiftUI
import Firebase
@main
struct two_screensApp: App {
init() {
FirebaseApp.configure()
}
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(UserAuth())
}
}
}
- Here is my content view:
import SwiftUI
import CoreData
import CryptoKit
import FirebaseAuth
import AuthenticationServices
struct ContentView: View {
@EnvironmentObject var userAuth: UserAuth
var body: some View {
NavigationView {
if !userAuth.isLoggedin{
LoginView()
} else {
DashboardView()
}
}
}
}
- Here is my Auth File:
import Combine
class UserAuth: ObservableObject {
@Published var isLoggedin: Bool = false
func login() {
self.isLoggedin = true
}
}
- Lastly, here is my LoginView that has the SignInWithApple Button
import SwiftUI
import AuthenticationServices
import CryptoKit
import FirebaseAuth
struct LoginView: View {
@EnvironmentObject var userAuth: UserAuth
@State var currentNonce:String?
//Hashing function using CryptoKit
func sha256(_ input: String) -> String {
let inputData = Data(input.utf8)
let hashedData = SHA256.hash(data: inputData)
let hashString = hashedData.compactMap {
return String(format: "%02x", $0)
}.joined()
return hashString
}
private func randomNonceString(length: Int = 32) -> String {
precondition(length > 0)
let charset: Array<Character> =
Array("0123456789ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz-._")
var result = ""
var remainingLength = length
while remainingLength > 0 {
let randoms: [UInt8] = (0 ..< 16).map { _ in
var random: UInt8 = 0
let errorCode = SecRandomCopyBytes(kSecRandomDefault, 1, &random)
if errorCode != errSecSuccess {
fatalError("Unable to generate nonce. SecRandomCopyBytes failed with OSStatus (errorCode)")
}
return random
}
randoms.forEach { random in
if remainingLength == 0 {
return
}
if random < charset.count {
result.append(charset[Int(random)])
remainingLength -= 1
}
}
}
return result
}
var body: some View {
NavigationView {
ZStack {
Color.orange
.ignoresSafeArea()
SignInWithAppleButton(
//Request
onRequest: { request in
let nonce = randomNonceString()
currentNonce = nonce
request.requestedScopes = [.fullName, .email]
request.nonce = sha256(nonce)
},
//Completion
onCompletion: { result in
switch result {
case .success(let authResults):
switch authResults.credential {
case let appleIDCredential as ASAuthorizationAppleIDCredential:
guard let nonce = currentNonce else {
fatalError("Invalid state: A login callback was received, but no login request was sent.")
}
guard let appleIDToken = appleIDCredential.identityToken else {
fatalError("Invalid state: A login callback was received, but no login request was sent.")
}
guard let idTokenString = String(data: appleIDToken, encoding: .utf8) else {
print("Unable to serialize token string from data: (appleIDToken.debugDescription)")
return
}
let credential = OAuthProvider.credential(withProviderID: "apple.com",idToken: idTokenString,rawNonce: nonce)
Auth.auth().signIn(with: credential) { (authResult, error) in
if (error != nil) {
// Error. If error.code == .MissingOrInvalidNonce, make sure
// you're sending the SHA256-hashed nonce as a hex string with
// your request to Apple.
print(error?.localizedDescription as Any)
return
}
print("signed in")
self.userAuth.login()
}
print("(String(describing: Auth.auth().currentUser?.uid))")
default:
break
}
default:
break
}
}
)
.frame(width: 280, height: 45, alignment: .center)
.padding(.init(top: 400, leading: 50, bottom: 20, trailing: 50))
}
}
}
}
When I run this app I get the following error:
2021-07-18 08:56:24.350661-0500 two screens[2350:796467] 8.3.0 - [GoogleUtilities/AppDelegateSwizzler][I-SWZ001014] App Delegate does not conform to UIApplicationDelegate protocol.
And this error:
ready channel-flow (satisfied (Path is satisfied), viable, interface: en0, ipv4, dns)] unregister notification for write_timeout failed
The app still starts and I see the login view with the Sign in with apple Button. When I press the button I get this error.
Optional("An internal error has occurred, print and inspect the error details for more information.")
2
Answers
I’d try what the error message says and check if your application delegate supports the UIApplicationDelegate protocol. You’re calling some code that thinks it doesn’t.
add an AppDelegate it might resolve or like mentioned just do what the error code says in output I’m trying this on my end to see if I get the same —-