简体   繁体   English

Swift,Xcode,iOS-如何处理通过“在...中打开”从另一个应用程序发送到我自己的应用程序的文件

[英]Swift , Xcode, iOS - How to handle a file sent with 'Open in…' from another app to my own app

I am developing an app that reads csv files and display its content in a tableViewController. 我正在开发一个读取csv文件并在tableViewController中显示其内容的应用程序。 When doing my testings using Xcode, I have a sample file within the projects directory and I am perfectly able to read that file using its location path. 使用Xcode进行测试时,我在projects目录中有一个示例文件,并且能够使用其位置路径完全读取该文件。 The problem that I have is I want to be able to take any csv file (sent to me via some method), click the 'Open in' button and have the file sent to my app. 我遇到的问题是我希望能够获取任何csv文件(通过某种方法发送给我),单击“打开方式”按钮,然后将文件发送到我的应用程序。

My app is being displayed in the available applications to which I can send the file to. 我的应用程序显示在可以向其发送文件的可用应用程序中。 My question is what happens after? 我的问题是以后会发生什么? When I choose to send it to my app it then switches over to my app and from there, I don't know how to receive the file and read it to extract its content. 当我选择将其发送到我的应用程序时,然后切换到我的应用程序,然后从那里切换到我的应用程序,我不知道如何接收文件并读取文件以提取其内容。 Where does the file go and how do I interact with it? 文件在哪里,如何与它交互?

Thank you Yann 谢谢你Yann

This is handled in your AppDelegate , more precisely, you get passed an URL to the document and then you handle it from there in optional function, eg: 这是在您的AppDelegate处理的,更确切地说,您将URL传递给文档,然后从此处通过可选功能对其进行处理,例如:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    do {
        let data = try Data(contentsOf: url)
        // Do something with the file
    } catch {
        print("Unable to load data: \(error)")
    }

    return true
}

More info: https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623112-application 更多信息: https : //developer.apple.com/documentation/uikit/uiapplicationdelegate/1623112-application

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

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