如何使用本机崩溃报告在Swift 4中实现异常/错误处理?

iAj*_*iAj 10 crash ios swift swift4

我想在我的iOS Swift 4代码中实现本机崩溃报告(不使用推荐的第三方库),并在App崩溃或生成任何异常时通过API调用通知我的后端服务器.我发现App Store/iTunes Connect提供了多次崩溃,但它没有提供崩溃信息,例如UIViewController或者由于它崩溃的错误.有谁可以帮我解决这个问题?提前致谢.

San*_*hah 5

是的,您必须制作自定义崩溃报告管理器,并且可以NSSetUncaughtExceptionHandler用于获取崩溃报告堆栈。

例子

NSSetUncaughtExceptionHandler { exception in

    print(exception)
    print(exception.callStackSymbols)

    // Write callStackSymbols into the file
    // Call your mail function with that file
    // mail()
}

let array = NSArray()
let element = array.object(at: 4)
Run Code Online (Sandbox Code Playgroud)

您可以将该崩溃报告写入文件并邮寄。