UIApplication/Event recording: Difference between revisions

From iPhone Development Wiki
(Created page with '== Interface == <source lang="objc"> @interface UIApplication -(void)_addRecorder:(id)recorder; -(void)_removeRecorder:(id)recorder; -(void)_playbackEvents:(NSArray*)events atPla…')
 
 
Line 13: Line 13:
A recorder object must conform to the informal protocol
A recorder object must conform to the informal protocol
<source lang="objc">
<source lang="objc">
@protocol UIEventRecorder
@protocol UIApplicationEventRecording
-(void)recordApplicationEvent:(NSDictionary*)event;
-(void)recordApplicationEvent:(NSDictionary*)event;
@end
@end

Latest revision as of 09:10, 6 February 2010

Interface

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

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
-(void)recordApplicationEvent:(NSDictionary*)event;
@end

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;
}
@end
@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];
}
@end

...

// 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
-(void)done:(NSDictionary*)info

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];

References