IOS exception handling, sending bug information to developer email

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.