The beginning of the column: in the process of development, as the sender we often encounter crashes, flashback situation, and crashes, there are many kinds of flash back. If it is in the development of the test process, we can timely analysis and repair, but for our KPI will be a definite impact, to lead the impression of poor. And it still takes a lot of time to locate crash. If the crash, the flash back on the line, then the product of our company more impact, the impact on us is also a big no, the light is scolded, heavy deduction of wages. And the online crash difficult to track positioning, I believe we all have deep experience. If there is a mechanism, the most common crash will be able to shield the screen, will not crash, and can send crash information to log log or the server backstage to facilitate our analysis. It's not as if the core code of this mechanism is not executed when the app is on the line, only when the crash occurs, and the performance impact is almost zero. Is there such a wonderful mechanism? Of course there is. Next in this column, I will explore with you and strive to make this mechanism perfect. and heart to integrate nuclear code into the JKCrashProtect , welcome to participate in the discussion oh.
Unrecognized selector sent to instance
?? Because of the many causes of collapse, this article only analyzes the unrecognized selector sent to instance . This collapse of the main is not found in the implementation of the method of production, in the development process of the probability is still relatively large, especially in the team size, personnel level, there is a dynamic API situation is relatively easy to produce.
?? A small partner who does not understand the runtime can fill the runtime knowledge point. To block out this crash, you first need to know how the method is passed. Here I do a simple comb, to the demo midpoint JKVC0 This class as an example, the method called is jkclicked
1 , calling Jkclicked[jkvc0 Jkclicked]; //if the Jkclicked method is not found perform subsequent operations 2 , calls the Jkclicked method in the parent class //if there is no jkclicked method in the parent class, The parent class of the parent class is continued to call the Jkclicked method until NSObject calls the Jkclicked method, and if NSObject also does not invoke the Jkclicked method, the following action is performed 3 , dynamic Method Resolution + (bool ) Resolveinstancemethod: (SEL) Asel; //dynamic Add method, but if we do not dynamically add the implementation of the Jkclicked method, then the following redirection operation is done. 4 , Message forwarding-(id ) Forwardingtargetforselector: (SEL) Aselector//if there is no message forwarding operation, or there is still no normal implementation method call after message forwarding, The face operation after the line is followed by 5 , Crash-(void ) Doesnotrecognizeselector: (SEL) Aselector//tune This method will also appear Carsh, program Flash back.
such as big fruit home to this process is not too familiar with, I looked for a picture from the Internet, we can see.
?? Know the function of the call process, then my side in the message forwarding time to operate, in NSObject the category following several methods to rewrite, with the body:
-(ID) Forwardingtargetforselector: (SEL) aselector{//Resets the redirected message recipient to nil return Nil;} - (void) Forwardinvocation: (nsinvocation *) aninvocation{//Override this method, where it does nothing, and the mask generates crash method calls}-(Nsmethodsignature *) Methodsignatureforselector: (SEL) Aselector {NSString*methodname =nsstringfromselector (Aselector);if([MethodName hasprefix:@"_"]) {//Private method does not perform crash log capture operations return Nil; }NSString*crashmessages = [NSStringstringwithformat:@"Jkcrashprotect: [%@%@]: Unrecognized selector sent to instance", Self, Nsstringfromselector (Aselector)]; Nsmethodsignature *signature = [Jkcrashprotect instancemethodsignatureforselector:@selector(jkcrashprotectcollectcrashmessages:)]; [[Jkcrashprotect New] jkcrashprotectcollectcrashmessages:crashmessages];returnSignature//Rewrite the methodsignatureforselector, or the Forwardinvocation method will not be called}
Among them JKCrashProtectCollectCrashMessages实如现下 :
- (void)JKCrashProtectCollectCrashMessages:(NSString *)crashMessage{ NSLog(@"%@",crashMessage);}
The core code is finished, and then I will show you my demo.
As you can see, the tap Gesture and button, starting out of the method is not in existence, but, and have not produced Carsh, but print out the relevant log information to facilitate our positioning crash.
Demo Address
Cocoapod:
"JKCrashProtect"
Crash protection in iOS (i) unrecognized selector sent to instance