一.日志監(jiān)控
I.自己寫API調(diào)用
II.定義宏替換
#define NSLog(frmt,...) LogAPI(frmt,##__VA_ARGS__)
(##__VA_ARGS__可變參數(shù)宏贼陶,##去掉只有一個(gè)參數(shù)時(shí)的,號(hào))
III.使用官方提供的框架
ASL apple system log (已棄用)ios(8.0,10.0)
NSLog:
Logs an error message to the Apple System Log facility.
IIII.fishhook
fishhook可以將我們Mach-O的庫的符號(hào)表重新綁定
hook掉NSLog函數(shù),這邊采用這種方式
static void (* orig_nslog)(NSString *format, ...);
void redirect_nslog(NSString *format, ...) {
// 可以在這里先進(jìn)行自己的處理
// 繼續(xù)執(zhí)行原 NSLog
orig_nslog(@"%@",[format stringByAppendingString:@"被HOOK了"]);
va_list va;
va_start(va, format);
NSLogv(format, va);
va_end(va);
}
- (void)openLogRecord
{
struct rebinding nsLog;
nsLog.name = "NSLog";
nsLog.replacement = redirect_nslog;
nsLog.replaced = (void *)&orig_nslog;
struct rebinding rebinds[1] = {nsLog};
rebind_symbols(rebinds, 1);
}
二.網(wǎng)絡(luò)監(jiān)控
I.NSURLProtocol
@class NSURLProtocol
@abstract NSURLProtocol is an abstract class which provides the
basic structure for performing protocol-specific loading of URL
data. Concrete subclasses handle the specifics associated with one
or more protocols or URL schemes.
抽象類,需要實(shí)現(xiàn)一個(gè)子類
+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
if ([NSURLProtocol propertyForKey:request.URL.absoluteString.md5String inRequest:request]) {
return NO;
}
NSString *url = request.URL.absoluteString;
// 如果url以https開頭躲雅,則進(jìn)行攔截處理近弟,否則不處理
if ([url hasPrefix:@"https"] ||[url hasPrefix:@"http"]) {
return YES;
}
return NO;
}
/**
* 如果需要對請求進(jìn)行重定向避除,添加指定頭部等操作,可以在該方法中進(jìn)行
*/
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
NSMutableURLRequest *req = [request mutableCopy];
[NSURLProtocol setProperty:@(YES) forKey:request.URL.absoluteString.md5String inRequest:req];
return req;
}
II.WKURLSchemeHandler
調(diào)用wkwebview的方法setURLSchemeHandler:forURLScheme:
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
if (@available(iOS 11.0, *)) {
URLSchemeHandler *handler = [URLSchemeHandler new];
[configuration setURLSchemeHandler:handler forURLScheme:@"https"];
[configuration setURLSchemeHandler:handler forURLScheme:@"http"];
} else {
// Fallback on earlier versions
}
聲明http辰斋,https需要攔截
直接攔截會(huì)導(dǎo)致崩潰退子,需要方法交換一下
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method handlesURLScheme = class_getClassMethod(self, @selector(handlesURLScheme:));
Method ssshandlesURLScheme = class_getClassMethod(self, @selector(ssshandlesURLScheme:));
method_exchangeImplementations(handlesURLScheme, sshandlesURLScheme);
});
}
+ (BOOL)ssshandlesURLScheme:(NSString *)urlScheme {
if(DailyRecordManager.shared.type&DailyRecordAll ||
DailyRecordManager.shared.type&DailyRecordRequest
){
//返回NO,否則可能會(huì)中斷言, 'http' is a URL scheme that WKWebView handles natively
if ([urlScheme isEqualToString:@"http"] || [urlScheme isEqualToString:@"https"]) {
return NO;
} else {
return [self ssshandlesURLScheme:urlScheme];
}
}else{
return [self ssshandlesURLScheme:urlScheme];
}
}
//對于NSURLSession發(fā)起的網(wǎng)絡(luò)請求切端,我們發(fā)現(xiàn)通過shared得到的session發(fā)起的網(wǎng)絡(luò)請求都能夠監(jiān)聽到彻坛,但是通過方法sessionWithConfiguration:delegate:delegateQueue:得到的session,我們是不能監(jiān)聽到的踏枣,原因就出在NSURLSessionConfiguration上昌屉,我們進(jìn)到NSURLSessionConfiguration里面看一下,他有一個(gè)屬性 @property(nullable, copy)NSArray<Class>*protocolClasses;復(fù)制代碼
//我們能夠看出茵瀑,這是一個(gè)NSURLProtocol數(shù)組间驮,上面我們提到了,我們監(jiān)控網(wǎng)絡(luò)是通過注冊NSURLProtocol來進(jìn)行網(wǎng)絡(luò)監(jiān)控的马昨,但是通過sessionWithConfiguration:delegate:delegateQueue:得到的session竞帽,他的configuration中已經(jīng)有一個(gè)NSURLProtocol,所以他不會(huì)走我們的protocol來鸿捧,怎么解決這個(gè)問題呢屹篓? 其實(shí)很簡單,我們將NSURLSessionConfiguration的屬性protocolClasses的get方法hook掉匙奴,通過返回我們自己的protocol堆巧,這樣,我們就能夠監(jiān)控到通過sessionWithConfiguration:delegate:delegateQueue:得到的session的網(wǎng)絡(luò)請求
//https://juejin.cn/post/6844903473671077895
因?yàn)榈谌絪dk有可能對wkwebview有封裝
所以直接hook掉wkwebview的configuration
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method setConfiguration = class_getInstanceMethod(self, @selector(configuration));
Method setSSSConfiguration = class_getInstanceMethod(self, @selector(SSSConfiguration));
method_exchangeImplementations(setConfiguration, setSSSConfiguration);
Method con = class_getInstanceMethod(self, @selector(initWithFrame:configuration:));
Method ssscon = class_getInstanceMethod(self, @selector(sssinitWithFrame:configuration:));
method_exchangeImplementations(setConfiguration, setSSSConfiguration);
method_exchangeImplementations(con, ssscon);
});
}
III.查看NSURLSession的請求連接開始時(shí)間結(jié)束時(shí)間重定向次數(shù)等具體信息泼菌,iOS10之后可以使用代理方法谍肤,iOS10之前可以使用libcurl
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFinishCollectingMetrics:(NSURLSessionTaskMetrics *)metrics API_AVAILABLE(macosx(10.12), ios(10.0), watchos(3.0), tvos(10.0));
http://www.reibang.com/p/32935af04593
http://www.reibang.com/p/f146f8fdadbf
IIII.攔截console.log
使用js注入的方式
[configuration.userContentController addScriptMessageHandler:self name:@"logger"];//js打印事件
NSString *printContent = @"console.log = function(message){window.webkit.messageHandlers['logger'].postMessage(message)};";
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:printContent injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[configuration.userContentController addUserScript:userScript];
http://www.reibang.com/p/840e049741bc
https://juejin.cn/post/6844903552385564679#comment
三.崩潰監(jiān)控
NSSetUncaughtExceptionHandler();//設(shè)置崩潰時(shí)走的函數(shù)
NSGetUncaughtExceptionHandler();//獲取崩潰處理函數(shù)
ios崩潰分類:
1.未捕獲異常分類
2.底層信號(hào)崩潰
未捕獲異常處理:
static NSUncaughtExceptionHandler *oldUncaughtExceptionHandler = nil;
void uncaughtExceptionHandler(NSException *exception) {
//記錄日志等操作 舊崩潰函數(shù)處理
// Internal error reporting
}
oldUncaughtExceptionHandler = NSGetUncaughtExceptionHandler();
NSGetUncaughtExceptionHandler(&uncaughtExceptionHandler);//獲取崩潰處理函數(shù)
底層信號(hào)崩潰
void SignalExceptionHandler(int signal){
NSLog(@"signal: %d", signal);
[DailyRecordManager.shared logType:(DailyRecordCrash) message:@(signal)];
}
- (void)crashSingleHandler{
signal(SIGABRT, SignalExceptionHandler);
signal(SIGILL, SignalExceptionHandler);
signal(SIGSEGV, SignalExceptionHandler);
signal(SIGFPE, SignalExceptionHandler);
signal(SIGBUS, SignalExceptionHandler);
}
[https://blog.csdn.net/u012390519/article/details/51682951]
(https://blog.csdn.net/u012390519/article/details/51682951)
https://segmentfault.com/a/1190000039111381