首页 > 解决方案 > SwiftUI:在 NavigationLink 或 Sheet 中显示时,使用 Apple 按钮登录会使应用程序崩溃

问题描述

我们正在开发一个 SwiftUI watchOS 应用程序,我们希望支持使用 Apple 功能登录。因为它没有 SwiftUI 组件,所以我们使用 WKInterfaceObjectRepresentable 来展示。最初,这显示为带有其他登录选项的第一个视图,我们没有遇到任何问题,但后来我们决定将这些添加到一些导航流程中,它开始崩溃。

只是为了澄清,下面的代码有效:

struct QWSplashScreenView: View {
    @State var showLogin: Bool = true

    var body: some View {
        QWSignInView(showEmailLogin: self.$showLogin)
    }
} 

虽然这个,不:

struct QWSplashScreenView: View {
    @State var showLogin: Bool = true

    var body: some View {
        NavigationLink(destination: QWSignInView(showEmailLogin: self.$showLogin), label: { Text("Login") })
    }
}

崩溃完全在系统级别,只是为了显示它的初始部分:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   com.apple.SwiftUI               0x010d33d2 static PlatformViewRepresentableAdaptor.platformView(for:) + 50
1   com.apple.SwiftUI               0x010d3c8c protocol witness for static PlatformViewRepresentable.platformView(for:) in conformance PlatformViewRepresentableAdaptor<A> + 28
2   com.apple.SwiftUI               0x011d7d5b PlatformViewHost.representedView.getter + 27
3   com.apple.SwiftUI               0x011e68f4 PlatformViewHost.init(_:host:environment:viewPhase:) + 340
4   com.apple.SwiftUI               0x011e2a8e PlatformViewHost.__allocating_init(_:host:environment:viewPhase:) + 46
5   com.apple.SwiftUI               0x011e235e closure #1 in PlatformViewChild.update(context:) + 1662
6   com.apple.SwiftUI               0x011ddaa9 PlatformViewChild.update(context:) + 553
7   com.apple.SwiftUI               0x011e4057 protocol witness for static UntypedAttribute._update(_:graph:attribute:) in conformance PlatformViewChild<A> + 39
8   com.apple.AttributeGraph        0x035c0791 partial apply + 33
9   com.apple.AttributeGraph        0x035a8d99 AG::Graph::UpdateStack::update() + 461

有没有人见过这种行为?(如果我也使用工作表,也会发生同样的崩溃)

QWSignInView 的代码:

struct QWSignInView: View {
    @Binding var showEmailLogin: Bool

    var body: some View {
        VStack {
            Text("Record and save your blood pressure measurements")

            VStack() {
                QWSignInWithAppleButton()
                    .frame(height:40)

                Button(action: {
                    self.showEmailLogin = true
                }) {

                    Text("I have an account")
                }
            }
        }
    }
}

和 QWSignInWithAppleButton:

final class QWSignInWithAppleButton: WKInterfaceObjectRepresentable {
    typealias WKInterfaceObjectType = WKInterfaceAuthorizationAppleIDButton

    func makeWKInterfaceObject(context: WKInterfaceObjectRepresentableContext<QWSignInWithAppleButton>) -> WKInterfaceAuthorizationAppleIDButton {
        WKInterfaceAuthorizationAppleIDButton(target: context.coordinator, action: #selector(Coordinator.buttonPressed))
    }

    func updateWKInterfaceObject(_ wkInterfaceObject: WKInterfaceAuthorizationAppleIDButton, context: WKInterfaceObjectRepresentableContext<QWSignInWithAppleButton>) {
    }

    func makeCoordinator() -> Coordinator {
        return Coordinator()
    }

    class Coordinator {
        @objc func buttonPressed() {
            appStore.loginStore.beginAppleIDSignIn()
        }
    }
}

标签: swiftswiftuiwatchos

解决方案


推荐阅读