1.iOS
In the development of the program we will inevitably encounter the problem of collapse. And then, in the user experience, how do we prevent crashes from happening and send our crash reasons to the developer to handle it.
Take a look at an example
NSString *str = @ "523"; arr = @[@ "Sdad", @ "Dwada", @ "Ffwwra"]; [Str substringfromindex:111];
The program will definitely crash when it's written like this. If we want it not to collapse, then what should be done.
@try { NSString *str = @ "523"; arr = @[@ "Sdad", @ "Dwada", @ "Ffwwra"]; [Str substringfromindex:111]; } @catch (NSException *exception) { NSLog (@ "%s%@", __function__, exception); } @finally { //How to treat this exception handling NSLog (@ "Trytwo-I will definitely execute"); }
We can use the @try{} @catch () {} @finally to handle it so that the program does not crash when it runs. Instead, run to @finnally to handle the anomaly and print out the cause of the crash inside the @catch.
How do I send a message to a developer?
Sending a message in iOS is easy.
Encapsulation to the following methods
crashbug.h// Try handles exception//// Created by Huang Quanhao on 15-1-24.// Copyright (c) 2015 Huang Quanhao. All rights reserved.//#import <Foundation/Foundation.h> @interface crashbug:nsobject+ (void) Sendbug: (NSString * ) Bug Interface: (NSException *) Interfaceinfo; @end
crashbug.m// Try handles exception//// Created by Huang Quanhao on 15-1-24.// Copyright (c) 2015 Huang Quanhao. All rights reserved.//#import "CRASHBUG.h" #import "AppDelegate.h" @implementation crashbug+ (void) Sendbug: (NSString *) Bug Interface: (NSException *) interfaceinfo{ nstimeinterval time = [[NSDate date] timeIntervalSince1970]; A long long int date = (long long int) time; NSDate *DD = [NSDate datewithtimeintervalsince1970:date]; NSString *crashloginfo = [NSString stringwithformat:@ "exception type:%@ \ n crash reason:%@ \ Call stack time:%@", I Nterfaceinfo, Bug, DD]; NSString *urlstr = [NSString stringwithformat:@ "Mailto://[email protected]?subject=bug Report&body=Thank Your cooperation! "" Error details:%@ ", Crashloginfo]; Nsurl *url = [Nsurl urlwithstring:[urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; [[UIApplication sharedapplication] openurl:url];} @end
And then use this.
viewcontroller.m// Try handles exception//// Created by Huang Quanhao on 15-1-24.// Copyright (c) 2015 Huang Quanhao. All rights reserved.//#import "ViewController.h" #import "CRASHBUG.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload { [super viewdidload]; Additional setup after loading the view, typically from a nib. @try { NSString *str = @ "523"; [Str substringfromindex:111]; } @catch (NSException *exception) { NSLog (@ "%s%@", __function__, exception); /** * send abnormal crash information to developer Mail * / [crashbug sendbug:@ "type conversion of string" interface:exception]; } @finally { //How to treat this exception handling NSLog (@ "Trytwo-I will definitely execute");} } -(void) didreceivememorywarning { [super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
This will send the email to the developer's email when it crashes.
IOS exception handling, sending bug information to developer email