[英]How to connect Dropbox API to iOS app with Swift 5
我在将 Dropbox API 连接到我的应用程序时遇到问题。 Dropbox API 文档就在我眼前,我做的一切都是在那里写的。 但是在某些指示的方法中存在错误,我不知道用什么来替换那里指示的实体。 我可以登录,但无法获取令牌,而是出现错误:“-canOpenURL:URL 失败:“dbapi-2://1/connect” - 错误:“操作无法完成。 (OSStatus 错误 -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 文档中复制的,但它们都不起作用,我也找不到合适的解决方案。
对于 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
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 应该包含这些,以便重定向返回到您的应用程序
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.