UIApplication/Event recording

From iPhone Development Wiki


@interface UIApplication
-(void)_playbackEvents:(NSArray*)events atPlaybackRate:(float)playbackRate messageWhenDone:(id)target withSelector:(SEL)selector;

Event recorder

Event recording is supported by two private methods, -_addRecorder: and -_removeRecorder:. It uses the delegation pattern. The recorder object will be informed whenever an event arrives. There can be multiple event recorders.

A recorder object must conform to the informal protocol

@protocol UIApplicationEventRecording

the argument event is the plist representation of the GSEvent, converted using the GSEventCreatePlistRepresentation function.

Example implementation

This code will record events into the file events.plist.

@interface Recorder : NSObject {
  NSMutableArray* eventList;
@implementation Recorder
-(id)init {
  if ((self = [super init]))
    eventList = [[NSMutableArray alloc] init];
  return self;
-(void)dealloc {
  [eventList release];
  [super dealloc];
-(void)save {
  [eventList writeToFile:@"events.plist" atomically:YES];
-(void)recordApplicationEvent:(NSDictionary*)event {
  [eventList addObject:event];


// Start recording.
Recorder* recorder = [[Recorder alloc] init];
[[UIApplication sharedApplication] _addRecorder:recorder];


// Stop recording and save the files.
[[UIApplication sharedApplication] _removeRecorder:recorder];
[recorder save];
[recorder release];

Event playback

Event playbacks allows you to send recorded events back the UI elements. It can be used as a macro system, or UI unit testing.

Playbacks are called through the -_playbackEvents:atPlaybackRate:messageWhenDone:withSelector: method.

  1. The events is an NSArray of NSDictionary's (which are GSEvents).
  2. The playback rate is a number governing the speed the events will be sent. The larger the number, the faster it will go. By default you should pass 1.
  3. The target is informed when the playback is done. The selector should have the signature

where the content of info is:

  • UIApplicationEventRecordingDeliveryTimeUserInfoKey: An NSArray which contains the time where the corresponding event happened.

Example implementation

Assume the events are recorded to event.plist:

NSArray* eventList = [NSArray arrayWithContentsOfFile:@"events.plist"];
[[UIApplication sharedApplication] _playbackEvents:eventList atPlaybackRate:1.0f messageWhenDone:nil withSelector:NULL];