Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import and read a file in my iOS app?

Tags:

import

ios

swift

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: enter image description here Thanks in advance.

like image 301
Ethan Rappaport Avatar asked Oct 20 '25 17:10

Ethan Rappaport


1 Answers

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: enter image description here

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.

like image 193
Fahim Avatar answered Oct 23 '25 07:10

Fahim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!