Your workflow is unique 👨💻 - tell us how you use Dropbox here.
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
2 years agoHelpful | Level 6
All the examples I have seen so far do not mention keeping a session alive.
An example from 2015.
The github docs to try out the api.
A github gist.
When I try to add `await` to the uploadstart all I get is "No 'async' operations occur within 'await' expression" or when I add it inside for the appendV2 I get "Cannot pass function of type '(Files.UploadSessionStartResult?, CallError<Files.UploadSessionStartError>?) async -> Void' to parameter expecting synchronous function type"
Even tried the create folder and download
func createFolder(path: String) {
dbxClient.files.createFolderV2(path: path).response { response, error in
if let response = response {
print(response)
} else if let error = error {
print(error)
}
}
}
func downloadFile(path: String) {
dbxClient.files.download(path: path).response { response, error in
if let response = response {
print(response)
} else if let error = error {
print(error)
}
}
}What am I missing?
Greg-DB
Dropbox Community Moderator
2 years agoThis isn't a matter of changing your code to use 'async'/'await' operations. This issue occurs when the client variable itself is deinitialized, which severs the network connection performing the API call.
In typical cases, the app would use the authorizeFromControllerV2 method and get the client from the authorizedClient singleton like in the readme, which would make this easier. I see you're not using that flow though; you can certainly create and manage your own client object as you do, but you'll need to make sure you keep a reference to it for the entire lifetime of the requests so that they can complete successfully.
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!