Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

russellelly

macrumors regular
Original poster
Jun 23, 2006
139
41
Glasgow, UK
I appreciate this is probably really easy. I've spent ages trying, but getting nowhere.

I'm trying to just download the contents of a webpage (http://xmltv.radiotimes.com/xmltv/channels.dat) to an NSString (eventually split, but the whole page as one string will do for now). I've looked at the NSURL class, but anything I try results in the iPhone (sim) crashing, or an uncaught exception warning.

Again, sorry for (yet another) fairly noob question - new to Obj-C and iPhone :)
 
I actually experimented with this exact thing this morning. Here's code that works for me...

Code:
NSString *fileContents = [NSString stringWithContentsOfURL:
                                   [[NSURL alloc] initWithString:
                                           @"http://example.com/textfile"]];
 
Thanks - that does the trick :) Finally getting somewhere with my first app! Was having trouble having it as a separate method and possibly allocating the memory twice. Been treated to good old GC'ed Java for too long!
 
Be careful with synchronous URL requests on the main thread, they will lock the UI. NSURLConnection's initWithRequest will do it asynchronously. With the asynchronous route, you'll append received data to a mutable data object. However, this will eventually require a conversion to string for you, so at some point you'll still have duplicated your data, if only temporarily.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.