微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwiftUI 使用 Apple 按钮暗模式登录

如何解决SwiftUI 使用 Apple 按钮暗模式登录

我已经实现了使用 Apple 登录,但问题是按钮始终为黑色。我想根据用户手机以亮/暗模式显示

有没有办法做到这一点?

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
                }
            }
        )
    }
}

解决方法

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith(';hello'):
        await message.channel.send('Hello dirtbag')

    if message.content.startswith(';bitcoin price'):
        answer = bitcoin_price()ON
        await message.channel.send("The current price of bitcoin is ' + answer + ' USD')
,

首先添加

@Environment(.colorScheme) var colorScheme

然后添加样式依赖变量

.signInWithAppleButtonStyle(colorScheme == .dark ? .white : .black)

在你的情况下,它看起来像这样:

import SwiftUI
import CryptoKit
import FirebaseAuth
import AuthenticationServices

struct SignInWithAppleButtonView: View {
    @Environment(\.colorScheme) var colorScheme
    @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
                }
            }
        )
        .signInWithAppleButtonStyle(colorScheme == .dark ? .white : .black)
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。