You might see that the Dropbox Community team have been busy working on some major updates to the Community itself! So, here is some info on what’s changed, what’s staying the same and what you can expect from the Dropbox Community overall.
Forum Discussion
Michael-jamf
2 years agoHelpful | Level 6
When using upload/uploadsession I get a sessionDeinitialized from alamofire in Swift
When I try to send a test file using either upload or uploadSession I get a sessionDeinitialized error. I edit the code to generate a file that is larger or smaller than the chunksize to test both o...
- 2 years ago
Okay managed to get it working with the examples that Dropbox has on their github. With no duplicate views. I think that was something on my end where the app wasnt actually closed or something.
This is for MacOS. If you want to use iOS I would recommend looking here. It worked out of the box for iOS.
ContentView.swift
import SwiftUI import SwiftyDropbox import AppKit struct ContentView: View { func myButtonInControllerPressed() { // OAuth 2 code flow with PKCE that grants a short-lived token with scopes, and performs refreshes of the token automatically. let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read"], includeGrantedScopes: false) DropboxClientsManager.authorizeFromControllerV2( sharedApplication: NSApplication.shared, controller: nil, loadingStatusDelegate: nil, openURL: {(url: URL) -> Void in NSWorkspace.shared.open(url)}, scopeRequest: scopeRequest ) } var body: some View { VStack { Button { myButtonInControllerPressed() } label: { Text("Test Dropbox Auth") } }.frame(maxWidth: .infinity, maxHeight: .infinity) } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
YOURAPPNAMEapp.swift
import SwiftUI import SwiftyDropbox @main struct dropboxClientApp: App { @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate var body: some Scene { WindowGroup { ContentView() } } } class AppDelegate: NSObject, NSApplicationDelegate { func applicationDidFinishLaunching(_ aNotification: Notification) { DropboxClientsManager.setupWithAppKeyDesktop("app-key") NSAppleEventManager.shared().setEventHandler(self, andSelector: #selector(handleGetURLEvent), forEventClass: AEEventClass(kInternetEventClass), andEventID: AEEventID(kAEGetURL)) } @objc func handleGetURLEvent(_ event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) { if let aeEventDescriptor = event?.paramDescriptor(forKeyword: AEKeyword(keyDirectObject)) { if let urlStr = aeEventDescriptor.stringValue { let url = URL(string: urlStr)! let oauthCompletion: DropboxOAuthCompletion = { if let authResult = $0 { switch authResult { case .success: print("Success! User is logged into Dropbox.") case .cancel: print("Authorization flow was manually canceled by user!") case .error(_, let description): print("Error: \(String(describing: description))") } } } DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion) // this brings your application back the foreground on redirect NSApp.activate(ignoringOtherApps: true) } } } }
Michael-jamf
Helpful | Level 6
Ah gotcha. I was reading the doc and it seemed like if I just used DropboxClient with a shortlived token, it would do that. I have since rewritten the code, different from how the docs have it since I was not having any luck getting that to work. Edited this code to be macOS.
Now to figure out why it creates a duplicate window. Think it has something to do with the NSView but not sure.
For anyone that comes across this. Here is my code, it works but has that duplicate app window I mentioned earlier.
//ContentView.swift
import SwiftUI
import SwiftyDropbox
import AppKit
struct ContentView: View {
@State var isShown = false
var body: some View {
HStack {
VStack {
Button(action: {
self.isShown.toggle()
}) {
Text("Login to Dropbox")
.padding(.top)
}
DropboxView(isShown: $isShown)
Button {
if let client = DropboxClientsManager.authorizedClient {
print("successful login")
} else {
print("Error")
}
} label: {
Text("Test Login")
.padding(.bottom)
}
}
.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))")
}
NSApplication.shared.setActivationPolicy(.prohibited)
}
}
DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
}
}
}
}
struct DropboxView: NSViewControllerRepresentable {
typealias NSViewControllerType = NSViewController
@Binding var isShown: Bool
func updateNSViewController(_ nsViewController: NSViewController, 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(
sharedApplication: NSApplication.shared,
controller: nsViewController,
loadingStatusDelegate: nil,
openURL: { (url: URL) -> Void in NSWorkspace.shared.open(url) },
scopeRequest: scopeRequest)
}
}
func makeNSViewController(context _: Self.Context) -> NSViewController {
return NSViewController(nibName: "DBViewController", bundle: nil)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
//YOURApp.swift
import SwiftUI
import SwiftyDropbox
@main
struct dropboxClientApp: App {
init() {
DropboxClientsManager.setupWithAppKeyDesktop("app_key")
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Then you need to make a file type of View. I called mine "DBViewController" in the contentview file at the bottom. After that follow these instructions
Make sure that you have a nib file named DBViewController.xib in your Xcode project.
Open the nib file, select the File's Owner object, and set its class to NSViewController in the Identity Inspector.
Connect the view outlet of the File's Owner to the view object in the nib file.
In the makeNSViewController function of DropboxView, instantiate the DBViewController from the nib file using the init?(nibName:bundle:) initializer.
Michael-jamf
2 years agoHelpful | Level 6
Okay managed to get it working with the examples that Dropbox has on their github. With no duplicate views. I think that was something on my end where the app wasnt actually closed or something.
This is for MacOS. If you want to use iOS I would recommend looking here. It worked out of the box for iOS.
ContentView.swift
import SwiftUI
import SwiftyDropbox
import AppKit
struct ContentView: View {
func myButtonInControllerPressed() {
// OAuth 2 code flow with PKCE that grants a short-lived token with scopes, and performs refreshes of the token automatically.
let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read"], includeGrantedScopes: false)
DropboxClientsManager.authorizeFromControllerV2(
sharedApplication: NSApplication.shared,
controller: nil,
loadingStatusDelegate: nil,
openURL: {(url: URL) -> Void in NSWorkspace.shared.open(url)},
scopeRequest: scopeRequest
)
}
var body: some View {
VStack {
Button {
myButtonInControllerPressed()
} label: {
Text("Test Dropbox Auth")
}
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
YOURAPPNAMEapp.swift
import SwiftUI
import SwiftyDropbox
@main
struct dropboxClientApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
DropboxClientsManager.setupWithAppKeyDesktop("app-key")
NSAppleEventManager.shared().setEventHandler(self,
andSelector: #selector(handleGetURLEvent),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL))
}
@objc
func handleGetURLEvent(_ event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
if let aeEventDescriptor = event?.paramDescriptor(forKeyword: AEKeyword(keyDirectObject)) {
if let urlStr = aeEventDescriptor.stringValue {
let url = URL(string: urlStr)!
let oauthCompletion: DropboxOAuthCompletion = {
if let authResult = $0 {
switch authResult {
case .success:
print("Success! User is logged into Dropbox.")
case .cancel:
print("Authorization flow was manually canceled by user!")
case .error(_, let description):
print("Error: \(String(describing: description))")
}
}
}
DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
// this brings your application back the foreground on redirect
NSApp.activate(ignoringOtherApps: true)
}
}
}
}
About Dropbox API Support & Feedback
Find help with the Dropbox API from other developers.
5,877 PostsLatest Activity: 12 months agoIf 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 or Facebook.
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!