iPhone Bootcamp Day 5
iPhone Bootcamp Day 5
Join the DZone community and get the full member experience.Join For Free
Today is Day 5 — the final day — of the iPhone bootcamp at Big Nerd Ranch,taught by Joe Conway. The last day of Big Nerd Ranch bootcamps arehalf-days so you have breakfast, have class until lunch, have lunch,and then shuttle back to the airport. Unfortunately for me my flightisn't until 7pm so I have a lot of time to write this entry!
See here for a list of each day's blog entries.
First up on this last day of iPhone bootcamp is preferences, whichallow an app to keep user settings in a centralized location. While youcan store user settings yourself using a custom UI, the easiest way isto use the iPhone's built-in Settings app. The Settings app is thecentral location where apps can store user settings. It is also whereall the other common iPhone settings are, which is convenient.
You work with the
NSUserDefaults object to store app settings based on the app's bundle ID, for example
com.acme.MyKillerApp. You'll also want to ensure you provide "factory default" settings using the
registerDefaults method; these settings will be used if the user has not changed anything.
NSUserDefaults is basically a dictionary of key-value pairs, and you obtain the standard user defaults using the
standardUserDefaults method. From that point you can write user settings using methods like
setInteger:forKey, and so on, and you can remove settings using
removeObjectForKey. Similarly, you read settings using methods like
integerForKey, and so forth.
As mentioned earlier, the Settings app is a uniform way of settingpreferences for all apps. Users know where to look for settingpreferences for apps there. In addition, managing the settingsseparately from your application also saves screen real estate, whichis important since the screen isn't all that big. You define thesettings for your application using the Settings Bundle. This is astandard Property List. You use special property keys, such as
PSToggleSwitchSpecifier,to define the settings for your application. The Settings app uses theSettings bundle and property list to create the settings UI for yourapp. For example, if you define settings for a
PSTextFieldSpecifier, and a
PSMultiValueSpecifier, the user will see a toggle switch, a text field, and a slider when she goes to edit your app's settings.
For the lab exercise, we modified our Random Number app (from Day 4during the Web Services section) to have user settings for how therandom numbers are generated — for example minimum number and the rangeof numbers. It is quite easy to add user settings to your app in theiPhone using the Settings app and bundle.
Joe briefly talked about using the Instruments app to do powerfuldebugging, for example to detect memory leaks in your app. He thendemonstrated using the Leaks tool within Instruments to track a memoryleak — which he had previously introduced intentionally — in the TileGame app. Leaks was able to pinpoint the exact line of code where aleaked object (i.e. it was not released) was allocated. This is prettycool.
Joe also showed what leaks look like when they come from C codeversus Objective-C code. C-code allocation leaks (i.e. allocated using
malloc)show up in Instruments as just 'General Block' and from what I gatherare probably not that fun to track down the exact cause. Leaks inObjective-C code are easier, and as I just mentioned, Leaks can showyou the exact line of code where a leaked object was allocated.
By this time, it is getting closer to lunch, and the end of theclass. Sad, I know. Anyway, probably one of the hardest and broadesttopics was saved for last, given that you could spend an entire courseon nothing but network programming. Anyway, on the iPhone a key conceptwhen doing network programming is "reachability." Reachability does notmean that a network resource is definitely available; rather it meansthe resource is "not impossible" to reach. Another important thing onthe iPhone is figuring out what type of connection you have, forexample EDGE or 3G or Wi-Fi. You might choose to do different thingsbased on the type of connection.
With the SystemConfiguration framework you can determine if theoutside world is reachable, again with the caveat the "reachable" means"not impossible to reach." When coding you work with "reachabilityreferences" and "reachability flags" which describe the type ofconnection, for example reachable, connection required, is wide-areanetwork, and so on.
We then learned a bit about Bonjour,which is basically a zero-configuration network service discoverymechanism that works on all platforms, has implementations in manylanguages, and is easy to use. For example, Bonjour makes itridiculously easy to find printers on a network, as opposed to the wayyou have to do it in other operating systems. You can use Bonjour onthe iPhone to discover and resolve Bonjour services. You use
NSNetServiceBrowser to accomplish this, and you specify a delegate to receive notifications when services appear and disappear.
In addition to Bonjour, you can also send and receive data over thenetwork as you would expect. To send and receive data you can eitheruse a C-based Core Foundation interface or use a stream-based interfacewith
CFStream. You register read and write callbacks with the main iPhone run loop, which calls your functions when network events occur.
Network programming is really hard, since there are so many thingsthat can go wrong and you need to deal with many if not all of them.Joe recommended that before jumping right into hardcore networking,developers should become familiar with the concepts and recommended theBeej's guide to network programmingas a good primer. Given that I've really only done simple socketprogramming in higher-level APIs like Java's, and have not really donemuch of it, I'll need to check that out. And so this concludes thisepisode of iPhone bootcamp blog, since it is now time for lunch and,sadly, the course is over!
This week has been a really intensive introduction to iPhoneprogramming. We've gone from a very simple app on Day 1 and covered allkinds of things like form-based UIs and text manipulation; CoreLocation; Core Graphics; view transitions and Core Animation; using theiPhone camera and accelerometer; retrieving web-based data;manipulating Address Book data; setting user preferences, and finallynetworking and debugging using Instruments. We've basically covered anentire book's worth of content and written about 10 iPhone applicationsranging from simple to not-so-simple. If you are interested inprogramming the iPhone, I definitely recommend checking out the iPhone bootcamp at Big Nerd Ranch. Because the OpenGL stuff we did on the iPhone was so cool, I'm thinking my next course to take might be the OpenGL bootcampbut that probably won't happen until next year sometime! With my newknowledge I plan to go home and try my hand at creating a snowflake orsnowstorm application that my daughters can play with. If I actuallyget it working maybe I'll write something about it.
Published at DZone with permission of Scott Leberknight , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.