We’re Still Here to Help (Even Over the Holidays!) - find out more here.
Forum Discussion
Fleaurent
5 years agoExplorer | Level 4
SwiftyDropbox SwiftUI iOS
Hey all,
I've tried to add the SwiftyDropbox package into my SwiftUI iOS project and followed the GitHub readme:
I updated the following files as recommended using my app key:
- Info.plist...
himike12
4 years agoHelpful | Level 5
I have successfully authenticated authenticated with SwiftyDropbox using SwiftUI. I hope that this is useful.
Setup info.plist as the SwiftyDropbox readme instructs.
<app name>.swift
import SwiftUI
import SwiftyDropbox
@main
struct DropboxTestApp: App {
init() {
DropboxClientsManager.setupWithAppKey("<app key>")
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
ContentView.swift
import SwiftUI
import SwiftyDropbox
struct ContentView: View {
@State var isShown = false
var body: some View {
VStack {
Button(action: {
self.isShown.toggle()
}) {
Text("Login to Dropbox")
}
DropboxView(isShown: $isShown)
Button {
if let client = DropboxClientsManager.authorizedClient {
print("successful login")
} else {
print("Error")
}
} label: {
Text("Test Login")
}
}
.onOpenURL { url in
let oauthCompletion: DropboxOAuthCompletion = {
if let authResult = $0 {
switch authResult {
case .success:
print("Success! User is logged into DropboxClientsManager.")
case .cancel:
print("Authorization flow was manually canceled by user!")
case .error(_, let description):
print("Error: \(String(describing: description))")
}
}
}
DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
}
}
}
struct DropboxView: UIViewControllerRepresentable {
typealias UIViewControllerType = UIViewController
@Binding var isShown : Bool
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
if isShown {
let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read", "files.metadata.write", "files.metadata.read", "files.content.write", "files.content.read"], includeGrantedScopes: false)
DropboxClientsManager.authorizeFromControllerV2(
UIApplication.shared,
controller: uiViewController,
loadingStatusDelegate: nil,
openURL: { (url: URL) -> Void in UIApplication.shared.open(url, options: [:], completionHandler: nil) },
scopeRequest: scopeRequest)
}
}
func makeUIViewController(context _: Self.Context) -> UIViewController {
return UIViewController()
}
}
You do not need create an AppDelegate.
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.
If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.
For more info on available support options for your Dropbox plan, see this article.
If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!