问题
I have a problem connecting the Dropbox API to my application. Dropbox API documentation is in front of my eyes, I do everything as it is written there. But in some of the methods that are indicated there are errors and I do not know what to replace the entities that are indicated there. I can log in, but I cannot get the token, error instead: "-canOpenURL: failed for URL: "dbapi-2://1/connect" - error: "The operation couldn’t be completed. (OSStatus error -10814.)"
import SwiftyDropbox
class AppDelegate:... {
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
//error Use of undeclared type 'DropboxOAuthCompletion'
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)
return true
}
}
import SwiftyDropbox
class ViewController:... {
func openDropboxAutorization() {
// Legacy authorization flow that grants a long-lived token.
DropboxClientsManager.authorizeFromController(UIApplication.shared,
controller: self,
openURL: { (url: URL) -> Void in
UIApplication.shared.open(url, options: [:], completionHandler: nil)
})
//New: OAuth 2 code flow with PKCE that grants a short-lived token with scopes.
//error Use of unresolved identifier 'ScopeRequest'
let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read"], includeGrantedScopes: false)
DropboxClientsManager.authorizeFromControllerV2(
UIApplication.shared,
controller: self,
loadingStatusDelegate: nil,
openURL: { (url: URL) -> Void in UIApplication.shared.openURL(url) },
scopeRequest: scopeRequest
)
}
}
Both of these methods are copied from the DropboxAppi documentation, but they don't work and I can't find the right solution.
回答1:
For SwiftyDropBox
within AppDelegate.swift
import SwiftyDropbox
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
/// Add Dropbox
DropboxClientsManager.setupWithAppKey("<YOUR_APP_KEY>")
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
var canHandleUrl = false
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))")
}
}
canHandleUrl = DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
}
return canHandleUrl
}
Now for the ViewController where you want to start the authentication Process. In this code, I am using a UISwitch
import SwiftyDropBox
@IBAction func connectDropboxSwitchClicked(_ sender: Any) {
if connectDropboxSwitch.isOn {
/// https://stackoverflow.com/a/39546950/14414215
/// openURL deprecated
let scopeRequest = ScopeRequest(scopeType: .user, scopes: ["account_info.read","files.content.write"], includeGrantedScopes: false)
DropboxClientsManager.authorizeFromControllerV2(
UIApplication.shared,
controller: self,
loadingStatusDelegate: nil,
// openURL: { (url: URL) -> Void in UIApplication.shared.openURL(url) },
openURL: { (url: URL) -> Void in UIApplication.shared.open( url, options: [:])},
scopeRequest: scopeRequest
)
} else {
print(" connectDropbox Switch:\(connectDropbox)")
}
}
your Info.plist should contain these for the redirect to come back to your app
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleIdentifier</key>
<string></string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string><YOUR API KEY></string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>dbapi-8-emm</string>
<string>dbapi-2</string>
</array>
来源:https://stackoverflow.com/questions/63409444/how-to-connect-dropbox-api-to-ios-app-with-swift-5