If my blog helped you and your business, I'd like to ask you to help me raise money to secure drinking water to 25 people in a developing country.
It's charity, good karma and a chance to give back to the people. Learn more.
Update your iPhone app with new content – Part 2

And when the connection is ready downloading I’ll wrap up the download and will update the UI:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
	[self updateDownloaded];
	[filesize release];
	[connection release];
}

That was not hard at all wasn’t it? Yet so much better to have the progress bar – I specially made quite larger update file for demo application in the Part 2 of the tutorial. Fire it up and update from the Internet – you’ll see the progress bar filling up.

More effectively managing the threads for downloading and updating the UI

If you have a deeper look inside the code of the demo app you’ll see quite a big change. There’s no more InvocationOperation and threads found.

How come? Normally you’d have a worker thread which does the heavy work and the main thread updating the UI. That’s definitely true, but I wanted to exploit the fact that NSURLConnection is already multithreaded – it calls the messages on its delegate on the main thread, while it does the downloading in a separate thread. This way my code looks much easier, there’s less of it – and I’m letting Apple do my work of taking care of spawning new threads.

In a small app like that probably the effect is not that stunning, but when you have 30 – 40 thousand lines of code any code which can be simpler totally pays off.

Accessing your test server

That’s probably not immediately clear to everyone, so I am just mentioning it. Your iPhone device is not your development computer- thus when you access your local test server you cannot use a URL like “http://localhost/myfile.zip” for your update file. When the app runs on the iPhone localhost won’t point to anywhere. Furthermore if you have a hosts entry on your computer to have a name for your localhost like “http://mytestmachine/” and you point that to your localhost web server, that won’t work either. To make it just a bit more difficult Apple also does not provide you means to change your hosts file on your iPhone.

So, in the end – for your update file you need to have it on a server which will be visible and accessible from your iPhone device. Either put it on a real web server (as my app uses the update file from touch-code-magazine.com) or you need to have a custom DNS entry for your test server (ask the IT department)

The demo application for Part 2 of the tutorial

Now all that is left for you is to download the code, have a deep look; hopefully with all the explanations up to now you’ll get the code pretty fast.

Fire the app up and you’ll see a screen saying :

No images are included with the app; close the alert and push the button to update from Internet. You’ll see the progress bar appear and as the update file is downloaded show the progress of the operation.

Next the contents.plist is  read and a new alert pops up showing the file list of the current update along the details included with the update file:

Just to play more you can hit few more times the update button or click on the thumbnails to see the full images.

That’s it for today, download the source code here : the demo Xcode project. If you liked the tutorial or have further questions leave a comment below or ping me on Twitter. If there is nice further discussion I can always wrap a Part 3.

Previous page of "Update your iPhone app with new content – Part 2"



Marin Todorov

is an independent iOS developer and publisher. He's got more than 18 years of experience in a dozen of languages and platforms. This is his writing project.
» Marin's homepage    » Contact    » Marin's Cocos2D game creation course

  1. Mark on Monday 5, 2010

    You simply rock

  2. DAVID on Monday 5, 2010

    you are an absolute legend. This has allowed me to complete my project which I have been looking for a way around this problem on for months!

    Thank you!

  3. Marin on Monday 5, 2010

    :) ) thanks David, glad I helped

  4. juan on Monday 5, 2010

    hi….

    i wonder if its posible to make a magazine reader on this way….. through the in app purchase…..

    anyone knows about it?? anyone knows how to do it? (the magazine downloader and reader )

  5. Joe Morgan on Monday 5, 2010

    Really nice tutorials Marin.

    I haven’t dug very deep into your app yet (Will do today though).

    It looks as though the download link is hard wired to a url specific address. I want to (try to) expand your app by getting it to read a custom php script when opened. This way it could inform you that new data is available for download.

    Somehow I’ll have to send info of zips already downloaded (maybe from a basic IOS DB or log file) to PHP script which will then compare with zip packages on server.

    Should be fun, don’t hold your breath though :)

  6. Marin on Monday 5, 2010

    Hey Joe,

    yes what you describe is what an app would normally do – read the output of a server script (try JSON or just PLIST), then try to download files and update.
    One hint: if your app always updates downloads ordered updates do this – 1) when you request the JSON from the server pass as a param the date/time of the last successful update of the app 2) the server script then should take this date and build JSON output with only the updates you created after this date 3) the app downloads them and saves on the iPhone the current date/time

    hope that helps a bit, Marin

  7. [...] a Part 1 of a 2 parts tutorial, if you are returning visitor you should definitely check out Part 2. If you’re a newcomer have a look at this one and the attached demo application and then head [...]

  8. Tony on Monday 5, 2010

    Marin
    This is great. I was wondering if you could write something about how you prevented people from taking your app and buying an addin then placing it on github or something similar to prevent revenue from others buying it. You mentioned this problem on your site or blogs somewhere!
    -Tony