我可以将 Snapchat SDK (SnapKit) 与 SwiftUI 结合使用吗?

bze*_*e12 5 snapchat swift swiftui uiviewrepresentable

我正在尝试将Snapkit与 iOS 应用程序集成,但我想使用 SwiftUI 而不是 UIKit。我已经使用 Snapkit 完成了所需的设置,现在我正在尝试让 Snapchat 登录按钮显示在我的应用程序中。我知道 Snapkit SDK 是为 UIKit 而不是 SwiftUI 制作的,但 SwiftUI 有一种方法可以使用 UIViewRepresentable 协议将 UIView 包装到 SwiftUI 中。我已经尝试实现此功能,但登录按钮仍然不显示。

\n\n

这是我的代码:

\n\n
import SwiftUI\nimport UIKit\nimport SCSDKLoginKit\n\nstruct ContentView: View {\n    var body: some View {\n        SnapchatLoginButtonView()\n    }\n}\n\nstruct ContentView_Previews: PreviewProvider {\n    static var previews: some View {\n        ContentView()\n    }\n}\n\n\nstruct SnapchatLoginButtonView: UIViewRepresentable {\n\n    func makeCoordinator() -> Coordinator {\n        Coordinator()\n    }\n\n    func makeUIView(context: Context) -> SCSDKLoginButton {\n        let s = SCSDKLoginButton()\n        s.delegate = context.coordinator\n\n        return s\n    }\n\n    func updateUIView(_ uiView: SCSDKLoginButton, context: Context) {\n    }\n\n    class Coordinator: NSObject, SCSDKLoginButtonDelegate {\n        func loginButtonDidTap() {\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我有一种感觉,我在 SCSDKLoginButton 中丢失了一些东西,但不确定它是什么,所以这里是文件 SCSDKLoginButton.h 供参考。任何帮助将不胜感激!

\n\n
//\n//  SCSDKLoginButton.h\n//  SCSDKLoginKit\n//\n//  Copyright \xc2\xa9 2018 Snap, Inc. All rights reserved.\n//\n\n#import <UIKit/UIKit.h>\n\n@protocol SCSDKLoginButtonDelegate\n- (void)loginButtonDidTap;\n@end\n\n@interface SCSDKLoginButton : UIView\n\n@property (nonatomic, weak, nullable) id<SCSDKLoginButtonDelegate> delegate;\n\n- (instancetype)initWithCompletion:(nullable void (^)(BOOL success, NSError *error))completion NS_DESIGNATED_INITIALIZER;\n\n@end\n
Run Code Online (Sandbox Code Playgroud)\n

bze*_*e12 2

@Stephen2697 正确地指出,snap sdk 尚未针对 iOS 13 构建,因为 SceneDelegate 现在处理 oauth 重定向而不是 AppDelegate。我找到了一种解决方法,使用 SCSDKLoginClient.application() 方法(为 appdelegate 制作)在场景委托中使用。这是代码,将其添加到场景委托中,传递到 Snapchat 登录的完成处理程序将运行:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    for urlContext in URLContexts {
        let url = urlContext.url
        var options: [UIApplication.OpenURLOptionsKey : Any] = [:]
        options[.openInPlace] = urlContext.options.openInPlace
        options[.sourceApplication] = urlContext.options.sourceApplication
        options[.annotation] = urlContext.options.annotation
        SCSDKLoginClient.application(UIApplication.shared, open: url, options: options)
    }
}
Run Code Online (Sandbox Code Playgroud)

只要登录按钮不出现,请确保向 SCSDKLoginButton 实例添加完成处理程序,否则它将无法工作。像这样:

let s = SCSDKLoginButton { (success, error) in
        //handle login
    }
Run Code Online (Sandbox Code Playgroud)