为什么OAuth重定向在iOS 13中不起作用,而在iOS 12中却起作用?

时间:2019-11-09 16:42:15

标签: ios swift swiftydropbox

我正在开发应与Dropbox交互的应用程序,下载文件,进行读写,然后上传。问题是该应用程序在iOS 12中可以正常运行,但在iOS 13中不能正常运行。我认为问题出在这里,因为代码未执行,而在iOS 12模拟器中却是。

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

这是视图控制器中的代码

import UIKit
import Foundation
import SwiftyDropbox

class viewCon:UIViewController {

    @IBOutlet weak var lab: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()

    }
    @IBAction func refresh(_ sender: Any) {
        let client = DropboxClientsManager.authorizedClient
        if client == nil { lab.text = "Not logged"} else {lab.text = "Logged"}
    }

    @IBAction func login(_ sender: Any) {
        DropboxClientsManager.authorizeFromController(UIApplication.shared, controller:self, openURL: { (url: URL) -> Void in UIApplication.shared.open(url)})
    }


    @IBAction func download(_ sender: Any) {
    }
}

这是AppDelegate.swift

import UIKit
import SwiftyDropbox

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        print("init")
        DropboxClientsManager.setupWithAppKey("********")
        // Override point for customization after application launch.
        return true
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        print("1")
        if let authResult = DropboxClientsManager.handleRedirectURL(url as URL) {
            print("2")
            switch authResult {
            case .success(_): //(let token)
                //print("Success! User is logged into Dropbox with token: \(token)")
                print("Success! User is logged into Dropbox.")
            case .cancel:
                print("User canceld OAuth flow.")
            case .error(let error, let description):
                print("Error \(error): \(description)")
            }
        } else {
            print("3")
        }
        return true
    }

}

这是我的info.plist

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>db-*********</string>
            </array>
        </dict>
    </array>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>dbapi-8-emm</string>
        <string>dbapi-2</string>
    </array>

我放了print(“ x”)来了解代码是否已执行,并且我注意到ios12没问题,在iOS 13中不起作用。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

在SceneDelegate中添加此功能

 func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
      for urlContext in URLContexts {
          let url = urlContext.url

          if let authResult = DropboxClientsManager.handleRedirectURL(url) {
              switch authResult {
              case .success:
                  print("Success! User is logged into account.")

              case .cancel:
                  print("Authorization flow was manually canceled by user!")
              case .error(_, let description):
                  print("Error: \(description)")
              }
          }

      }
  }