How to Use The Snapchat Sdk (Snapkit) with Swiftui

Can I use the Snapchat SDK (SnapKit) with SwiftUI?

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

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
}

Where to place app delegate code in App.swift file?

You should use .onOpenURL instead, like

@main
struct SampleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onOpenURL { url in
// .. do whatever needed here
}
}
}
}

How to authenticate with the Box SDK using SwiftUI?

I have figured out how to get the OAuth2 login sheet for Dropbox to appear and authenticate the client using SwiftUI.

The trick is to use a Coordinator to make the UIViewControllerRepresentable satisfy a protocol.

import SwiftUI
import BoxSDK
import AuthenticationServices

var boxSDK = BoxSDK(clientId: "<Client ID>", clientSecret: "<Client Secret>")
var boxClient: BoxClient

struct BoxLoginView: View {

@State var showLogin = false

var body: some View {
VStack {
Button {
showLogin = true
} label: {
Text("Login")
}

BoxView(isShown: $showLogin)
// Arbitrary frame size so that this view does not take up the whole screen
.frame(width: 40, height: 40)
}
}
}

/// A UIViewController that will present the OAuth2 Safari login screen when the isShown is true.
struct BoxView: UIViewControllerRepresentable {

typealias UIViewControllerType = UIViewController

let letsView = UIViewController()

@Binding var isShown : Bool

// Show the login Safari window when isShown
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
if(isShown) {
getOAuthClient()
}
}

func makeUIViewController(context _: Self.Context) -> UIViewController {
return self.letsView
}

func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}

func getOAuthClient() {
boxSDK.getOAuth2Client(tokenStore: KeychainTokenStore(), context:self.makeCoordinator()) { result in
switch result {
case let .success(client):
boxClient = client
case let .failure(error):
print("error in getOAuth2Client: \(error)")
}
}
}

class Coordinator: NSObject, ASWebAuthenticationPresentationContextProviding {
var parent: BoxView

init(parent: BoxView) {
self.parent = parent
}

func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return parent.letsView.view.window ?? ASPresentationAnchor()
}
}
}


Related Topics



Leave a reply



Submit