如何使用Swift 5将Dropbox API连接到iOS应用

问题描述

将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,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>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...