简体   繁体   English

iOS DropBox - 浏览文件夹和查看文件 - 示例代码或教程?

[英]iOS DropBox - browse folders and view files - sample code or tutorial?

Inside of my app, I would like the user to be able to log into DropBox, and browse and view his or her files and folders.在我的应用程序中,我希望用户能够登录 DropBox,并浏览和查看他或她的文件和文件夹。 This seems like a very common task to me, so I'm wondering if there are any tutorials or sample code out there?这对我来说似乎是一项非常常见的任务,所以我想知道是否有任何教程或示例代码? I haven't been able to find anything on Google.我在谷歌上找不到任何东西。

I believe I could do it myself using a table view and the Core API tutorial from dropbox.com, but I don't want to waste all that time if the code is already out there.我相信我可以使用 table view 和来自 dropbox.com 的Core API 教程自己完成,但如果代码已经在那里,我不想浪费所有时间。

if you are looking it on Objective c you can get a demo project from this link.如果您在 Objective c 上查看它,您可以从此链接获取演示项目。 where you can browse the folder of rootdirectory and also you can download the files.您可以在其中浏览根目录的文件夹,也可以下载文件。

https://github.com/danielbierwirth/DropboxBrowser https://github.com/danielbierwirth/DropboxBrowser

I had the same problem.我有同样的问题。 It took me so long to find the answer for that, really it was overwhelming because there isn't much info about it.我花了很长时间才找到答案,真的很不知所措,因为没有太多关于它的信息。 I did this: I'm sorry for my english, is not my first language.我这样做了:对不起,我的英语不是我的母语。

Install Dropbox Chooser https://www.dropbox.com/developers/chooser#ios Install SwiftyDropbox安装 Dropbox 选择器https://www.dropbox.com/developers/chooser#ios安装 SwiftyDropbox

SwiftyDropbox installs Alamofire, we need this to download the file, if not then install it SwiftyDropbox 安装 Alamofire,我们需要它来下载文件,如果没有就安装它

Dropbox Chooser allow the user select any file from their dropbox account and returns the shared link of the selected file so: Dropbox Chooser 允许用户从他们的 Dropbox 帐户中选择任何文件并返回所选文件的共享链接,因此:

    //Ask SwiftyDropbox if the user is logged in

    if DropboxClientsManager.authorizedClient != nil{


    // Use this from Dropbox chooser to open a view controller that allow the user select the file
    //DBChooserLinkTypeDirect <--- it has to be this parameter, there is another one but is useless for our purpose

    DBChooser.default().open(for: DBChooserLinkTypeDirect, from: self, completion: { (results) in

                //Here starts the completion block of Chooser

                if let resultado = results{ //Obtain the results 

                    var d = DBChooserResult() //Create a variable of DBChooserResult type
                    d = resultado[0] as! DBChooserResult //Get the result. Just one result because the user just can select ONE file (in this version just one, it may be more but I don't know how in this moment
                    print(d.name)
                    print(d.link)//The atributte link give us the url to download the file. 
    //If you paste it in your browser the download starts right away


                  //Use Alamofire to download the file from the url
                    Alamofire.request(d.link).responseData { response in

         //Get the data from the URL
                        if let data = response.result.value
                        {
                           //Here we have the data to do whatever we want, in my case I show the PDF file in a WebView
                            print("Data de Alamofire \(response.description)")
                            self.desplegarUrlEnWebview(datos: data, url: d.link)
                        }
                    }
                }

            })
        }else{

            //If the user is not logged in we use SwiftyDropbox to log in

            DropboxClientsManager.authorizeFromController(UIApplication.shared, controller: self, openURL: { (url) in

                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            })
        }

All of the official resources for the Dropbox API and SDKs can be found on the Dropbox Developer website: Dropbox API 和 SDK 的所有官方资源都可以在 Dropbox 开发者网站上找到:

https://www.dropbox.com/developers https://www.dropbox.com/developers

Specifically, you're referring to the iOS Core SDK.具体来说,您指的是 iOS 核心 SDK。 You already mentioned the tutorial , but the SDK download itself also contains a working sample app:您已经提到了教程,但 SDK 下载本身也包含一个工作示例应用程序:

https://www.dropbox.com/developers/core/sdks/ios https://www.dropbox.com/developers/core/sdks/ios

The sample doesn't contain all of the functionality you're looking for though.该示例不包含您正在寻找的所有功能。

Well, I've gone ahead and created it.好吧,我已经继续创建了它。 Although it seems to have a problem when trying to view a pdf file.虽然在尝试查看 pdf 文件时似乎有问题。 Anyway, here is the code:无论如何,这是代码:

https://github.com/danielbierwirth/DropboxBrowser https://github.com/danielbierwirth/DropboxBrowser

and for more inforation you can refer to official developer site of dropbox.有关更多信息,您可以参考 dropbox 的官方开发者网站。

Use pod 'SwiftyDropbox'.使用 pod 'SwiftyDropbox'。

Configure your project as explained here: http://dropbox.github.io/SwiftyDropbox/api-docs/latest/ .按照此处的说明配置您的项目: http : //dropbox.github.io/SwiftyDropbox/api-docs/latest/

In your Dropbox App console remember to give Permissions that allow your app to view and manage files and folders under Permission tab.在您的 Dropbox 应用控制台中,请记住在“权限”选项卡下授予允许您的应用查看和管理文件和文件夹的权限。 You have to regenerate the access token if you make any changes in the permission and remember to change the access token in your code.如果您对权限进行任何更改,则必须重新生成访问令牌,并记住在代码中更改访问令牌。

Since dropbox doesn't has a redirect url to redirect and view our files & folders after we login, we have to create a table view controller to load our files with the metadata.由于 dropbox 没有重定向 url 来在我们登录后重定向和查看我们的文件和文件夹,因此我们必须创建一个表视图控制器来加载带有元数据的文件。 (Remember, you have to return back to your app from dropbox to view the files). (请记住,您必须从 Dropbox 返回到您的应用才能查看文件)。

If you want to use the file for uploading purpose in your app, do it with tableView delegate method 'didSeelectRowAt'.如果您想在您的应用程序中使用该文件进行上传,请使用 tableView 委托方法“didSeelectRowAt”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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