I'm migrating an old Swift app to SwiftyDropbox 6.0.3 and short-lived tokens with refresh; our app needs offline access. But with the code changes, I'm getting odd behaviour -- the completion handler in DropboxClientsManager.handleRedirectURL is never being called, with either a success or an error. It just quietly fails and DropboxClientsManager.authorizedClient remains nil.
Here's my code fragment, which is basically the old sample code. First, I've set the event handler:
NSAppleEventManager.shared().setEventHandler(self,
andSelector: #selector(handleGetURLEvent),
forEventClass: AEEventClass(kInternetEventClass),
andEventID: AEEventID(kAEGetURL))
Then, there's the function which now calls authorizeFromControllerV2:
func launchAuthorizeURL() {
let scopeRequest = ScopeRequest(scopeType: .user, scopes: [], includeGrantedScopes: true)
DropboxClientsManager.authorizeFromControllerV2(
sharedWorkspace: NSWorkspace.shared,
controller: viewController,
loadingStatusDelegate: nil,
openURL: { (url: URL) -> Void in
NSLog("DbXConnect opening Dropbox URL")
NSWorkspace.shared.open(url)
},
scopeRequest: scopeRequest // I've also tried with a nil scopeRequest
)
}
And the receiving end:
@objc func handleGetURLEvent(_ event: NSAppleEventDescriptor?, replyEvent: NSAppleEventDescriptor?) {
if let aeEventDescriptor = event?.paramDescriptor(forKeyword: AEKeyword(keyDirectObject)) {
if let urlStr = aeEventDescriptor.stringValue {
// It gets here...
let url = URL(string: urlStr)!
let oauthCompletion: DropboxOAuthCompletion = {
print("HandleGetURLEvent redirect completion handler running") // It doesn't get here!
if let authResult = $0 {
switch authResult {
case .success (let newToken):
print("handleGetURLEvent: Success! User is logged into Dropbox.")
/* here I do my own setup */
// Launch confirmation page separately
NSWorkspace.shared.open(URL(string: "https://mysite/response.html")!)
case .cancel:
print("handleGetURLEvent: Authorization flow was manually canceled by user!")
case .error(let errCode, let description):
print("HandleGetURLEvent Error: \(description)")
}
}
}
let bResult = DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
// this brings your application back to the foreground on redirect
NSApp.activate(ignoringOtherApps: true)
}
}
client = DropboxClientsManager.authorizedClient
if (client == nil) {
// It reports an error here.
}
}
This code worked fine with the old authorizeFromController call (and still does if I switch back that one call).
On a related note -- what's the best practice for how to specify offline token access with SwiftyDropbox? Is that made available by default with authorizeFromControllerV2?