我可以在 SwiftUI 中使用 Snapchat SDK (SnapKit) 吗? [英] Can I use the Snapchat SDK (SnapKit) with SwiftUI?

查看:64
本文介绍了我可以在 SwiftUI 中使用 Snapchat SDK (SnapKit) 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to integrate Snapkit with an iOS app but I want to use SwiftUI instead of UIKit. I've already done the required setup with Snapkit and now I'm trying to get the snapchat login button to display in my app. I know the Snapkit SDK is made for UIKit and not SwiftUI, but SwiftUI has a way to wrap UIViews into SwiftUI with the UIViewRepresentable protocol. I've tried implementing this but the login button still doesn't display.

这是我的代码:

import SwiftUI
import UIKit
import SCSDKLoginKit

struct ContentView: View {
    var body: some View {
        SnapchatLoginButtonView()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


struct SnapchatLoginButtonView: UIViewRepresentable {

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

    func makeUIView(context: Context) -> SCSDKLoginButton {
        let s = SCSDKLoginButton()
        s.delegate = context.coordinator

        return s
    }

    func updateUIView(_ uiView: SCSDKLoginButton, context: Context) {
    }

    class Coordinator: NSObject, SCSDKLoginButtonDelegate {
        func loginButtonDidTap() {
        }
    }
}

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

I have a feeling I'm missing something from within SCSDKLoginButton, but not sure what it is so here's the file SCSDKLoginButton.h for reference. Any help would be greatly appreciated!

//
//  SCSDKLoginButton.h
//  SCSDKLoginKit
//
//  Copyright © 2018 Snap, Inc. All rights reserved.
//

#import <UIKit/UIKit.h>

@protocol SCSDKLoginButtonDelegate
- (void)loginButtonDidTap;
@end

@interface SCSDKLoginButton : UIView

@property (nonatomic, weak, nullable) id<SCSDKLoginButtonDelegate> delegate;

- (instancetype)initWithCompletion:(nullable void (^)(BOOL success, NSError *error))completion NS_DESIGNATED_INITIALIZER;

@end

推荐答案

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

@Stephen2697 was right in pointing out that the snap sdk isn't built for iOS 13 yet due to SceneDelegate now handling oauth redirects rather than AppDelegate. I figured out a workaround to use the SCSDKLoginClient.application() method (which was made for appdelegate) to be used in scene delegate. Here's the code, add it to your scene delegate and the completion handler passed in to your Snapchat login will run:

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

至于登录按钮没有出现,请确保向 SCSDKLoginButton 实例添加完成处理程序,否则它将无法工作.像这样:

As far as the login button not appearing, make sure to add a completion handler to the SCSDKLoginButton instance or else it won't work. Like this:

let s = SCSDKLoginButton { (success, error) in
        //handle login
    }

这篇关于我可以在 SwiftUI 中使用 Snapchat SDK (SnapKit) 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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