使用Dropbox SDK时无法调用AppDelegate openURL()

时间:2015-10-30 05:00:24

标签: swift dropbox-api openurl

我正在尝试将Dropbox身份验证添加到我的应用中,但它显示了这个错误:

DropboxSDK: app delegate does not implement application:openURL:sourceApplication:annotation:

以下是我的app delegate中的代码:

let dbSession = DBSession(appKey: dbAppKey, appSecret: dbAppSecret, root: kDBRootDropbox)
DBSession.setSharedSession(dbSession)

func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {

    if DBChooser.defaultChooser().handleOpenURL(url){
        return true
    }
    else if DBSession.sharedSession().handleOpenURL(url){

        if DBSession.sharedSession().isLinked(){
            return true
        }
    }
    return false
}

我创建了一个NSObject用于身份验证和处理与Dropbox API相关的其他操作。以下是我的身份验证码:

if !DBSession.sharedSession().isLinked(){
    DBSession.sharedSession().linkFromController(viewController)
}
else{
    delegate.authenticationStatus!(true, error: nil)
}

我在成功登录后收到此错误。我还在plist中设置了LSApplicationQueriesSchemes,并添加了URL类型。我无法找到我犯错的地方。

此外,我正在尝试使用

检查是否存在Dropbox应用
if UIApplication.sharedApplication().canOpenURL(NSURL(string: "dbapi-1://")!){
}

但它给了我以下错误:

canOpenURL: failed for URL: "dbapi-1://" - error: "(null)"
canOpenURL: failed for URL: "dbapi-2://1/connect" - error: "(null)"

任何帮助将不胜感激。

提前致谢。

2 个答案:

答案 0 :(得分:1)

您的应用委托似乎实现了与SDK预期不同的openURL签名。

将签名改为:

func application(application: UIApplication, openURL url: NSURL,
    sourceApplication: String?, annotation: AnyObject) -> Bool {

请注意,此方法已在iOS 9 SDK中标记为deprecated。它会继续按预期工作一段时间。

答案 1 :(得分:1)

在iOS 13或之后的版本中,向SceneDelegate.swift添加以下功能似乎可行:

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
    if let url = URLContexts.first?.url {
        print(url)
        
        if let authResult = DropboxClientsManager.handleRedirectURL(url) {
            switch authResult {
            case .success:
                print("Success! User is logged into Dropbox.")
            case .cancel:
                print("Authorization flow was manually canceled by user!")
            case .error(_, let description):
                print("Error: \(description)")
            }
        }
    }
}