用网上的一段代码
#pragma mark -#pragma mark Application lifecycle- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point forcustomization after application launch. [windowmakeKeyAndVisible]; [NdUncaughtExceptionHandlersetDefaultHandler]; NSArray *array = [NSArray arrayWithObject:@"thereis only one objective in this arary,call index one, app will crashand throw an exception!"]; NSLog(@"%@", [array objectAtIndex:1]); returnYES;}基本接口展示:#import<Foundation/Foundation.h>@interface NdUncaughtExceptionHandler : NSObject {}+ (void)setDefaultHandler;+ (NSUncaughtExceptionHandler*)getHandler;@end//还可以选择设置自定义的handler,让用户取选择接口实现展示#import "NdUncaughtExceptionHandler.h"NSString *applicationDocumentsDirectory() { return[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];}void UncaughtExceptionHandler(NSException *exception) { NSArray *arr = [exceptioncallStackSymbols]; NSString *reason = [exception reason]; NSString *name = [exception name]; NSString *url = [NSStringstringWithFormat:@"=============异常崩溃报告=============nname:n%@nreason:n%@ncallStackSymbols:n%@", name,reason,[arrcomponentsJoinedByString:@"n"]]; NSString *path = [applicationDocumentsDirectory()stringByAppendingPathComponent:@"Exception.txt"]; [urlwriteToFile:path atomically:YES encoding:NSUTF8StringEncodingerror:nil]; //除了可以选择写到应用下的某个文件,通过后续处理将信息发送到服务器等 //还可以选择调用发送邮件的的程序,发送信息到指定的邮件地址 //或者调用某个处理程序来处理这个信息}@implementation NdUncaughtExceptionHandler-(NSString *)applicationDocumentsDirectory { return[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];}+ (void)setDefaultHandler{ NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);}+ (NSUncaughtExceptionHandler*)getHandler{ returnNSGetUncaughtExceptionHandler();}@end
第二种方法使用PLCrashReporter框架借鉴google的
PLCrashReporter provide an in-process CrashReporter frameworkfor the iPhone and Mac OS X. This framework is very helpful ingetting the crash report for your app from beta tester or realusers.
I’m not sure how most of the developer ask for the crashreport from their users, but one way is to ask them to connecttheir IOS device to iTune, get the crash report and send themmanually, but this is just too troublesome.
With PLCrashReporter, you can capture the logs and output acrash report and your users can choose to send the report to thedeveloper directly within the app.
To start using PLCrashReporter,
First download thesource release. Extract the package and run theXcode project, we need to build the crashreport framework using thesource before we can use it in our project.There are a lot of Targets, for this example lets use only thesimulator Target. Navigate to the CrashReporter-iPhoneSimulatorTarget. Under the Build Settings, set “Perform Single-ObjectPrelink” to “No”, “Mach-O Type” to “Static Library”.Select your simulator as iphone simulator and then clickbuild.A static library should be built now, navigate to your builddirectory for the PLCrashReporter, eg./Users/username/Library/Developer/Xcode/DerivedData/CrashReporter-(randomcharacters)/Build/Products/Debug-iphonesimulator and copy thelibCrashReporter-iphonesimulator.a to your project that use thePLCrashReporter.Now, download thePLCrashReporterbinary releaseand extract the framework to a local directory.
Open your new project, and add the CrashReporter framework toyour project. To add a new framework, click on your project name inXCode, click on your target, under ‘Build Phases’ ->‘Link Binary with libraries’, click the ‘+’, and ‘add other’. Lookfor the ‘libCrashReporter-iphonesimulator.a’ you’ve built and theCrashReporter.framework you’ve downloaded, add them into theproject.Now you are ready to use the CrashReporter framework. There’sa demo project in the source release you’ve downloaded, you can runthe project and see how it works.
For XCode 4, the CrashReporter might have problem in creatingthe report. If it doesn’t work, you can try to switch off the GDBdebugger. To do this, in XCode, go to Product ->Edit Scheme -> Select the debugger as ‘None’.
If it works, you should be able to see a report‘live_report.plcrash’ under your ‘applicationdirectory/library/caches/com.plausiblelabs.crashreporter.data/com.yourcompany.CrashReporterDemo/’after you trigger a crash in the demo project.
With PLCrashReporter, you can provide a simpler way for yourBeta tester or real users to send the crash report back toyou.
因為app常常crash 所以去google做了一下功課,找到了這篇:SendCrash Reports from withinApp,發現foursquare也是用 plcrashreporter這個framework,所以就來研究一下。
1.首先到 http://code.google.com/p/plcrashreporter/下載最新framework版本plcrashreporter1.0.1,然後import到自己的project裡面。
2.參考 Example iPhone Usage 的範例,本身是用xcode 4.1去compile& run,確定可以使用,但是如果發現
PLCrashReporter *crashReporter = [PLCrashReporter sharedReporter];
會造成crash的話,請確定一下是否是下載最新的plcrashreporter。
3.當app在執行期間發生了crash,下次再開啓app的時候,我們可以利用以下程式碼,將上次crash的crashreport寄給自己。
PLCrashReporter *crashReporter =[PLCrashReporter sharedReporter];
NSData *crashData;NSError *error;crashData = [crashReporterloadPendingCrashReportDataAndReturnError:&error];MFMailComposeViewController *picker =[[MFMailComposeViewController alloc] init];[picker addAttachmentData:crashDatamimeType:@"application/octet-stream"fileName:@"crashfile.crash"];[self presentModalViewController:pickeranimated:YES];[picker release];
4.拿到crashreport之後,打開plcrashreport 1.0.1裡面的toolfolder,利用裡面的plcrashutil解碼(termail裡面打入./plcrashutilconvert --format=iphoneexample_report.plcrash),可以得到完整的crashreport。
5.配合dsym file 做symbolicate即可以看到app crash在那個檔案的那一行。可參考http://blog.sina.com.cn/s/blog_67b7cb7b01014h2x.html6.如果發現沒辦法看到crash出現在那一行的話,我的解決辦法是將利用plcrashutil解碼後的crashreport裡面version 103改成 104,然後將所有的 ???(???) 取代成armv7,再做synbpilcate即可。
还有第三种采用 CrashKit 这个类库三种方法比较起来还是这个简单点,呵呵 ..去googlecode下载最新版本1.拷贝 CrashController , CrashLogger四个文件到项目中2.在AppDelegate中实现 CrashSaveDelegate 委托3.下面就可以在AppDelegate.m文件中实现代码CrashController *crash = [CrashControllersharedInstance];crash.delegate = self;// 类库提供两种方式 ,邮件和服务器//邮件[crashsendCrashReportsToEmail:@"crash@smartfulstudios.com" withViewController:appDelegate.navigationController];//服务器[crashsendCrashReportsToBugzScoutURL:@"https://smartfulstudios.fogbugz.com/api.asp" withUser:@".com" password:@"" forProject:@"Inbox" withArea:@"Misc"];完成这些,别忘记实现onCrash回调方法- (void)onCrash{ UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Crash" message:@"The App has crashed and will attemptto send a crash report" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];}
OK了.很简单!不过 crashkit 目前只支持 ipad3.0