简体   繁体   中英

connecting Dropbox to swift 5 fails with error -canOpenURL: failed

I'm setting up an app where I want to show and modify data which are stored in a json-file. The files should be accessible either via an sftp-connection to a server or from an dropbox account. Part of my app is a view where the user can select whether the file should be accessed via sftp or dropbox and type in the necessary credentials for sftp and the filename to be accessed. Once the input is finished I start a method to check the inserted data and to check the connectivity to the specified source.

If dropbox is selected I try to launch the authorization within this method as follows:

if newAccess.credentials.accesstype == .dropbox {
            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.open(url, options: [:], completionHandler: nil) },
                scopeRequest: scopeRequest
            )
          
        }

I'm using Xcode 12.3 and test with an iPhone 8 simulator.

The sign-in workflow of dropbox is launched properly and I can type in my login data for dropbox and grant access. But after allowing the access in the dropbox view the view stays and there is no jump back to my app. Furthermore, I get the following error: 2021-01-16 06:52:20.212331+0100..[1805:55943] -canOpenURL: failed for URL: "dbapi-8-emm://1/connect?k=..........&s=" - error: "Der Vorgang konnte nicht abgeschlossen werden. (OSStatus-Fehler -10814.)

I checked several hints, not at least this: How to connect Dropbox API to iOS app with Swift 5 , but nothing fixed my issue.

If I test my app on my iPhone, where the dropboxApp is installed, the shortened authorization workflow via this app leads to a jump back to my app, but the connection to the dropbox is not authorized.

Within my dropbox account I have registered my app and created my app-key. The permissions I request are "account.info.read", "files.content.write" and "files.content.read".

I have changed the AppDelegate and the Info.plist as described in the documentation of SDK SwiftyDropbox:

App-Delegate:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    DropboxClientsManager.setupWithAppKey(dropboxAppKey) // my key
    return true
}


// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
   
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
    
}

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    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))")
          }
      }
    }
    let canHandleUrl = DropboxClientsManager.handleRedirectURL(url, completion: oauthCompletion)
    return canHandleUrl
}

Info.plist:

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>dbapi-8-emm</string>
        <string>dbapi-2</string>
    </array>
<key>CFBundleURLTypes</key>
        <array>
            <dict>
                <key>CFBundleIdentifier</key>
                <string></string>
                <key>CFBundleTypeRole</key>
                <string>Editor</string>
                <key>CFBundleURLSchemes</key>
                <array>
                    <string>db-...myKey...</string>
                </array>
        </dict>
    </array>

Has anyone an idea where my problem is? Thanks for your support.

I could fix this problem meanwhile.

In the SceneDelegate the following code has to be added:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url {
        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)
    }
}

Additionally: The initialization of the dropbox-connection has to be launched from the currently active view. In my case I wanted to use it on the version for iPad in SplitScreenMode and to launch a folder overview of the dropbox from a view modally shown on the DetailViewController. The connection has to be launched from the modal view and not via a method from the DetailViewController

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM