iphone - 使用 Cocoa/iPhoneSDK 获取 URL 的字符串内容的最快方法是什么?

标签 iphone objective-c http url networking

假设我想获取

的HTML
http://www.google.com

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 类发出适当的异步请求。

关于iphone - 使用 Cocoa/iPhoneSDK 获取 URL 的字符串内容的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/241860/

相关文章:

ios - 使用xcode获取iPhone应用程序 View 中的所有元素

ios - 如何在UIWebView中播放时更改嵌入视频的大小?

javascript - Stack Overflow 如何在不重新加载页面的情况下通知服务器端事件?我在 Firebug 中看不到任何请求

iphone - 如何在 Objective-C 中缓存 session 的数据

ios - 使用 presentViewController 呈现 View :animated:completion: not working when presentViewController:animated:completion: hotspot enabled

iphone - 等待围栏: failed to receive reply: 10004003 error - UIAlert WITHOUT UITextField present

iphone - 如何更改 iphone 的 UISearchBar 中的光标颜色?

ios - 如何将 UIImageview 从 A 点移动到 B 点一定程度

javascript - 如何从图像 URL 获取状态代码? JavaScript

apache - 没有 http 服务器将提供来 self 的服务器的页面