|
| 1 | +// |
| 2 | +// QCloudLogManager.m |
| 3 | +// QCloudCOSXML |
| 4 | +// |
| 5 | +// Created by erichmzhang(张恒铭) on 2018/10/8. |
| 6 | +// |
| 7 | +#if TARGET_OS_IPHONE |
| 8 | +#import "QCloudLogManager.h" |
| 9 | +#import <QCloudCore/QCloudCore.h> |
| 10 | +#import "QCloudLogTableViewController.h" |
| 11 | +@implementation QCloudLogManager |
| 12 | ++ (instancetype) sharedInstance { |
| 13 | + static QCloudLogManager *instance; |
| 14 | + static dispatch_once_t onceToken; |
| 15 | + dispatch_once(&onceToken, ^{ |
| 16 | + instance = [[QCloudLogManager alloc] init]; |
| 17 | + }); |
| 18 | + return instance; |
| 19 | +} |
| 20 | + |
| 21 | +- (instancetype) init { |
| 22 | + self = [super init]; |
| 23 | + |
| 24 | + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onHandleAppBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil]; |
| 25 | + return self; |
| 26 | +} |
| 27 | + |
| 28 | +- (void)dealloc { |
| 29 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 30 | +} |
| 31 | +- (void) onHandleAppBecomeActive :(NSNotification *)notification { |
| 32 | + if ([self shouldShowLogs]) { |
| 33 | + [UIPasteboard generalPasteboard].string = @""; |
| 34 | + [self showLogs]; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +- (BOOL) shouldShowLogs { |
| 40 | + NSString *currentPasteBoardContent = [UIPasteboard generalPasteboard].string; |
| 41 | + if ([currentPasteBoardContent isEqualToString:@"##qcloud-cos-log-ispct##"]) { |
| 42 | + return YES; |
| 43 | + } |
| 44 | + return NO; |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +- (NSArray *)currentLogs { |
| 49 | + NSString *directoryPath = [QCloudLogger sharedLogger].logDirctoryPath; |
| 50 | + NSArray *content = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil]; |
| 51 | + return content; |
| 52 | +} |
| 53 | + |
| 54 | +- (NSString *)readLog:(NSString *)path { |
| 55 | + NSData *content = [[NSFileManager defaultManager] contentsAtPath:path]; |
| 56 | + return [[NSString alloc] initWithData:content encoding:NSUTF8StringEncoding]; |
| 57 | +} |
| 58 | + |
| 59 | +- (void) showLogs { |
| 60 | + UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定显示log" preferredStyle:UIAlertControllerStyleAlert]; |
| 61 | + UIAlertAction* actionEnsure = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action) { |
| 62 | + [self onHandleBeginShowlogs]; |
| 63 | + }]; |
| 64 | + UIAlertAction* actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil]; |
| 65 | + [alertController addAction:actionEnsure]; |
| 66 | + [alertController addAction:actionCancel]; |
| 67 | + UIViewController *currentViewController = [self currentViewController]; |
| 68 | + [currentViewController presentViewController:alertController animated:YES completion:nil]; |
| 69 | + |
| 70 | +} |
| 71 | +-(void)onHandleBeginShowlogs{ |
| 72 | + NSArray *currentLogPath = [self currentLogs]; |
| 73 | + UIViewController *currentViewController = [self currentViewController]; |
| 74 | + QCloudLogTableViewController *tableViewController = [[QCloudLogTableViewController alloc] initWithLog:currentLogPath]; |
| 75 | + if ([currentViewController isKindOfClass:UINavigationController.class]) { |
| 76 | + [((UINavigationController *)currentViewController) pushViewController:tableViewController animated:YES]; |
| 77 | + } else { |
| 78 | + [currentViewController presentViewController:tableViewController animated:YES completion:nil]; |
| 79 | + } |
| 80 | +} |
| 81 | +- (UIViewController *)currentViewController { |
| 82 | + UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow; |
| 83 | + UIViewController *vc = keyWindow.rootViewController; |
| 84 | + while (vc.presentedViewController) { |
| 85 | + vc = vc.presentedViewController; |
| 86 | + |
| 87 | + if ([vc isKindOfClass:[UINavigationController class]]) { |
| 88 | + vc = [(UINavigationController *)vc visibleViewController]; |
| 89 | + } else if ([vc isKindOfClass:[UITabBarController class]]) { |
| 90 | + vc = [(UITabBarController *)vc selectedViewController]; |
| 91 | + } |
| 92 | + } |
| 93 | + return vc; |
| 94 | +} |
| 95 | + |
| 96 | +@end |
| 97 | +#endif |
0 commit comments