首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有苹果按钮深色模式的SwiftUI登录

带有苹果按钮深色模式的SwiftUI登录
EN

Stack Overflow用户
提问于 2020-12-22 06:25:06
回答 2查看 300关注 0票数 0

我已经实现了与苹果的登录,但问题是按钮总是黑色的。我想根据用户的手机在亮/暗模式下显示它。

有没有办法做到这一点?

代码语言:javascript
复制
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
                }
            }
        )
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-21 02:47:15

代码语言:javascript
复制
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")
        }
        
    }
}
票数 1
EN

Stack Overflow用户

发布于 2021-06-16 05:55:49

首先添加

@环境(.colorScheme)变量colorScheme

然后添加依赖于变量的样式

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

在您的示例中,它将如下所示:

代码语言:javascript
复制
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)
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65400766

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档