Detect app crashed during load / last time it was run?
Make 2 functions in your AppDelegate.m
file:
void HandleException(NSException *exception) {
NSLog(@"App crashing with exception: %@", exception);
//Save somewhere that your app has crashed.
}
void HandleSignal(int signal) {
NSLog(@"We received a signal: %d", signal);
//Save somewhere that your app has crashed.
}
Then in your -(BOOL)application:didFinishLaunchingWithOptions
before anything else put:
NSSetUncaughtExceptionHandler(&HandleException);
struct sigaction signalAction;
memset(&signalAction, 0, sizeof(signalAction));
signalAction.sa_handler = &HandleSignal;
sigaction(SIGABRT, &signalAction, NULL);
sigaction(SIGILL, &signalAction, NULL);
sigaction(SIGBUS, &signalAction, NULL);
Swift: Natively Detect if App has Crashed
Thanks to a little help from @RyanCollins, I was able to solve the problem myself. The function applicationWillTerminate
in the App Delegate only runs when the app closes properly. The code to natively detecting an app crash looks like this.
Globally Defined Variables
let crashedNotificationKey = "com.stackoverflow.crashNotificationKey"
var crashedLastTime = true
App Delegate
func applicationWillTerminate(application: UIApplication) {
crashedLastTime = false
prefs.setBool(crashedLastTime, forKey: "crash")
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
crashedLastTime = prefs.boolForKey("crash")
if crashedLastTime == true {
crashedLastTime = false
prefs.setBool(crashedLastTime, forKey: "crash")
NSNotificationCenter.defaultCenter().postNotificationName(crashedNotificationKey, object: self)
} else {
crashedLastTime = true
prefs.setBool(crashedLastTime, forKey: "crash")
}
return true
}
Root View Controller
override func awakeFromNib() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "crashedAlert", name: crashedNotificationKey, object: nil)
}
func crashedAlert() {
let alert = UIAlertController(title: "The app has crashed!", message: "Sorry about that! I am just a 17 year old highschooler making my first game!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "It's cool bro.", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
}
Is there any way to detect an application crash and then launch it again in Android?
When the system force stops your application, the entire process is simply killed. There is no callback made to inform you that this has happened but there is workaround and you can try:
https://medium.com/@ssaurel/how-to-auto-restart-an-android-application-after-a-crash-or-a-force-close-error-1a361677c0ce
how do we know if ios app is crashed in last launch?
One way would be to create an empty file, say, lock_file.tmp
, in the documents folder from the applicationDidBecomeActive:
app delegate method, and delete that file inside the applicationWillResignActive:
method.
Normally, the app should see no lock_file.tmp
file in applicationDidBecomeActive:
in the documents folder, because applicationWillResignActive:
deletes it. If the app crashes, however, the file would remain there, so your program would be able to detect that the previous run has been interrupted due to a crash.
How to determine, upon launch, whether the app crashed the last time it quit?
Write to a file every time you exit. Read that file every time you open the program. Write a specific integer to the file every time the program is exited that is read every time the program is started.
For example if a 0 is written to a file that may signify a non-crash. Anything else signifies a crash.
How to use swift to detect app crash?
I don't think there is currently a way to do this. When your app crashes it ceases to function and all processes stop. Usually any crash and diagnostic data is sent via the user settings panel.
Edit: There is an helpful post here: https://stackoverflow.com/a/8242215/4891259
Hope that helps.
How to use swift to detect app crash?
I don't think there is currently a way to do this. When your app crashes it ceases to function and all processes stop. Usually any crash and diagnostic data is sent via the user settings panel.
Edit: There is an helpful post here: https://stackoverflow.com/a/8242215/4891259
Hope that helps.
Related Topics
Over the Air (Ota) iOS IPA File Distribution for Public
Using Tesseract to Recognize License Plates
How to Use Charles Proxy on the Xcode 6 (iOS 8) Simulator
Why Does Uiviewcontroller Extend Under Uinavigationbar, While Uitableviewcontroller Doesn'T
Set Background Gradient on Button in Swift
Apply Gradient Color to Arc Created with Uibezierpath
Shake Animation for Uitextfield/Uiview in Swift
Detected a Case Where Constraints Ambiguously Suggest a Height of Zero
Is There a Public API for Card View UI That Can Be Seen Across iOS 10
iOS App Submission and Beta Review Process
Launch a Local Notification at a Specific Time in iOS
How to Ask User for Camera Access After They Have Already Denied It on iOS
Macos on Vmware Doesn't Recognize iOS Device
Ios7 Uipickerview How to Hide the Selection Indicator
How to Know If an Emoji Is Supported in iOS
Adding Multiple Arrays to Form One Final Array. Debug Swift Xcode
How to Import Private Framework Headers in a Swift Framework