I am sort of new to developing view-based iPhone applications, and I need to download this "txt" file off the internet and save it into the documents folder of the app. Can anyone show me simply how I can do this? The txt file is of a tiny size, so I wouldn't need any User interface objects...
我是开发基于视图的iPhone应用程序的新手,我需要从互联网上下载这个“txt”文件并将其保存到应用程序的文档文件夹中。谁能告诉我我怎么做到这一点? txt文件很小,所以我不需要任何用户界面对象......
Thanks,
Kevin
2 个解决方案
#1
11
NSError *err = nil;
NSString *url = [[NSString stringWithFormat:@"http://myurl.com/mypage"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *myTxtFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&err];
if(err != nil) {
//HANDLE ERROR HERE
}
Then to save it you can use:
然后保存它你可以使用:
[[NSUserDefaults standardUserDefaults] setObject:myTxtFile forKey:@"MyFile"];
And to retrieve it:
并检索它:
NSString *myTxtFile = [[NSUserDefaults standardUserDefaults] stringForKey:@"MyFile"];
Updated to reflect Joe's input
更新以反映Joe的输入
#2
1
Hey you can write the string content to the file in the document folder like this:
嘿,您可以将字符串内容写入文档文件夹中的文件,如下所示:
//get the documents directory:
//获取文档目录:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
//使用文档目录创建文件名以写入数据:
NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",documentsDirectory];
//save content of myTxtFile to the documents directory
//将myTxtFile的内容保存到文档目录中
**[myTxtFile writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];**
After executing this, you'll find a file named textfile.txt in the document folder with the content stored in it..
执行此操作后,您将在文档文件夹中找到一个名为textfile.txt的文件,其中存储了内容。
Cheers!!!
#1
11
NSError *err = nil;
NSString *url = [[NSString stringWithFormat:@"http://myurl.com/mypage"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *myTxtFile = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&err];
if(err != nil) {
//HANDLE ERROR HERE
}
Then to save it you can use:
然后保存它你可以使用:
[[NSUserDefaults standardUserDefaults] setObject:myTxtFile forKey:@"MyFile"];
And to retrieve it:
并检索它:
NSString *myTxtFile = [[NSUserDefaults standardUserDefaults] stringForKey:@"MyFile"];
Updated to reflect Joe's input
更新以反映Joe的输入
#2
1
Hey you can write the string content to the file in the document folder like this:
嘿,您可以将字符串内容写入文档文件夹中的文件,如下所示:
//get the documents directory:
//获取文档目录:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//make a file name to write the data to using the documents directory:
//使用文档目录创建文件名以写入数据:
NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",documentsDirectory];
//save content of myTxtFile to the documents directory
//将myTxtFile的内容保存到文档目录中
**[myTxtFile writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];**
After executing this, you'll find a file named textfile.txt in the document folder with the content stored in it..
执行此操作后,您将在文档文件夹中找到一个名为textfile.txt的文件,其中存储了内容。
Cheers!!!