Monday 17 November 2008

Loading HTML from the Main App Bundle

To load a static HTML from the main app bundle (useful to initialize web views in some way) use the following code:


//fakes a URL
NSString * urlStr = @"http://myServer/mypage.html";
// loads the actual HTML
NSString * html = [NSString stringWithContentsOfFile:
[[NSBundle mainBundle] pathForResource:@"phone" ofType:@"html"]];
// loads and displays the page
[thisWebView loadHTMLString:html baseURL:[NSURL URLWithString: urlStr]];

Calling Nonatomic Properties in the Context of the Main Thread

In multithreaded applications, it may sometimes be helpful to call nonatomic properties in the context of the main thread. This is especially relevant if you want to update views (which run in the main thread) from other threads, i.e. a server accepting commands that change the properties of some view of the app.

Luckily, NSObject implements just such a function, which is called performSelectorOnMainThread, that lets you call the selector of an object in the main thread's context:


[myView performSelectorOnMainThread: @selector(update:) withObject: updateItems waitUntilDone: YES];


It even has the option to let the calling thread wait until the call has been completed!

Tuesday 11 November 2008

iPhone Tech Talk at HU-Berlin

I visited the iPhone Tech Talk at HU Berlin yesterday. It was quite interesting, altough I would have liked the speakers to go into more detail about certain subjects (i.e. layer manipulation). For a (somewhat) experienced iPhone developer like me, the event rather much of a promotional, commercial character and was less serious about conveying in-depth technical information.

Anyhow, for those who are interested, here are my rough notes covering some important things that were said:


== Session: Iphone Programming ==

- Don't implement a quit button (pretty obvious).
- Sandbox (limited directory access):
/Documents
/tmp
/Library/Caches
- External Data:
- contacts
- system settings
- voicemail
- photos
- other app data
- Exceptions in Objective-C++
- unified exception model (@try...@catch) works both with c++ and objective-c
- Objective-C memory management:
- retain / release pattern (reference counting)
- first initialisation sets reference counter to 1 (no extra retain needed)
- -autorelease message: sends -release at a later moment (using autoreleasepool)

myString = [[NSString alloc] initWith...] -> retain count 1
myString = [NSString stringWith...] -> utility method, retain count 0 (gets autoreleased at end of method)
- Objective-C setters / getters in Objective-C 2.0
- "properties" control storage semantics
- e.g. @property(readwrite) float speed;
- "readwrite" only one of many keywords, e.g. "atomic, nonatomic, readonly..."
- in implementation file: @synthesize p1,p2,... ; generates getter/setter code implicitly
- Objective-C categories: expand functionality at runtime (similar to protocols)
- @implementation NSString (myExtensions) ...
- Foundation Bundles -> special folders containing the data associated with your app
- special apis provide access to the bundle (NSBundle class)
- bundles are read-only on iPhone (because of code-signing)
- Application Design Patterns
- MVC
- Model: Foundation
- View: UIKit
- Controller: UIKit View Controllers (provide basic functionality)
- Target-Action: (controls can be re-used for different actions)
Control -> Target -> Object
-> Action -> Selector
- Notifications: Notification -> Notification Center --> Observers
- i.e. finishedPlaying Notification when movie terminated
- Delegation: Application Life Cycle:
- UIApplication instantiation, Main NIB loaded, Main Window created
- Application Delegate: this is the user's code that gets called after UIapplication has started (your app)
- benefit: no subclassing necessary, as delegate extends functionality
- app delegate hooked up in interface builder
- other UI classes relying on delegates: pickers, table views, etc...


== Session: UIKit App Development ==
- View controllers:
- manage currenct screen content
- handle user interaction, key SDK concept
- two major types:
- yours -> present per-screen content
- from apple -> manage multiple screens (i.e. switched by tab-bar controller)
- rarely subclassed (customization throught delegation)
- Key Methods: loadView, viewDidLoad, viewWillAppear, viewDidAppear
- write in Xcode: view controllers
- layout in interf. builder: views

- Apple view controllers:
- navigation controller: provides navigation bar on top of window
- stack-based naviagation: manages stack of view controllers, easy to customize...

- Tab bar controllers:
- "flat" navigation, no hierarchy
- provides "tab bar" on the bottom of the device
- different views on the same data or show different parts of the app (i.e. clock app)
- only five icons shown at a time, but more can be added on a separate screen (accessed via the "more" button)
- tab icons: developer provided only monochrome image with apha -> appropriate colors are provided by framework

- Other special views:
- tool bars:
- icons don't retain a title
- icons don't remain highlighted
- used for one-shot actions, often to present follow-up modal or alert views (by default, they slide up from the bottom, to indicate that they are temporary, vs. "navigation" affordance by left-right movement)
- table views:
- tableview delegate + data source
- table view cells are re-usable: already instantiated cells get re-used once the scroll by. They shouldn't be deleted and re-instantiated.
- Autorotation + Autoresizing maks can be specified with interface builder
- Size of the screen can change when incoming call arrives, or when keyboard appears -> well-behaved apps handle these conditions
- memory warning: -didReceiveMemoryWarning
- release non-critical objects ASAP
- initialize application lazily

- Animations:

- implement UIView change properties by wrapping with UIAnimation! --> very nice feature. setup animation ... commit animation.
- animation finished callback called when animation is done

- Text Display:
- UILabel
- UIWebView: can display HTML, PDF, Word files
- NSStringDrawing

Touch Screen:

- UITouch: information per touch

- UIEvent: collects all touch events
- allTouches, touchesForWindow, etc..
- myview.multipleTouchEnabled = YES;
- "Touches" sample code on developer connection

- Accelerometer:
- x axis is inverted (runs from right to left)
- interface orientation
- device orientation
- analog user input

- raw accelerometer data:
UIAccelerometer accel [uiaccelerometer sharedAccelerometer];
accel.updateInterval = 1 / f; (f = 1 - 100hz)
accel.delegate = self; -> delivery begins immediately

- filtering data:
- low-pass
- isolates current acceleration, discrete inputs --> bubbleLevel
- high-pass filter: isolates instantaneous movement
- used to find shakes and oscillations
-> accelerometerGraph

Session: Optimizing & Profiling

- Tools: Instruments (memory footprint, leak detection, cpu usage), Shark (profiling of method calls -> doesn't work that well yet)
- static memory: -mthumb compiler flag reduces to 16-bit ARM instruction subset (35% savings in size)
- PNGs are optimized
- plists are converte to compact binary format by Xcode -> use plists for structured data files
- Dynamic memory:
- autoreleasing is is expensive: try to do it manually (avoid conveniece Methods)
- use local autorelease pools to prevent object accumulation
- drain local autorelease pools periodically
- declare properties as nonatomic -> access is 10 times faster, but not thread safe
- avoid leaks, use Instruments
- Drawing Envirnment:
- use UIKit view animations when possible (very optimized)
- mark views as opaque to improve performa