swift - Can I use the Snapchat SDK (SnapKit) with 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.
Here's my code:
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() {
}
}
}
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 登录的完成处理程序将运行:
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 实例,否则它将不起作用。像这样:
let s = SCSDKLoginButton { (success, error) in
//handle login
}
推荐阅读
- python - 如何执行不涉及重新拟合 ARIMA 模型的多步超时预测?
- c# - 使随机标签位置适应窗口大小的最有效方法是什么?
- android - 如何从多个设备向单个设备发送 FCM 通知
- python - 使用 Python 2.7 的 Google Cloud ML
- java - 如何使用 java-selenium webdriver 检查 zip 文件是否已成功下载?
- ios - ICEStopConnectivityCheck() 没有找到带有呼叫 ID 的 ICE 检查
- arrays - 如何将元组转换为二维矩阵
- angularjs - 从控制器获取配置值
- php - PDO 一般错误:2006 mysql server has gone away
- c - 一个关于使用 fork() 创建的子进程的退出状态值的查询