cancel
Showing results for 
Search instead for 
Did you mean: 
Announcements
Update: Find information on Dropbox support during COVID-19 here
Close
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted

SwiftyDropBox Auth flow issue

Helpful | Level 6

I'm following the SwiftyDropbox documentation for my project, and have gotten as far as having my app switch to authenticating via the Dropbox app and switching back into my app. 

 

However, the 

application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [) -> Bool function is not being called. When I try to initialize a client via 

DropboxClientsManager.authorizedClient, the client is nil. 

 

The setupWithAppKey is in my AppDelegate, as is the above-referenced function. I know things are partially working, since when I trigger authorizeFromController, I don't get a login prompt from the Dropbox app - just an Allow button.

 

Any ideas what I'm missing or doing wrong?

 

Thanks in advance,

 

Greg

1 Accepted Solution

Accepted Solutions
Highlighted

Re: SwiftyDropBox Auth flow issue

Helpful | Level 6

Greg, that was the issue! Thanks kindly, and I wish I'd noticed that myself so I could have saved us both some time. Oy!!!!

View solution in original post

10 Replies 10
Highlighted

Re: SwiftyDropBox Auth flow issue

Dropboxer
Dropboxer
Thanks for the report! Can you share:

- the version number of SwiftyDropbox you're using
- the version(s) of iOS you're seeing this with
- the exact `open url` code you have in your app delegate

Thanks in advance!
Highlighted

Re: SwiftyDropBox Auth flow issue

Helpful | Level 6

Thanks for the quick reply, Greg.

 

SwiftyDropbox version: 4.3.0

iOS version: 11.2.2

'open URL' code (identical to reference code):

 

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [) -> Bool {
    if let authResult = DropboxClientsManager.handleRedirectURL(url) {
        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: \(description)")
        }
    }
    return true
}

  

Highlighted

Re: SwiftyDropBox Auth flow issue

Dropboxer
Dropboxer
Thanks! That looks correct. So to clarify, are you sure `open url` itself isn't getting called, or does it appears `handleRedirectURL` isn't working properly?

To check, please add some logging immediately inside `open url` (i.e., before `handleRedirectURL`). If it does get called, it would help to log and post the `open url` parameters for reference. (Just be sure to redact secret values, such as an access token.)
Highlighted

Re: SwiftyDropBox Auth flow issue

Helpful | Level 6

Hi Greg,

 

When I put a print statement directly after 'open url' and immediately before 'handleRedirectURL', the print statement is not executed (even after successfully authenticating to Dropbox and switching back to my app). 

 

Is there any other debugging I can do?

Highlighted

Re: SwiftyDropBox Auth flow issue

Dropboxer
Dropboxer
I just tried this with the same versions of SwiftyDropbox and iOS, and it is working properly for me. Unfortunately if it's not getting called at all, it sounds like an issue with iOS itself, so I can't offer much help, and you may need to debug this on Apple's side.

Can you check that you only have one `open url` definition though? If you have two by accident it may only call one of them.
Highlighted

Re: SwiftyDropBox Auth flow issue

Helpful | Level 6

Only a single 'open url' definition in the project, so that's not the issue. Oh well, I'll forego Dropbox support for the time being and hope that an update sorts things out.

 

Thanks kindly for your time and help.

 

Greg

Highlighted

Re: SwiftyDropBox Auth flow issue

Dropboxer
Dropboxer
I wasn't able to reproduce this myself, but if you can provide a sample project that reproduces the issue, we'll look into it.
Highlighted

Re: SwiftyDropBox Auth flow issue

Helpful | Level 6

Sure. I created a dummy project to test, and I get the same result. This tells me I've got to be missing something fundmental.

 

ViewController:

import UIKit

import SwiftyDropbox

 

class ViewController: UIViewController {

 

    @IBAction func launchAuth(_ sender: Any) {

        authenticateToDropBox()

    }

    @IBAction func testClient(_ sender: Any) {

        if let _ = DropboxClientsManager.authorizedClient {

            print("Got client.")

        } else {

            print("No client.")

        }

    }

    // Dropbox stuff

    fileprivate func authenticateToDropBox() {

        if DropboxClientsManager.authorizedClient == nil {

            DropboxClientsManager.authorizeFromController(UIApplication.shared,

                                                          controller: self,

                                                          openURL: { (url: URL) -> Void in

                                                            UIApplication.shared.open(url, options: [, completionHandler: nil)

            })

        }

    }

}

 

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        // Override point for customization after application launch.

        if DropboxClientsManager.authorizedClient == nil {

            DropboxClientsManager.setupWithAppKey("<redacted>")

        }

        // Setup redirect back into app after Dropbox auth

        func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [) -> Bool {

            print("open URL getting called")

            if let authResult = DropboxClientsManager.handleRedirectURL(url) {

                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: \(description)")

                }

            }

            return true

        }

        return true

    }

 

When I click the first button (launchAuth), I am taken to the authentication dialog, where I log in. I click 'Allow' and am returned to my app. When I click the second button (testClient), I get nil for the authorizedClient (i.e., prints 'No client').

 

Thanks and let me know if something's missing.

Highlighted

Re: SwiftyDropBox Auth flow issue

Dropboxer
Dropboxer
In this code, you have `open url` inside your `didFinishLaunchingWithOptions`, but it should just be its own method in your app delegate. There's a sample here:

https://github.com/dropbox/PhotoWatch/blob/master/PhotoWatch/AppDelegate.swift#L25

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?