View Full Version : Downloading a text file from internet to iPhone

Nov 2, 2008, 08:31 AM
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 :)

Nov 2, 2008, 02:25 PM
I actually experimented with this exact thing this morning. Here's code that works for me...

NSString *fileContents = [NSString stringWithContentsOfURL:
[[NSURL alloc] initWithString:

Nov 2, 2008, 04:10 PM
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!

Nov 2, 2008, 11:06 PM
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.