Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Exception/Error Handling in Swift 4 with native Crash Reporting?

I want to implement native crash reporting (without using 3rd party library recommended) in my iOS Swift 4 code and inform to my back end server with API call when the App Crashes or generating any exceptions. I found that App Store / iTunes Connect provides number of Crashes but it does not provide Crash information like from which UIViewController or from due to which error it crashed. Could anyone help me out in this? Thanks in advance.

like image 451
iAj Avatar asked Aug 03 '18 12:08

iAj


People also ask

What does Exc_breakpoint Sigtrap mean?

EXC_BREAKPOINT (SIGTRAP) and EXC_BAD_INSTRUCTION (SIGILL)The breakpoint exception type indicates a trace trap interrupted the process. A trace trap gives an attached debugger the chance to interrupt the process at a specific point in its execution. On ARM processors, this appears as EXC_BREAKPOINT (SIGTRAP).

How do I add a try catch in Swift?

try – You must use this keyword in front of the method that throws. Think of it like this: “You're trying to execute the method. catch – If the throwing method fails and raises an error, the execution will fall into this catch block. This is where you'll write code display a graceful error message to the user.


1 Answers

Yes for that you have to make your custom crash report manager and you can use NSSetUncaughtExceptionHandler for getting the crash report stack.

Example

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)

You can write that Crash Report into the file and mail it.

like image 59
Sanjay Shah Avatar answered Sep 28 '22 13:09

Sanjay Shah