Ali
Ali

Reputation: 1

Xcode: Read text file from URL

I'm new to Xcode. I have the following code working perfectly to read a text file located in the project bundle.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"file" ofType:@"txt"];
NSString *textFromFile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

My question is how to read the text file from URL?

Upvotes: 0

Views: 4217

Answers (2)

Ajy_Chikn
Ajy_Chikn

Reputation: 3

I Found a Way to Read the File By Doing This:

let str = "Text Here"
let filename = getDocumentsDirectory().appendingPathComponent("file.txt")
do {
    try str?.write(to: filename, atomically: true, encoding: String.Encoding.utf8)
}catch {
    print("Something Went Wrong")
}

Upvotes: 0

Marcelo Cantos
Marcelo Cantos

Reputation: 185862

It's a bit more involved than reading a file. Use the NURLConnection class.

Upvotes: 2

Related Questions