在了解GameKit这一块的时候,有个是关于监控的,就是监控错误的输出,然后显示,在项目中我们可以巧妙的使用这个技巧来完成错误日志的输出然后提交的给我们,然后我们进行错误日志的分析,来改善我们的项目
这里是选自cookbook的示例
MonitorGameKitViewController.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
#import <UIKit/UIKit.h> #import "GameKitHelper.h"
#define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f] #define BARBUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] #define STDERR_OUT [NSHomeDirectory() stringByAppendingPathComponent:@"tmp/stderr.txt"]
@interface MonitorGameKitViewController : UIViewController @property (strong, nonatomic) IBOutlet UITextView *textView;
@end
|
MonitorGameKitViewController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
#import "MonitorGameKitViewController.h"
@interface MonitorGameKitViewController ()
@end
@implementation MonitorGameKitViewController
@synthesize textView;
-(void) listenForStderr: (NSTimer *) timer { NSString *contents = [NSString stringWithContentsOfFile:STDERR_OUT encoding:NSUTF8StringEncoding error:NULL]; contents = [contents stringByReplacingOccurrencesOfString:@"\n" withString:@"\n\n"]; if([contents isEqualToString:textView.text]) { return ; } [textView setText:contents]; textView.contentOffset = CGPointMake(0.0f, MAX(textView.contentSize.height - textView.frame.size.height, 0.0f)); }
- (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR; [GameKitHelper sharedInstance].sessionID = @"Peeking at GameKit"; [GameKitHelper assignViewController:self]; freopen([STDERR_OUT fileSystemRepresentation], "w", stderr); [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(listenForStderr:) userInfo:nil repeats:YES]; }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
@end
|
关于GameKitHelper的文件,可以到这里下载GameKitHelper,如果想支持IOS5的话可以到这里GameKitHelper
这里重要的一点是利用了错误日志重定向到文件。