xcode 如何使用objective-c和xcode从网站读取文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4531514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to read a file from a website using objective-c & xcode
提问by bhappy
I am trying to write a code that will grab some information provided by my server (remote), for example request a url that will return data that will be represented in the application after parsing it.
我正在尝试编写一个代码来获取我的服务器(远程)提供的一些信息,例如请求一个 url,该 url 将返回解析后将在应用程序中表示的数据。
I've been trying since 2 days now I googled it i found some incomplete solution but nothing really worked out for me
我从 2 天开始一直在尝试,现在我用谷歌搜索了它我找到了一些不完整的解决方案,但没有什么真正适合我
I am really noob in Xcode and Objective-C
我真的是 Xcode 和 Objective-C 的菜鸟
Thanks
谢谢
回答by jsadfeew
Clickfor the URL-Loading documentation provided by Apple.
Especially Using NSURLConnection
looks interesting for you.
单击以获取 Apple 提供的 URL 加载文档。Using NSURLConnection
对你来说特别有趣。
Edit: Another verygood and easy-to-use Framework for this task is ASIHTTP: Click
编辑:此任务的另一个非常好且易于使用的框架是 ASIHTTP: 单击
The easiest way:
最简单的方法:
- (void)grabURL
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
}
}
Asynchronous loading is only slightly more complex:
异步加载只是稍微复杂一点:
- (void)grabURLInBackground
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}