如何使用 iPhone SDK 下载文本文件? [英] How to download a text file with iPhone SDK?
本文介绍了如何使用 iPhone SDK 下载文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是开发基于视图的 iPhone 应用程序的新手,我需要下载这个txt"文件从互联网上删除并将其保存到应用程序的文档文件夹中.谁能简单地告诉我如何做到这一点?txt 文件很小,所以我不会需要任何用户界面对象...
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...
谢谢,
凯文
推荐答案
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
}
然后你可以使用:
[[NSUserDefaults standardUserDefaults] setObject:myTxtFile forKey:@"MyFile"];
并检索它:
NSString *myTxtFile = [[NSUserDefaults standardUserDefaults] stringForKey:@"MyFile"];
更新以反映 Joe 的意见
这篇关于如何使用 iPhone SDK 下载文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文