views:

131

answers:

3

How can i download the html content of a webpage given an url? using objective c..

+1  A: 

Depends on what you are trying to do. For a web view:

webView = [[UIWebview alloc] loadRequest:(NSURLRequest *)request];
mahboudz
+3  A: 

Assuming it's on iPhone or Mac OS X, use the sample here:

http://developer.apple.com/mac/library/DOCUMENTATION/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

Linking CURL with Objective C might be possible, but why?

Seva Alekseyev
+2  A: 

Use stringWithContentsOfURL:usedEncoding:error: from NSString

NSURL * url = ... some url
NSError * error;
NSStringEncoding * encoding;
NSString * htmlContent = [NSString stringWithContentsOfURL:url usedEncoding:&encoding error:&error];
Brandon Bodnár
While this will work, you should never do this from the main thread as it will block until the call returns, which may be a long time if there are network issues. You should either do this on a separate thread or use the asynchronous methods of `NSURLConnection` instead.
Rob Keniger
correct, that you should not do this in the main thread, but with how easy Operation Queues are to use, it would make more sense to use them as opposed to redoing what NSString already does if you want all the contents of the URL. A good tutorial for Operation Queues is at http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/
Brandon Bodnár