Mob*_*its 65
感谢所有投入的人...我把你的解决方案变成了一个可以解决我的问题的解决方案..这就是我所做的......当然我没有编译代码,它是一个半烤代码..但我会当我在我的代码中实现它时,很快就会把它弄好.
NSLog进入文件如何将NSLog转换成 LOG2FILE 文件
#if TARGET_IPHONE_SIMULATOR == 0
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
#endif
Run Code Online (Sandbox Code Playgroud)
抓住崩溃并将它们记录到文件中
首先,创建一个处理错误并将其输出到控制台的函数(以及您想要用它做的任何其他事情):
void uncaughtExceptionHandler(NSException *exception) {
NSLog(@"CRASH: %@", exception);
NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
// Internal error reporting
}
Run Code Online (Sandbox Code Playgroud)
接下来,将异常处理程序添加到您的应用程序委托:
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:
(NSDictionary*)launchOptions
{
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler); // Normal launch stuff
}
Run Code Online (Sandbox Code Playgroud)
在info.plist中设置一个名为Crashed的变量,然后以这种方式读/写它
- (void)readPlist
{
NSString *localizedPath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:localizedPath];
NSString *crashed;
crashed = [plistDict objectForKey:@"Crashed"];
}
- (void)writeToPlist
{
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
[plistDict setValue:@"YES" forKey:@"Crashed"];
[plistDict writeToFile:filePath atomically: YES];
}
Run Code Online (Sandbox Code Playgroud)
应用程序启动后,请阅读info.plist并提示用户提交崩溃日志.
{
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;[mailComposer setSubject:@"Crash Log"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
[mailComposer setToRecipients:toRecipients];
// Attach the Crash Log..
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
NSData *myData = [NSData dataWithContentsOfFile:logPath];
[mailComposer addAttachmentData:myData mimeType:@"Text/XML" fileName:@"Console.log"];
// Fill out the email body text
NSString *emailBody = @"Crash Log";
[mailComposer setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:mailComposer animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
Ker*_*rni 49
要记录您自己的数据,请使用Cocoalumberjack.它比NSLog快得多,可以动态打开/关闭.它还提供将数据保存到文件中的选项.NSLog将减慢您的应用程序并填充控制台日志.另外,您通常不希望记录太多.发生崩溃时,您无法安全地进行日志记录.所以,一旦你弄清楚问题区域在哪里,在那里添加更多的日志并尝试重现它,例如通过使用像KIF这样的自动化测试框架.
对于捕获崩溃报告,除了基于开源框架PLCrashReporter的解决方案之外别无其他,它可以安全地捕获崩溃,当您的应用程序已经在应用程序商店中时!不推荐像其他人建议的异常捕获,请查看此文章以了解原因!
iTunes Connect也可以让你查看一些崩溃报告,但是看一些崩溃报告需要2周时间,但到目前为止并不是全部,例如Camera +开发人员所指出的.所以你最好使用自己的解决方案.
PLCrashReporter将向您发送标准的Apple格式崩溃报告,准备进行符号化,以便您知道代码中发生崩溃的位置,包括行号.
一些基于PLCrashReporter的解决方案是:
建议的解决方案允许在下次启动时自动发送数据,或者询问用户是否同意发送.
这是一种可以在崩溃发生时立即捕获崩溃的解决方案,它将提供比崩溃日志更多的人类可读代码信息。它将缺少一些崩溃日志,但是正如Till所说,您仍然应该能够访问这些日志。
关于Xcode 4.2的另一个SO问题总是在崩溃时返回main。那里的答案使用此方法,您可以扩展它以跟踪崩溃。
在AppDelegate中实现自己的异常处理程序
// on load
NSSetUncaughtExceptionHandler(&uncaughtExceptionHandler);
void uncaughtExceptionHandler(NSException *exception) {
NSLog(@"CRASH: %@", exception);
NSLog(@"Stack Trace: %@", [exception callStackSymbols]);
// Internal error reporting
}
Run Code Online (Sandbox Code Playgroud)
更新我做了一些回溯,Zane Claes提供了此解决方案,以解决Xcode 4.2调试不表示堆栈调用的问题
他在第二条评论中提供了一个通用的解决方案。“我发现将崩溃日志写入文件并提示用户在下次启动时提交它很有用(仅在发布模式下,以免妨碍调试)。这使我得到了很多错误报告。 ..并且用户知道他们的问题已得到解决”“我知道不是每个人都想问这个用户,但是那里有超级用户很乐意为您提供帮助。
您当然可以包括一个“永不显示此提示”按钮,以使人们不会因报告机制而感到沮丧。
或者,您可以通过信息与服务器联系(不确定它是否会因为崩溃而工作,但请保存它,并偶尔尝试使用详细信息将其发布到服务器上)
对于Swift下的日志记录和分析,您可以使用SwiftyBeaver,它是一个功能齐全的日志记录平台,包括开源Swift 2和Objective-C Framework,加密云存储和Mac App.
框架(支持):https://github.com/SwiftyBeaver/SwiftyBeaver
免责声明:我是创始人.
归档时间: |
|
查看次数: |
50165 次 |
最近记录: |