如何在iOS应用中导入和读取文件? [英] How do I import and read a file in my iOS app?

查看:684
本文介绍了如何在iOS应用中导入和读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的文本文件导出和导入困境的第二部分。现在,我有写代码和导出代码。拼图的最后部分是导入代码和读取代码。我想从电子邮件中获取附件并将​​其导入应用程序,因此我可以阅读它。我有基本的读代码:

  @IBAction func readButton(_ sender:UIButton){
let filePath = getDocumentsDirectory ()。appendingPathComponent(matrixFile.txt)
do {
try displayField.text = String(contentsOfFile:path!,
encoding:String.Encoding.utf8)
}抓住错误,因为NSError {
displayField.text = String(描述:错误)
}
}

但是我也想知道导入时filePath会是什么。我调整了info.plist以便导出,但是我还需要对其进行更改吗?以下是我目前的新增内容:

提前致谢。

解决方案

为了打开通过电子邮件附件进入的文档,您需要让您的应用程序显示在Open in ...菜单列表中。根据



更新:
正如@LeoDabus所提到的,你需要通过 UIApplicationDelegate 应用程序实现实际打开文件(_ app:UIApplication,open url:URL,选项:[UIApplicationOpenURLOptionsKey:Any] = [:]) - > Bool 方法。我想你已经知道了这一部分,但如果你需要额外的帮助,请在这里问一下我会回复。


This is sort of a part two for my text file exporting and importing dilemma. Right now, I have the write code and export code. The final pieces to the puzzle is import code and read code. I want to take an attachment from an email and import it into the app, so I can read it. I have the basic read code:

@IBAction func readButton(_ sender: UIButton) {
    let filePath = getDocumentsDirectory().appendingPathComponent("matrixFile.txt")
    do {
        try displayField.text = String(contentsOfFile: path!,
                                       encoding: String.Encoding.utf8)
    } catch let error as NSError{
        displayField.text = String(describing: error)
    }
}

But I was also wondering what filePath would be when importing. I adjusted the info.plist for exportation, but would I need to make any more changes to that? Here are my current additions: Thanks in advance.

解决方案

In order for you to open a document coming in via e-mail attachment, you would need to have your application appear in the Open in ... menu list. As per the relevant Apple Documentation you have to:

A: You need to register the document types that your application can open with iOS. To do this you need to add a document type to your app’s Info.plist for each document type that your app can open. Additionally if any of the document types are not known by iOS, you will need to provide an Uniform Type Identifier (UTI) for that document type.

I know you have added the Exported Type UTI already (as shown in your screenshot) but did you follow the instructions on the above linked page to add a Document Type using the UTI you defined? If you didn't, you should add the Document Type as well since, I believe, it's the Document Type which identifies that your app as being able to handle that particular type of document.

Also, since txt is a known format, you probably don't need to define the UTI again for text files. You might be able to just use the existing UTI for text files in your Document Type definition.

In fact, I tested the above by creating a quick test app and all I had to add to the app was the Document Type, (without a UTI definition since text is a known type) in order for me to see my test app in the Open in... list. Here's my Document Type declaration:

Update: As @LeoDabus mentions, you do need to implement actually opening the file via the UIApplicationDelegate's application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool method. I figured you were aware of this part, but if you need additional help with this, do ask here and I'll respond.

这篇关于如何在iOS应用中导入和读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆