Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在iOS中从web下载文本?_Ios_Objective C_Cocoa Touch - Fatal编程技术网

如何在iOS中从web下载文本?

如何在iOS中从web下载文本?,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我想在标签中显示来自服务器的txt文件的文本。例如,我有http://www.example.com/test.txt服务器上的文件,这里有一些文本,我想在我的应用程序中显示下载的文本作为标签。我试过了 - (IBAction)getText:(id)sender { NSURL *url = [NSURL URLWithString:@"http://http://mrrandom.cba.pl/test.txt"]; NSString *content = [NSString

我想在标签中显示来自服务器的txt文件的文本。例如,我有
http://www.example.com/test.txt
服务器上的文件,这里有一些文本,我想在我的应用程序中显示下载的文本作为标签。我试过了

- (IBAction)getText:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://http://mrrandom.cba.pl/test.txt"];
    NSString *content = [NSString stringWithContentsOfURL:url encoding:NSStringEncodingConversionAllowLossy error:nil];
    _label.text = content; 
}

但这对我不起作用。你有什么想法吗?谢谢你的帮助

您的url中有一个输入错误

而不是写作

http://http://mrrandom.cba.pl/test.txt

你应该写

http://mrrandom.cba.pl/test.txt

也应考虑使用.

在后台线程上进行“下载”。
有关在iOS上执行下载的更多信息,请参阅有关的文档。

NSURL*url=[NSURL URLWithString:@”http://mrrandom.cba.pl/test.txt"];