I am in the early stages of developing an app. What I would like to experiment with is having the app read a txt file. Similar to the way when you have an attachment in your email and you can take on it and open in with an app that is on your iPad or iPhone.
Any help would be greatly appreciated.
Thank you,
Bob
Hi Bob,
This should give you a pointer as a way to do it.
struct ContentView: View {
@State private var textFile = ""
var body: some View {
NavigationView {
ScrollView {
VStack {
Text(textFile)
.font(.system(size: 12))
.multilineTextAlignment(.leading)
}
.padding(.horizontal)
}
.navigationTitle("Text file Viewer")
.onAppear {
readTextFile()
}
}
}
func readTextFile() {
if let textFileUrl = Bundle.main.url(forResource: "textfile", withExtension: "txt") {
if let contents = try? String(contentsOf: textFileUrl) {
textFile = contents
}
}
}
}
Just drag your text file into the Bundle, make sure the name of the file matches in your code and away you go.
If you want to read a text file from a remote server then you will need to use URLSession.
Thanks, I was looking to have the user of my app create a text (.txt file) outside of the app, say in Word. Then email it to themselves so they can click on it and use the “Open in” feature. If you have any ideas on how to get started it would be greatly appreciated.
Thank you!!
Bob Lawson
That’s a really interesting question Bob. I’m now curious myself as to how to achieve that.
So I found a Technical Q&A QA1587 from Apple Technical Q&A QA1587: How do I get my application to show up in the Open in... menu.
I can’t seem to get past step 7, seems like the “Click here to add additional document type properties” is greyed out.
Bob
I dunno if this is of any help to you. Depends if you building your App in SwiftUI but anyway, for what it is worth:
https://www.hackingwithswift.com/forums/swiftui/handling-files-opened-from-mail-attachments/6717