cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
We are making some updates so the Community might be down for a few hours on Monday the 11th of November. Apologies for the inconvenience and thank you for your patience. You can find out more here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: SwiftyDropbox SwiftUI iOS

SwiftyDropbox SwiftUI iOS

Fleaurent
Explorer | Level 4

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

- AppDelegate.swift


I got stuck when implementing the Authentication Flow in my ContentView.swift:

are there any simple example projects using SwiftyDropbox with SwiftUI?

 

I get

1. a warning: 'openURL' was deprecated in iOS 10.0  

2. errors: 

-canOpenURL: failed for URL: "dbapi-2://1/connect?k=[MY_APP_KEY_:)]=" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"

-canOpenURL: failed for URL: "dbapi-8-emm://1/connect?k=[MY_APP_KEY_:)]=" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"

[Presentation] Attempt to present <SwiftyDropbox.MobileSafariViewController: 0x15d00ea00> on <TestSwiftyDropbox.ViewController: 0x15be06540> (from <TestSwiftyDropbox.ViewController: 0x15be06540>) whose view is not in the window hierarchy.

 

ContentView.swift

 

 

 

 

struct ContentView: View {
    var viewController = ViewController()
    var body: some View {
        VStack{
            Text("Dropbox Test")
                 
            if DropboxClientsManager.authorizedClient != nil {
                Button(action: {
                    DropboxClientsManager.unlinkClients()  // logout()
                }, label: {
                    Text("Dropbox Logout")
                })
            } else {
                Button(action: {
                    viewController.authenticate()
                }, label: {
                    Text("Dropbox Login")  // Authorization
                })
            }
            
        }
    }
}

class ViewController: UIViewController {
    func authenticate() {
        // a) Legacy authorization flow that grants a long-lived token.
        DropboxClientsManager.authorizeFromController(UIApplication.shared,
                                                      controller: self,
                                                      openURL: { (url: URL) -> Void in
                                                        UIApplication.shared.openURL(url)
                                                      })
        // b) New: OAuth 2 code flow with PKCE that grants a short-lived token with scopes.
//          DropboxClientsManager.authorizeFromControllerV2(
//              UIApplication.shared,
//              controller: self,
//              loadingStatusDelegate: nil,
//              openURL: { (url: URL) -> Void in UIApplication.shared.openURL(url) },
//              scopeRequest: scopeRequest
//          )
    }
}

 

 

 

 

 

Any hints are appreciated as I am new to UIKit and just implemented everything with SwiftUI!

Best regard,

Fleaurent

6 Replies 6

Greg-DB
Dropbox Staff

Unfortunately the SwiftyDropbox SDK wasn't built for or tested with SwiftUI, so we don't have any resources or examples for that. I'll pass this along as a request for official support for that, but I can't promise if/when that would be done.

Fleaurent
Explorer | Level 4

OK, thank you for the response!

Fleaurent
Explorer | Level 4

I found the following issue regarding SwiftyDropbox and SwiftUI: 

I just removed the ScrollView which made Problems, and it seems to work:

 

struct ContentView : View {
    @State var isShown = false
    
    var body : some View {
            VStack {
                Text("HI!")
                Button(action: {
                    self.isShown.toggle()
                }) {
                    Text("Dropbox....")
                }
                DropboxView(isShown: $isShown)
            }
    }
}


struct DropboxView: UIViewControllerRepresentable {
    typealias UIViewControllerType = UIViewController
    
    @Binding var isShown : Bool

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
        if isShown {
            DropboxClientsManager.authorizeFromController(UIApplication.shared,
            controller: uiViewController,
            openURL: { (url: URL) -> Void in
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            })
        }
    }

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

 

--> the authorisation page of Dropbox opens and I can enter my credentials.

 

My next Problem: when I check the DropcoxClientsManager afterwards, it still contains nil

if DropboxClientsManager.authorizedClient != nil {

 

aspiguel
New member | Level 2

Fleaurent,

  Thank you for being a pioneer of Dropbox and SwiftUI.  I've just started playing around with this kit in swiftUI and I'm wondering if you got past your last issue the client manager being nil?  Thanks!

himike12
Helpful | Level 5

Has anyone been able to solve DropboxClientsManager.authorizedClient always equal to nil with SwiftUI?

himike12
Helpful | 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.

Need more support?