SwiftUI使用Apple按钮暗模式登录 [英] SwiftUI Sign in with Apple button dark mode

查看:64
本文介绍了SwiftUI使用Apple按钮暗模式登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了与Apple的登录,但事实是该按钮始终是黑色的.我想以亮/暗模式显示它,具体取决于用户的手机.

I have implemented Sign in with Apple but the thing is that the button is always black. I would like to show it in light/ dark mode depending on the users phone.

有没有办法做到这一点?

Is there a way to achieve this?

import SwiftUI
import CryptoKit
import FirebaseAuth
import AuthenticationServices

struct SignInWithAppleButtonView: View {
    @State var currentNonce:String?
    
    var body: some View {
        SignInWithAppleButton(
            onRequest: { request in
                let nonce = randomNonceString()
                currentNonce = nonce
                request.requestedScopes = [.fullName, .email]
                request.nonce = sha256(nonce)
            },
            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")
                        }
                        
                        print("\(String(describing: Auth.auth().currentUser?.uid))")
                    default:
                        break
                        
                    }
                default:
                    break
                }
            }
        )
    }
}

推荐答案

import SwiftUI
import AuthenticationServices
struct SignInWithAppleButtonView: View{
    var body: some View{
        
        DynamicAppleSignIn(
            onRequest: { request in
                //Your Code
            },
            onCompletion: { result in
                switch result {
                case .success (let authResults):
                    print("Authorization successful.")
                // Your Code
                case .failure (let error):
                    print("Authorization failed: \(error)")
                // Your Code
                }
            }
        )
    }
}
struct DynamicAppleSignIn : View {
    @Environment(\.colorScheme) var colorScheme
    
    var onRequest: (ASAuthorizationAppleIDRequest) -> Void
    var onCompletion: ((Result<ASAuthorization, Error>) -> Void)
    
    var body: some View {
        
        switch colorScheme {
        case .dark:
            SignInWithAppleButton(
                onRequest: onRequest,
                onCompletion: onCompletion
            ).signInWithAppleButtonStyle(.white)
            .frame(minWidth: 140, maxWidth: 240, minHeight: 30,  maxHeight: 60, alignment: .center)
        case .light:
            SignInWithAppleButton(
                onRequest: onRequest,
                onCompletion: onCompletion
            ).signInWithAppleButtonStyle(.black)
            .frame(minWidth: 140, maxWidth: 240, minHeight: 30,  maxHeight: 60, alignment: .center)
        @unknown default:
            fatalError("Not Yet Implemented")
        }
        
    }
}

这篇关于SwiftUI使用Apple按钮暗模式登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆