小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

iOS已發(fā)布應用中對異常信息捕獲和處理

 quasiceo 2015-03-22


iOS開發(fā)中我們會遇到程序拋出異常退出的情況,如果是在調(diào)試的過程中,異常的信息是一目了然,但是如果是在已經(jīng)發(fā)布的程序中,獲取異常的信息有時候是比較困難的。

iOS提供了異常發(fā)生的處理API,我們在程序啟動的時候可以添加這樣的Handler,這樣的程序發(fā)生異常的時候就可以對這一部分的信息進行必要的處理,適時的反饋給開發(fā)者。

不足的地方是,并不是所有的程序崩潰都是由于發(fā)生可以捕捉的異常的,有些時候是因為內(nèi)存等一些其他的錯誤導致程序的崩潰,這樣的信息是不在這里體現(xiàn)的

我做了一個簡單的類,進行很基本的操作,可以添加和獲取Handler,捕獲到異常后將信息寫入到app的Documens下的Exception.txt中。

其實還有很多的處理的辦法。

l  比如可以在程序下一次起來的時候讀取這個異常文件發(fā)生到服務端。

l  或者直接就是在處理代碼中用openurl的方式(mailto:)調(diào)用發(fā)送郵件的方式,將異常信息直接變成郵件發(fā)送到指定地址。

以下是完整的代碼實現(xiàn)。

使用場景示例:

#pragma mark -

#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  

  

    // Override point for customization after application launch.

   

    [window makeKeyAndVisible];

     [NdUncaughtExceptionHandler setDefaultHandler];

     NSArray *array = [NSArray arrayWithObject:@"there is only one objective in this arary,call index one, app will crash and throw an exception!"];

     NSLog(@"%@", [array objectAtIndex:1]);

   

     return YES;

}

基本接口展示:

#import <Foundation/Foundation.h>

@interface NdUncaughtExceptionHandler : NSObject {

}

+ (void)setDefaultHandler;

+ (NSUncaughtExceptionHandler*)getHandler;

@end

//還可以選擇設置自定義的handler,讓用戶取選擇

接口實現(xiàn)展示

#import "NdUncaughtExceptionHandler.h"

NSString *applicationDocumentsDirectory() {

    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

}

void UncaughtExceptionHandler(NSException *exception) {

     NSArray *arr = [exception callStackSymbols];

     NSString *reason = [exception reason];

     NSString *name = [exception name];

     NSString *url = [NSString stringWithFormat:@"=============異常崩潰報告=============\nname:\n%@\nreason:\n%@\ncallStackSymbols:\n%@",

                   name,reason,[arr componentsJoinedByString:@"\n"]];

     NSString *path = [applicationDocumentsDirectory() stringByAppendingPathComponent:@"Exception.txt"];

     [url writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];

     //除了可以選擇寫到應用下的某個文件,通過后續(xù)處理將信息發(fā)送到服務器等

     //還可以選擇調(diào)用發(fā)送郵件的的程序,發(fā)送信息到指定的郵件地址

     //或者調(diào)用某個處理程序來處理這個信息

}

@implementation NdUncaughtExceptionHandler

-(NSString *)applicationDocumentsDirectory {

    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

}

+ (void)setDefaultHandler

{

     NSSetUncaughtExceptionHandler (&UncaughtExceptionHandler);

}

+ (NSUncaughtExceptionHandler*)getHandler

{

     return NSGetUncaughtExceptionHandler();

}

@end

異常崩潰報告:

=============異常崩潰報告=============

name:

NSRangeException

reason:

*** -[NSArray objectAtIndex:]: index 1 beyond bounds [0 .. 0]

callStackSymbols:

0   CoreFoundation                      0x02393919 __exceptionPreprocess + 185

1   libobjc.A.dylib                     0x024e15de objc_exception_throw + 47

2   CoreFoundation                      0x0238958c -[__NSArrayI objectAtIndex:] + 236

3   UncaughtE                           0x000022e8 -[UncaughtEAppDelegate application:didFinishLaunchingWithOptions:] + 157

4   UIKit                               0x002b8543 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163

5   UIKit                               0x002ba9a1 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 346

6   UIKit                               0x002c4452 -[UIApplication handleEvent:withNewEvent:] + 1958

7   UIKit                               0x002bd074 -[UIApplication sendEvent:] + 71

8   UIKit                               0x002c1ac4 _UIApplicationHandleEvent + 7495

9   GraphicsServices                    0x02bf9afa PurpleEventCallback + 1578

10  CoreFoundation                      0x02374dc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52

11  CoreFoundation                      0x022d5737 __CFRunLoopDoSource1 + 215

12  CoreFoundation                      0x022d29c3 __CFRunLoopRun + 979

13  CoreFoundation                      0x022d2280 CFRunLoopRunSpecific + 208

14  CoreFoundation                      0x022d21a1 CFRunLoopRunInMode + 97

15  UIKit                               0x002ba226 -[UIApplication _run] + 625

16  UIKit                               0x002c5b58 UIApplicationMain + 1160

17  UncaughtE                           0x00002228 main + 102

18  UncaughtE                           0x000021b9 start + 53


    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多