Simple Trick with NSURL

This is a simple little tip, but it's worth knowing that NSString and NSURL can be used together to quickly grab the contents of a web page or RSS feed. It's as simple as this:


NSURL * myURL;
myURL = [NSURL URLWithString: @"http://theocacao.com/index.rss"];

NSString * contents;
contents = [NSString stringWithContentsOfURL: myURL];

NSLog (@"%@", contents);



This will print out the contents of the Theocacao RSS feed.
Design Element
Simple Trick with NSURL
Posted Oct 4, 2005 — 4 comments below




 

Mike Zornek — Oct 04, 05 413

I've used this code myself in some personal hacking. It works but it does have side effects. Basically your main run loop stops until the full contents have finished downloading. My general reaction is that this is a fine way to do some quick prototyping or to use in personal software but you should use asynchronous loading in any production app.

Uli Kusterer — Oct 06, 05 421

This of course won't actually parse the RSS or HTML. But this also works with NSData, and even with NSDictionary and NSArray (i.e. you can download and parse a plist that way). And you can pass that into NSXML or to an NSAttributedString's initWithHTML: method. Really handy when run in a separate thread, as Mike said.

frank — Oct 07, 05 422

An asynchronous networking class in Objc is netclasses.aeruder.net (which may also be used for asynchronous loading). A regexp-based RSS parser in Ruby is Simple RSS ( tech.rufy.com/entry/83 ).

Steve — Oct 18, 05 439

I've been having some trouble with NSAttriubtedString's initWithHTML (and other init methods on Tiger). It seems like my app hangs (well, slows down), and the string is never initialized with Tiger, while it does work with Panther. I found a thread on the subject here already: http://www.idevapps.com/forum/showthread.php?t=5231

Any ideas? Thanks!




 

Comments Temporarily Disabled

I had to temporarily disable comments due to spam. I'll re-enable them soon.





Copyright © Scott Stevenson 2004-2015