iOS中 加強日誌輸出 開發技術總結

來源:互聯網
上載者:User

iOS中 加強日誌輸出 開發技術總結

對於那些做後端開發的工程師來說,看LOG解Bug應該是理所當然的事,但我接觸到的行動裝置 App開發的工程師裡面,很多人並沒有這個意識,查Bug時總是一遍一遍的試圖重現,試圖調試,特別是對一些不太容易重現的Bug經常焦頭爛額。而且iOS的異常機制比較複雜,Objective-C的語言駕馭也需要一定的功力,做出來的應用有時候挺容易產生崩潰閃退。一遍一遍的用XCode取應用崩潰記錄、解析符號,通常不勝其煩,有時還對著解析出來的調用棧發獃,因為程式當時的內部狀態常常難以看明白,只能去猜測。

對於真機,日誌沒法儲存,不好分析問題。所以有必要將日誌儲存到應用的Docunment目錄下,並設定成共用檔案,這樣才能取出分析。

匯入第三方:AFNetWorkinng

 

- (void)viewDidLoad{    [super viewDidLoad];        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];    [manager GET:@http://mobile.ximalaya.com/m/category_tag_list?category=entertainment&device=iPhone&type=album parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {                NSLog(@%@, responseObject);            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {        NSLog(@%@, [error localizedDescription]);    }];        }

給其寫一個類目:Foundation+Log.m

 

 

#import @implementation NSDictionary (Log)//+ (void)load//{//    NSLog(@11);//}- (NSString *)descriptionWithLocale:(id)locale{    NSMutableString *str = [NSMutableString string];    [str appendString:@{];            [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {        [str appendFormat:@%@ = %@, , key, obj];    }];        [str appendString:@}];        // 刪除最後一個,    NSRange range = [str rangeOfString:@, options:NSBackwardsSearch];    [str deleteCharactersInRange:range];            return str;}@end@implementation NSArray (Log)- (NSString *)descriptionWithLocale:(id)locale{    NSMutableString *str = [NSMutableString string];    [str appendString:@[];            [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {        [str appendFormat:@%@,, obj];    }];        [str appendString:@]];        // 刪除最後一個,    NSRange range = [str rangeOfString:@, options:NSBackwardsSearch];    [str deleteCharactersInRange:range];            return str;}@end

 

 

最終效果:


 

 

小技巧:iOS - 將控制台Log日誌轉為輸出為文字檔

 

1.在AppDelegate.m中建立函數實現以下代碼塊:- (void)redirectNSlogToDocumentFolder{    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentDirectory = [paths objectAtIndex:0];    NSString *fileName = [NSString stringWithFormat:@MrNSLog.txt];// 注意不是NSData!    NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];    // 先刪除已經存在的檔案    NSFileManager *defaultManager = [NSFileManager defaultManager];   [defaultManager removeItemAtPath:logFilePath error:nil];   // 將log輸入到檔案    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], a+, stdout);    freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], a+, stderr);}2.在didFinishLaunchingWithOptions中調用 : [self redirectNSlogToDocumentFolder];
最後配置共用資料夾:

 

在應用程式的Info.plist檔案中添加UIFileSharingEnabled鍵,並將索引值設定為YES。將您希望共用的檔案放在應用程式的 Documents目錄。一旦裝置插入到使用者電腦,iTunes9.1就會在選中裝置的Apps標籤中顯示一個File Sharing地區。此後,使用者就可以向該目錄添加檔案或者將檔案移動到案頭電腦中。如果應用程式支援檔案分享權限設定,當檔案添加到Documents目錄後,應用程式應該能夠識別並做出適當響應。例如說,應用程式可以將新檔案的內容顯示介面上。請不要向使用者展現目錄的檔案清單並詢問他們希望對檔案執行什麼操作。


 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.