使用Cocoa / iPhoneSDK获取URL的String内容的最快方法是什么? [英] What is the quickest way to get the String contents of a URL using Cocoa/iPhoneSDK?
问题描述
说我想获取
http://www.google.com
使用Cocoa Touch框架的一些内置类作为String。
as a String using some built-in classes of the Cocoa Touch framework.
我需要编写的代码量是多少?
What is the least amount of code I need to write?
我已经走到了这一步,但无法弄清楚如何进步。必须有一个更简单的方法。
I've gotten this far, but can't figure out how to progress. There must be an easier way.
CFHTTPMessageRef req;
NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
req = CFHTTPMessageCreateRequest(kCFAllocatorDefault,
CFSTR("GET"),
(CFURLRef)url,
kCFHTTPVersion1_1);
推荐答案
最快方式是使用NSString的 + stringWithContentsOfURL:
方法。但是,这是一个模态调用,并且您的应用程序在运行时将无响应。您可以将其移动到后台线程,或使用NSURLConnection类来发出正确的异步请求。
The quickest way is to use NSString's +stringWithContentsOfURL:
method. However, this is a modal call, and your application will be non-responsive while it runs. You can either move it to a background thread, or use the NSURLConnection class to make a proper, asynchronous request.
这篇关于使用Cocoa / iPhoneSDK获取URL的String内容的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!