问题描述
将Dropbox API连接到我的应用程序时遇到问题.Dropbox API文档摆在我眼前,我照着那里写的去做.但是在所示的某些方法中有错误,并且我不知道该如何替换此处所示的实体.我可以登录,但无法获取令牌,而是出现错误:"-canOpenURL:URL失败:" dbapi-2://1/connect";-错误:操作无法完成.(OSStatus错误-10814.)"
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
)
}
}
这两种方法都是从DropboxAppi文档中复制的,但是它们不起作用,我找不到正确的解决方案.
Both of these methods are copied from the DropboxAppi documentation, but they don't work and I can't find the right solution.
推荐答案
对于SwiftyDropBox
For SwiftyDropBox
在 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
}
现在是要在其中启动身份验证过程的ViewController.在这段代码中,我正在使用UISwitch
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)")
}
}
您的Info.plist应该包含这些内容,重定向才能返回到您的应用程序
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>
这篇关于如何使用Swift 5将Dropbox API连接到iOS应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!