├── .gitignore ├── LICENSE └── YSC-Avoid-Crash ├── YSC-Avoid-Crash.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist └── YSC-Avoid-Crash ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Examples ├── Base │ ├── BaseDefine.h │ ├── BaseViewController.h │ └── BaseViewController.m ├── Containers │ ├── TestContainersVC.h │ └── TestContainersVC.m ├── KVC │ ├── KVCCrashObject.h │ ├── KVCCrashObject.m │ ├── TestKVCCrashVC.h │ └── TestKVCCrashVC.m ├── KVO │ ├── KVOCrashObject.h │ ├── KVOCrashObject.m │ ├── TestKVOCrashVC.h │ └── TestKVOCrashVC.m ├── NSNull │ ├── TestNullVC.h │ └── TestNullVC.m ├── Notification │ ├── NotificationReceiver.h │ ├── NotificationReceiver.m │ ├── TestNotificationCrashVC.h │ └── TestNotificationCrashVC.m ├── Timer │ ├── TestTimerCrashVC.h │ └── TestTimerCrashVC.m └── Unrecognized Selector │ ├── SelectorObject.h │ ├── SelectorObject.m │ ├── TestUnrecognizedSelVC.h │ └── TestUnrecognizedSelVC.m ├── Info.plist ├── NSObject+KVODefenderEasy.h ├── NSObject+KVODefenderEasy.m ├── ViewController.h ├── ViewController.m ├── YSCDefender ├── NSObject+KVCDefender.h ├── NSObject+KVCDefender.m ├── NSObject+KVODefender.h ├── NSObject+KVODefender.m ├── NSObject+MethodSwizzling.h ├── NSObject+MethodSwizzling.m ├── NSObject+SelectorDefender.h └── NSObject+SelectorDefender.m └── main.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | # CocoaPods 32 | # 33 | # We recommend against adding the Pods directory to your .gitignore. However 34 | # you should judge for yourself, the pros and cons are mentioned at: 35 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 36 | # 37 | # Pods/ 38 | 39 | # Carthage 40 | # 41 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 42 | # Carthage/Checkouts 43 | 44 | Carthage/Build 45 | 46 | # fastlane 47 | # 48 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 49 | # screenshots whenever they are needed. 50 | # For more information about the recommended setup visit: 51 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 52 | 53 | fastlane/report.xml 54 | fastlane/Preview.html 55 | fastlane/screenshots/**/*.png 56 | fastlane/test_output 57 | 58 | # Code Injection 59 | # 60 | # After new code Injection tools there's a generated folder /iOSInjectionProject 61 | # https://github.com/johnno1962/injectionforxcode 62 | 63 | iOSInjectionProject/ 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 9534591A230FBF6C00767E14 /* KVOCrashObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345919230FBF6C00767E14 /* KVOCrashObject.m */; }; 11 | 9534592D231677CB00767E14 /* KVCCrashObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 9534592C231677CB00767E14 /* KVCCrashObject.m */; }; 12 | 9534594D231DF76900767E14 /* NotificationReceiver.m in Sources */ = {isa = PBXBuildFile; fileRef = 9534594C231DF76900767E14 /* NotificationReceiver.m */; }; 13 | 95345964231F52B700767E14 /* TestUnrecognizedSelVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345963231F52B700767E14 /* TestUnrecognizedSelVC.m */; }; 14 | 95345967231F52D000767E14 /* TestKVOCrashVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345966231F52D000767E14 /* TestKVOCrashVC.m */; }; 15 | 9534596A231F52E900767E14 /* TestKVCCrashVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345969231F52E900767E14 /* TestKVCCrashVC.m */; }; 16 | 9534596D231F530100767E14 /* TestNotificationCrashVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 9534596C231F530100767E14 /* TestNotificationCrashVC.m */; }; 17 | 95345970231F531E00767E14 /* TestTimerCrashVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 9534596F231F531E00767E14 /* TestTimerCrashVC.m */; }; 18 | 95345973231F533200767E14 /* TestContainersVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345972231F533200767E14 /* TestContainersVC.m */; }; 19 | 95345976231F534400767E14 /* TestNullVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345975231F534400767E14 /* TestNullVC.m */; }; 20 | 95345979231F57EE00767E14 /* BaseViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345978231F57EE00767E14 /* BaseViewController.m */; }; 21 | 95345984231F619A00767E14 /* SelectorObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345983231F619A00767E14 /* SelectorObject.m */; }; 22 | 9534598D231F791B00767E14 /* NSObject+SelectorDefender.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345986231F791B00767E14 /* NSObject+SelectorDefender.m */; }; 23 | 9534598E231F791B00767E14 /* NSObject+KVCDefender.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345987231F791B00767E14 /* NSObject+KVCDefender.m */; }; 24 | 9534598F231F791B00767E14 /* NSObject+KVODefender.m in Sources */ = {isa = PBXBuildFile; fileRef = 95345988231F791B00767E14 /* NSObject+KVODefender.m */; }; 25 | 95345990231F791B00767E14 /* NSObject+MethodSwizzling.m in Sources */ = {isa = PBXBuildFile; fileRef = 9534598A231F791B00767E14 /* NSObject+MethodSwizzling.m */; }; 26 | 953F56E223055D1A00710599 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 953F56E123055D1A00710599 /* AppDelegate.m */; }; 27 | 953F56E523055D1A00710599 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 953F56E423055D1A00710599 /* ViewController.m */; }; 28 | 953F56E823055D1A00710599 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 953F56E623055D1A00710599 /* Main.storyboard */; }; 29 | 953F56EA23055D1B00710599 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 953F56E923055D1B00710599 /* Assets.xcassets */; }; 30 | 953F56ED23055D1B00710599 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 953F56EB23055D1B00710599 /* LaunchScreen.storyboard */; }; 31 | 953F56F023055D1B00710599 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 953F56EF23055D1B00710599 /* main.m */; }; 32 | /* End PBXBuildFile section */ 33 | 34 | /* Begin PBXFileReference section */ 35 | 95345918230FBF6C00767E14 /* KVOCrashObject.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KVOCrashObject.h; sourceTree = ""; }; 36 | 95345919230FBF6C00767E14 /* KVOCrashObject.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KVOCrashObject.m; sourceTree = ""; }; 37 | 9534592B231677CB00767E14 /* KVCCrashObject.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KVCCrashObject.h; sourceTree = ""; }; 38 | 9534592C231677CB00767E14 /* KVCCrashObject.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KVCCrashObject.m; sourceTree = ""; }; 39 | 9534594B231DF76900767E14 /* NotificationReceiver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NotificationReceiver.h; sourceTree = ""; }; 40 | 9534594C231DF76900767E14 /* NotificationReceiver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NotificationReceiver.m; sourceTree = ""; }; 41 | 95345962231F52B700767E14 /* TestUnrecognizedSelVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestUnrecognizedSelVC.h; sourceTree = ""; }; 42 | 95345963231F52B700767E14 /* TestUnrecognizedSelVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TestUnrecognizedSelVC.m; sourceTree = ""; }; 43 | 95345965231F52D000767E14 /* TestKVOCrashVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestKVOCrashVC.h; sourceTree = ""; }; 44 | 95345966231F52D000767E14 /* TestKVOCrashVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TestKVOCrashVC.m; sourceTree = ""; }; 45 | 95345968231F52E900767E14 /* TestKVCCrashVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestKVCCrashVC.h; sourceTree = ""; }; 46 | 95345969231F52E900767E14 /* TestKVCCrashVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TestKVCCrashVC.m; sourceTree = ""; }; 47 | 9534596B231F530100767E14 /* TestNotificationCrashVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestNotificationCrashVC.h; sourceTree = ""; }; 48 | 9534596C231F530100767E14 /* TestNotificationCrashVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TestNotificationCrashVC.m; sourceTree = ""; }; 49 | 9534596E231F531E00767E14 /* TestTimerCrashVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestTimerCrashVC.h; sourceTree = ""; }; 50 | 9534596F231F531E00767E14 /* TestTimerCrashVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TestTimerCrashVC.m; sourceTree = ""; }; 51 | 95345971231F533200767E14 /* TestContainersVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestContainersVC.h; sourceTree = ""; }; 52 | 95345972231F533200767E14 /* TestContainersVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TestContainersVC.m; sourceTree = ""; }; 53 | 95345974231F534400767E14 /* TestNullVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestNullVC.h; sourceTree = ""; }; 54 | 95345975231F534400767E14 /* TestNullVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TestNullVC.m; sourceTree = ""; }; 55 | 95345977231F57EE00767E14 /* BaseViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BaseViewController.h; sourceTree = ""; }; 56 | 95345978231F57EE00767E14 /* BaseViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BaseViewController.m; sourceTree = ""; }; 57 | 9534597E231F5C2700767E14 /* BaseDefine.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BaseDefine.h; sourceTree = ""; }; 58 | 95345982231F619A00767E14 /* SelectorObject.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SelectorObject.h; sourceTree = ""; }; 59 | 95345983231F619A00767E14 /* SelectorObject.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SelectorObject.m; sourceTree = ""; }; 60 | 95345985231F791B00767E14 /* NSObject+SelectorDefender.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+SelectorDefender.h"; sourceTree = ""; }; 61 | 95345986231F791B00767E14 /* NSObject+SelectorDefender.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+SelectorDefender.m"; sourceTree = ""; }; 62 | 95345987231F791B00767E14 /* NSObject+KVCDefender.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+KVCDefender.m"; sourceTree = ""; }; 63 | 95345988231F791B00767E14 /* NSObject+KVODefender.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+KVODefender.m"; sourceTree = ""; }; 64 | 95345989231F791B00767E14 /* NSObject+KVCDefender.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+KVCDefender.h"; sourceTree = ""; }; 65 | 9534598A231F791B00767E14 /* NSObject+MethodSwizzling.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSObject+MethodSwizzling.m"; sourceTree = ""; }; 66 | 9534598B231F791B00767E14 /* NSObject+MethodSwizzling.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+MethodSwizzling.h"; sourceTree = ""; }; 67 | 9534598C231F791B00767E14 /* NSObject+KVODefender.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSObject+KVODefender.h"; sourceTree = ""; }; 68 | 953F56DD23055D1A00710599 /* YSC-Avoid-Crash.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "YSC-Avoid-Crash.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 69 | 953F56E023055D1A00710599 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 70 | 953F56E123055D1A00710599 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 71 | 953F56E323055D1A00710599 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 72 | 953F56E423055D1A00710599 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 73 | 953F56E723055D1A00710599 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 74 | 953F56E923055D1B00710599 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 75 | 953F56EC23055D1B00710599 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 76 | 953F56EE23055D1B00710599 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 77 | 953F56EF23055D1B00710599 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 78 | /* End PBXFileReference section */ 79 | 80 | /* Begin PBXFrameworksBuildPhase section */ 81 | 953F56DA23055D1A00710599 /* Frameworks */ = { 82 | isa = PBXFrameworksBuildPhase; 83 | buildActionMask = 2147483647; 84 | files = ( 85 | ); 86 | runOnlyForDeploymentPostprocessing = 0; 87 | }; 88 | /* End PBXFrameworksBuildPhase section */ 89 | 90 | /* Begin PBXGroup section */ 91 | 95345957231F4EC900767E14 /* YSCDefender */ = { 92 | isa = PBXGroup; 93 | children = ( 94 | 9534598B231F791B00767E14 /* NSObject+MethodSwizzling.h */, 95 | 9534598A231F791B00767E14 /* NSObject+MethodSwizzling.m */, 96 | 95345985231F791B00767E14 /* NSObject+SelectorDefender.h */, 97 | 95345986231F791B00767E14 /* NSObject+SelectorDefender.m */, 98 | 9534598C231F791B00767E14 /* NSObject+KVODefender.h */, 99 | 95345988231F791B00767E14 /* NSObject+KVODefender.m */, 100 | 95345989231F791B00767E14 /* NSObject+KVCDefender.h */, 101 | 95345987231F791B00767E14 /* NSObject+KVCDefender.m */, 102 | ); 103 | path = YSCDefender; 104 | sourceTree = ""; 105 | }; 106 | 95345958231F4EF000767E14 /* Examples */ = { 107 | isa = PBXGroup; 108 | children = ( 109 | 9534595B231F51FE00767E14 /* Unrecognized Selector */, 110 | 9534595C231F521100767E14 /* KVO */, 111 | 9534595D231F521500767E14 /* KVC */, 112 | 9534595E231F521F00767E14 /* Notification */, 113 | 9534595F231F523100767E14 /* Timer */, 114 | 95345960231F523D00767E14 /* Containers */, 115 | 95345961231F525100767E14 /* NSNull */, 116 | 9534597A231F57F400767E14 /* Base */, 117 | ); 118 | path = Examples; 119 | sourceTree = ""; 120 | }; 121 | 9534595B231F51FE00767E14 /* Unrecognized Selector */ = { 122 | isa = PBXGroup; 123 | children = ( 124 | 95345982231F619A00767E14 /* SelectorObject.h */, 125 | 95345983231F619A00767E14 /* SelectorObject.m */, 126 | 95345962231F52B700767E14 /* TestUnrecognizedSelVC.h */, 127 | 95345963231F52B700767E14 /* TestUnrecognizedSelVC.m */, 128 | ); 129 | path = "Unrecognized Selector"; 130 | sourceTree = ""; 131 | }; 132 | 9534595C231F521100767E14 /* KVO */ = { 133 | isa = PBXGroup; 134 | children = ( 135 | 95345918230FBF6C00767E14 /* KVOCrashObject.h */, 136 | 95345919230FBF6C00767E14 /* KVOCrashObject.m */, 137 | 95345965231F52D000767E14 /* TestKVOCrashVC.h */, 138 | 95345966231F52D000767E14 /* TestKVOCrashVC.m */, 139 | ); 140 | path = KVO; 141 | sourceTree = ""; 142 | }; 143 | 9534595D231F521500767E14 /* KVC */ = { 144 | isa = PBXGroup; 145 | children = ( 146 | 9534592B231677CB00767E14 /* KVCCrashObject.h */, 147 | 9534592C231677CB00767E14 /* KVCCrashObject.m */, 148 | 95345968231F52E900767E14 /* TestKVCCrashVC.h */, 149 | 95345969231F52E900767E14 /* TestKVCCrashVC.m */, 150 | ); 151 | path = KVC; 152 | sourceTree = ""; 153 | }; 154 | 9534595E231F521F00767E14 /* Notification */ = { 155 | isa = PBXGroup; 156 | children = ( 157 | 9534594B231DF76900767E14 /* NotificationReceiver.h */, 158 | 9534594C231DF76900767E14 /* NotificationReceiver.m */, 159 | 9534596B231F530100767E14 /* TestNotificationCrashVC.h */, 160 | 9534596C231F530100767E14 /* TestNotificationCrashVC.m */, 161 | ); 162 | path = Notification; 163 | sourceTree = ""; 164 | }; 165 | 9534595F231F523100767E14 /* Timer */ = { 166 | isa = PBXGroup; 167 | children = ( 168 | 9534596E231F531E00767E14 /* TestTimerCrashVC.h */, 169 | 9534596F231F531E00767E14 /* TestTimerCrashVC.m */, 170 | ); 171 | path = Timer; 172 | sourceTree = ""; 173 | }; 174 | 95345960231F523D00767E14 /* Containers */ = { 175 | isa = PBXGroup; 176 | children = ( 177 | 95345971231F533200767E14 /* TestContainersVC.h */, 178 | 95345972231F533200767E14 /* TestContainersVC.m */, 179 | ); 180 | path = Containers; 181 | sourceTree = ""; 182 | }; 183 | 95345961231F525100767E14 /* NSNull */ = { 184 | isa = PBXGroup; 185 | children = ( 186 | 95345974231F534400767E14 /* TestNullVC.h */, 187 | 95345975231F534400767E14 /* TestNullVC.m */, 188 | ); 189 | path = NSNull; 190 | sourceTree = ""; 191 | }; 192 | 9534597A231F57F400767E14 /* Base */ = { 193 | isa = PBXGroup; 194 | children = ( 195 | 95345977231F57EE00767E14 /* BaseViewController.h */, 196 | 95345978231F57EE00767E14 /* BaseViewController.m */, 197 | 9534597E231F5C2700767E14 /* BaseDefine.h */, 198 | ); 199 | path = Base; 200 | sourceTree = ""; 201 | }; 202 | 953F56D423055D1A00710599 = { 203 | isa = PBXGroup; 204 | children = ( 205 | 953F56DF23055D1A00710599 /* YSC-Avoid-Crash */, 206 | 953F56DE23055D1A00710599 /* Products */, 207 | ); 208 | sourceTree = ""; 209 | }; 210 | 953F56DE23055D1A00710599 /* Products */ = { 211 | isa = PBXGroup; 212 | children = ( 213 | 953F56DD23055D1A00710599 /* YSC-Avoid-Crash.app */, 214 | ); 215 | name = Products; 216 | sourceTree = ""; 217 | }; 218 | 953F56DF23055D1A00710599 /* YSC-Avoid-Crash */ = { 219 | isa = PBXGroup; 220 | children = ( 221 | 95345958231F4EF000767E14 /* Examples */, 222 | 95345957231F4EC900767E14 /* YSCDefender */, 223 | 953F56E023055D1A00710599 /* AppDelegate.h */, 224 | 953F56E123055D1A00710599 /* AppDelegate.m */, 225 | 953F56E323055D1A00710599 /* ViewController.h */, 226 | 953F56E423055D1A00710599 /* ViewController.m */, 227 | 953F56E623055D1A00710599 /* Main.storyboard */, 228 | 953F56E923055D1B00710599 /* Assets.xcassets */, 229 | 953F56EB23055D1B00710599 /* LaunchScreen.storyboard */, 230 | 953F56EE23055D1B00710599 /* Info.plist */, 231 | 953F56EF23055D1B00710599 /* main.m */, 232 | ); 233 | path = "YSC-Avoid-Crash"; 234 | sourceTree = ""; 235 | }; 236 | /* End PBXGroup section */ 237 | 238 | /* Begin PBXNativeTarget section */ 239 | 953F56DC23055D1A00710599 /* YSC-Avoid-Crash */ = { 240 | isa = PBXNativeTarget; 241 | buildConfigurationList = 953F56F323055D1B00710599 /* Build configuration list for PBXNativeTarget "YSC-Avoid-Crash" */; 242 | buildPhases = ( 243 | 953F56D923055D1A00710599 /* Sources */, 244 | 953F56DA23055D1A00710599 /* Frameworks */, 245 | 953F56DB23055D1A00710599 /* Resources */, 246 | ); 247 | buildRules = ( 248 | ); 249 | dependencies = ( 250 | ); 251 | name = "YSC-Avoid-Crash"; 252 | productName = "YSC-Avoid-Crash"; 253 | productReference = 953F56DD23055D1A00710599 /* YSC-Avoid-Crash.app */; 254 | productType = "com.apple.product-type.application"; 255 | }; 256 | /* End PBXNativeTarget section */ 257 | 258 | /* Begin PBXProject section */ 259 | 953F56D523055D1A00710599 /* Project object */ = { 260 | isa = PBXProject; 261 | attributes = { 262 | LastUpgradeCheck = 1030; 263 | ORGANIZATIONNAME = bujige; 264 | TargetAttributes = { 265 | 953F56DC23055D1A00710599 = { 266 | CreatedOnToolsVersion = 10.3; 267 | }; 268 | }; 269 | }; 270 | buildConfigurationList = 953F56D823055D1A00710599 /* Build configuration list for PBXProject "YSC-Avoid-Crash" */; 271 | compatibilityVersion = "Xcode 9.3"; 272 | developmentRegion = en; 273 | hasScannedForEncodings = 0; 274 | knownRegions = ( 275 | en, 276 | Base, 277 | ); 278 | mainGroup = 953F56D423055D1A00710599; 279 | productRefGroup = 953F56DE23055D1A00710599 /* Products */; 280 | projectDirPath = ""; 281 | projectRoot = ""; 282 | targets = ( 283 | 953F56DC23055D1A00710599 /* YSC-Avoid-Crash */, 284 | ); 285 | }; 286 | /* End PBXProject section */ 287 | 288 | /* Begin PBXResourcesBuildPhase section */ 289 | 953F56DB23055D1A00710599 /* Resources */ = { 290 | isa = PBXResourcesBuildPhase; 291 | buildActionMask = 2147483647; 292 | files = ( 293 | 953F56ED23055D1B00710599 /* LaunchScreen.storyboard in Resources */, 294 | 953F56EA23055D1B00710599 /* Assets.xcassets in Resources */, 295 | 953F56E823055D1A00710599 /* Main.storyboard in Resources */, 296 | ); 297 | runOnlyForDeploymentPostprocessing = 0; 298 | }; 299 | /* End PBXResourcesBuildPhase section */ 300 | 301 | /* Begin PBXSourcesBuildPhase section */ 302 | 953F56D923055D1A00710599 /* Sources */ = { 303 | isa = PBXSourcesBuildPhase; 304 | buildActionMask = 2147483647; 305 | files = ( 306 | 95345970231F531E00767E14 /* TestTimerCrashVC.m in Sources */, 307 | 9534591A230FBF6C00767E14 /* KVOCrashObject.m in Sources */, 308 | 953F56E523055D1A00710599 /* ViewController.m in Sources */, 309 | 953F56F023055D1B00710599 /* main.m in Sources */, 310 | 9534594D231DF76900767E14 /* NotificationReceiver.m in Sources */, 311 | 95345984231F619A00767E14 /* SelectorObject.m in Sources */, 312 | 953F56E223055D1A00710599 /* AppDelegate.m in Sources */, 313 | 9534598D231F791B00767E14 /* NSObject+SelectorDefender.m in Sources */, 314 | 9534598E231F791B00767E14 /* NSObject+KVCDefender.m in Sources */, 315 | 9534592D231677CB00767E14 /* KVCCrashObject.m in Sources */, 316 | 9534596D231F530100767E14 /* TestNotificationCrashVC.m in Sources */, 317 | 95345967231F52D000767E14 /* TestKVOCrashVC.m in Sources */, 318 | 9534596A231F52E900767E14 /* TestKVCCrashVC.m in Sources */, 319 | 95345964231F52B700767E14 /* TestUnrecognizedSelVC.m in Sources */, 320 | 95345990231F791B00767E14 /* NSObject+MethodSwizzling.m in Sources */, 321 | 95345973231F533200767E14 /* TestContainersVC.m in Sources */, 322 | 95345976231F534400767E14 /* TestNullVC.m in Sources */, 323 | 95345979231F57EE00767E14 /* BaseViewController.m in Sources */, 324 | 9534598F231F791B00767E14 /* NSObject+KVODefender.m in Sources */, 325 | ); 326 | runOnlyForDeploymentPostprocessing = 0; 327 | }; 328 | /* End PBXSourcesBuildPhase section */ 329 | 330 | /* Begin PBXVariantGroup section */ 331 | 953F56E623055D1A00710599 /* Main.storyboard */ = { 332 | isa = PBXVariantGroup; 333 | children = ( 334 | 953F56E723055D1A00710599 /* Base */, 335 | ); 336 | name = Main.storyboard; 337 | sourceTree = ""; 338 | }; 339 | 953F56EB23055D1B00710599 /* LaunchScreen.storyboard */ = { 340 | isa = PBXVariantGroup; 341 | children = ( 342 | 953F56EC23055D1B00710599 /* Base */, 343 | ); 344 | name = LaunchScreen.storyboard; 345 | sourceTree = ""; 346 | }; 347 | /* End PBXVariantGroup section */ 348 | 349 | /* Begin XCBuildConfiguration section */ 350 | 953F56F123055D1B00710599 /* Debug */ = { 351 | isa = XCBuildConfiguration; 352 | buildSettings = { 353 | ALWAYS_SEARCH_USER_PATHS = NO; 354 | CLANG_ANALYZER_NONNULL = YES; 355 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 356 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 357 | CLANG_CXX_LIBRARY = "libc++"; 358 | CLANG_ENABLE_MODULES = YES; 359 | CLANG_ENABLE_OBJC_ARC = YES; 360 | CLANG_ENABLE_OBJC_WEAK = YES; 361 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 362 | CLANG_WARN_BOOL_CONVERSION = YES; 363 | CLANG_WARN_COMMA = YES; 364 | CLANG_WARN_CONSTANT_CONVERSION = YES; 365 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 366 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 367 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 368 | CLANG_WARN_EMPTY_BODY = YES; 369 | CLANG_WARN_ENUM_CONVERSION = YES; 370 | CLANG_WARN_INFINITE_RECURSION = YES; 371 | CLANG_WARN_INT_CONVERSION = YES; 372 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 373 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 374 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 375 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 376 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 377 | CLANG_WARN_STRICT_PROTOTYPES = YES; 378 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 379 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 380 | CLANG_WARN_UNREACHABLE_CODE = YES; 381 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 382 | CODE_SIGN_IDENTITY = "iPhone Developer"; 383 | COPY_PHASE_STRIP = NO; 384 | DEBUG_INFORMATION_FORMAT = dwarf; 385 | ENABLE_STRICT_OBJC_MSGSEND = YES; 386 | ENABLE_TESTABILITY = YES; 387 | GCC_C_LANGUAGE_STANDARD = gnu11; 388 | GCC_DYNAMIC_NO_PIC = NO; 389 | GCC_NO_COMMON_BLOCKS = YES; 390 | GCC_OPTIMIZATION_LEVEL = 0; 391 | GCC_PREPROCESSOR_DEFINITIONS = ( 392 | "DEBUG=1", 393 | "$(inherited)", 394 | ); 395 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 396 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 397 | GCC_WARN_UNDECLARED_SELECTOR = YES; 398 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 399 | GCC_WARN_UNUSED_FUNCTION = YES; 400 | GCC_WARN_UNUSED_VARIABLE = YES; 401 | IPHONEOS_DEPLOYMENT_TARGET = 12.4; 402 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 403 | MTL_FAST_MATH = YES; 404 | ONLY_ACTIVE_ARCH = YES; 405 | SDKROOT = iphoneos; 406 | }; 407 | name = Debug; 408 | }; 409 | 953F56F223055D1B00710599 /* Release */ = { 410 | isa = XCBuildConfiguration; 411 | buildSettings = { 412 | ALWAYS_SEARCH_USER_PATHS = NO; 413 | CLANG_ANALYZER_NONNULL = YES; 414 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 415 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 416 | CLANG_CXX_LIBRARY = "libc++"; 417 | CLANG_ENABLE_MODULES = YES; 418 | CLANG_ENABLE_OBJC_ARC = YES; 419 | CLANG_ENABLE_OBJC_WEAK = YES; 420 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 421 | CLANG_WARN_BOOL_CONVERSION = YES; 422 | CLANG_WARN_COMMA = YES; 423 | CLANG_WARN_CONSTANT_CONVERSION = YES; 424 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 425 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 426 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 427 | CLANG_WARN_EMPTY_BODY = YES; 428 | CLANG_WARN_ENUM_CONVERSION = YES; 429 | CLANG_WARN_INFINITE_RECURSION = YES; 430 | CLANG_WARN_INT_CONVERSION = YES; 431 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 432 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 433 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 434 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 435 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 436 | CLANG_WARN_STRICT_PROTOTYPES = YES; 437 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 438 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 439 | CLANG_WARN_UNREACHABLE_CODE = YES; 440 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 441 | CODE_SIGN_IDENTITY = "iPhone Developer"; 442 | COPY_PHASE_STRIP = NO; 443 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 444 | ENABLE_NS_ASSERTIONS = NO; 445 | ENABLE_STRICT_OBJC_MSGSEND = YES; 446 | GCC_C_LANGUAGE_STANDARD = gnu11; 447 | GCC_NO_COMMON_BLOCKS = YES; 448 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 449 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 450 | GCC_WARN_UNDECLARED_SELECTOR = YES; 451 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 452 | GCC_WARN_UNUSED_FUNCTION = YES; 453 | GCC_WARN_UNUSED_VARIABLE = YES; 454 | IPHONEOS_DEPLOYMENT_TARGET = 12.4; 455 | MTL_ENABLE_DEBUG_INFO = NO; 456 | MTL_FAST_MATH = YES; 457 | SDKROOT = iphoneos; 458 | VALIDATE_PRODUCT = YES; 459 | }; 460 | name = Release; 461 | }; 462 | 953F56F423055D1B00710599 /* Debug */ = { 463 | isa = XCBuildConfiguration; 464 | buildSettings = { 465 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 466 | CODE_SIGN_STYLE = Automatic; 467 | DEVELOPMENT_TEAM = ""; 468 | INFOPLIST_FILE = "YSC-Avoid-Crash/Info.plist"; 469 | IPHONEOS_DEPLOYMENT_TARGET = 8.4; 470 | LD_RUNPATH_SEARCH_PATHS = ( 471 | "$(inherited)", 472 | "@executable_path/Frameworks", 473 | ); 474 | PRODUCT_BUNDLE_IDENTIFIER = "net.bujige.YSC-Avoid-Crash"; 475 | PRODUCT_NAME = "$(TARGET_NAME)"; 476 | TARGETED_DEVICE_FAMILY = "1,2"; 477 | }; 478 | name = Debug; 479 | }; 480 | 953F56F523055D1B00710599 /* Release */ = { 481 | isa = XCBuildConfiguration; 482 | buildSettings = { 483 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 484 | CODE_SIGN_STYLE = Automatic; 485 | DEVELOPMENT_TEAM = ""; 486 | INFOPLIST_FILE = "YSC-Avoid-Crash/Info.plist"; 487 | IPHONEOS_DEPLOYMENT_TARGET = 8.4; 488 | LD_RUNPATH_SEARCH_PATHS = ( 489 | "$(inherited)", 490 | "@executable_path/Frameworks", 491 | ); 492 | PRODUCT_BUNDLE_IDENTIFIER = "net.bujige.YSC-Avoid-Crash"; 493 | PRODUCT_NAME = "$(TARGET_NAME)"; 494 | TARGETED_DEVICE_FAMILY = "1,2"; 495 | }; 496 | name = Release; 497 | }; 498 | /* End XCBuildConfiguration section */ 499 | 500 | /* Begin XCConfigurationList section */ 501 | 953F56D823055D1A00710599 /* Build configuration list for PBXProject "YSC-Avoid-Crash" */ = { 502 | isa = XCConfigurationList; 503 | buildConfigurations = ( 504 | 953F56F123055D1B00710599 /* Debug */, 505 | 953F56F223055D1B00710599 /* Release */, 506 | ); 507 | defaultConfigurationIsVisible = 0; 508 | defaultConfigurationName = Release; 509 | }; 510 | 953F56F323055D1B00710599 /* Build configuration list for PBXNativeTarget "YSC-Avoid-Crash" */ = { 511 | isa = XCConfigurationList; 512 | buildConfigurations = ( 513 | 953F56F423055D1B00710599 /* Debug */, 514 | 953F56F523055D1B00710599 /* Release */, 515 | ); 516 | defaultConfigurationIsVisible = 0; 517 | defaultConfigurationName = Release; 518 | }; 519 | /* End XCConfigurationList section */ 520 | }; 521 | rootObject = 953F56D523055D1A00710599 /* Project object */; 522 | } 523 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/15. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/15. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Base/BaseDefine.h: -------------------------------------------------------------------------------- 1 | // 2 | // BaseDefine.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #ifndef BaseDefine_h 10 | #define BaseDefine_h 11 | 12 | #define UIScreenWidth ([[UIScreen mainScreen] bounds].size.width) 13 | #define UIScreenHeigh ([[UIScreen mainScreen] bounds].size.height) 14 | 15 | #endif /* BaseDefine_h */ 16 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Base/BaseViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // BaseViewController.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "BaseDefine.h" 11 | 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | @interface BaseViewController : UIViewController 15 | 16 | - (void)redirectSTD:(int)fd; 17 | 18 | @end 19 | 20 | NS_ASSUME_NONNULL_END 21 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Base/BaseViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // BaseViewController.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "BaseViewController.h" 10 | 11 | @interface BaseViewController () 12 | 13 | /* <#注释#> */ 14 | @property (nonatomic, strong) UIButton *backButton; 15 | 16 | /* <#注释#> */ 17 | @property (nonatomic, strong) UITextView *logTextView; 18 | @end 19 | 20 | @implementation BaseViewController 21 | 22 | - (void)viewDidLoad { 23 | [super viewDidLoad]; 24 | 25 | self.view.backgroundColor = [UIColor whiteColor]; 26 | [self.view addSubview:self.backButton]; 27 | [self.view addSubview:self.logTextView]; 28 | 29 | [self redirectSTD:STDOUT_FILENO]; 30 | [self redirectSTD:STDERR_FILENO]; 31 | } 32 | 33 | /** 34 | * backButton初始化 35 | */ 36 | - (UIButton *)backButton { 37 | if (!_backButton) { 38 | _backButton = [UIButton buttonWithType:UIButtonTypeCustom]; 39 | _backButton.frame = CGRectMake(15, 12, 44, 40); 40 | _backButton.contentMode = UIViewContentModeLeft; 41 | [_backButton setTitle:@"返回" forState:UIControlStateNormal]; 42 | [_backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; 43 | [_backButton addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside]; 44 | } 45 | return _backButton; 46 | } 47 | 48 | - (void)backButtonClick:(UIButton *)button { 49 | [self dismissViewControllerAnimated:YES completion:nil]; 50 | } 51 | 52 | /** 53 | * logTextView初始化 54 | */ 55 | - (UITextView *)logTextView { 56 | if (!_logTextView) { 57 | _logTextView = [[UITextView alloc] initWithFrame:CGRectMake(30, UIScreenHeigh-170, UIScreenWidth-60, 150)]; 58 | [_logTextView setEditable:NO]; 59 | [_logTextView setBackgroundColor:[UIColor lightGrayColor]]; 60 | [_logTextView setTextColor:[UIColor blackColor]]; 61 | [_logTextView setFont:[UIFont systemFontOfSize:15]]; 62 | } 63 | return _logTextView; 64 | } 65 | 66 | 67 | - (void)redirectNotificationHandle:(NSNotification *)nf{ // 通知方法 68 | NSData *data = [[nf userInfo] objectForKey:NSFileHandleNotificationDataItem]; 69 | NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 70 | 71 | self.logTextView.text = [NSString stringWithFormat:@"%@\n\n%@",self.logTextView.text, str]; // logTextView 就是要将日志输出的视图(UITextView) 72 | NSRange range; 73 | range.location = [self.logTextView.text length] - 1; 74 | range.length = 0; 75 | [self.logTextView scrollRangeToVisible:range]; 76 | [[nf object] readInBackgroundAndNotify]; 77 | } 78 | 79 | - (void)redirectSTD:(int)fd { 80 | NSPipe * pipe = [NSPipe pipe] ;// 初始化一个NSPipe 对象 81 | NSFileHandle *pipeReadHandle = [pipe fileHandleForReading] ; 82 | dup2([[pipe fileHandleForWriting] fileDescriptor], fd) ; 83 | 84 | [[NSNotificationCenter defaultCenter] addObserver:self 85 | selector:@selector(redirectNotificationHandle:) 86 | name:NSFileHandleReadCompletionNotification 87 | object:pipeReadHandle]; // 注册通知 88 | [pipeReadHandle readInBackgroundAndNotify]; 89 | } 90 | 91 | 92 | @end 93 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Containers/TestContainersVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestContainersVC.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "BaseViewController.h" 11 | 12 | @interface TestContainersVC : BaseViewController 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Containers/TestContainersVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestContainersVC.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "TestContainersVC.h" 10 | 11 | @interface TestContainersVC () 12 | 13 | @end 14 | 15 | @implementation TestContainersVC 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | // Do any additional setup after loading the view. 20 | } 21 | 22 | /* 23 | #pragma mark - Navigation 24 | 25 | // In a storyboard-based application, you will often want to do a little preparation before navigation 26 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 27 | // Get the new view controller using [segue destinationViewController]. 28 | // Pass the selected object to the new view controller. 29 | } 30 | */ 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/KVC/KVCCrashObject.h: -------------------------------------------------------------------------------- 1 | // 2 | // KVCCrashObject.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/28. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface KVCCrashObject : NSObject 14 | 15 | @property (nonatomic, copy) NSString *name; 16 | 17 | @property (nonatomic, assign) NSInteger age; 18 | 19 | @end 20 | 21 | NS_ASSUME_NONNULL_END 22 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/KVC/KVCCrashObject.m: -------------------------------------------------------------------------------- 1 | // 2 | // KVCCrashObject.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/28. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "KVCCrashObject.h" 10 | 11 | @implementation KVCCrashObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/KVC/TestKVCCrashVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestKVCCrashVC.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "BaseViewController.h" 11 | 12 | @interface TestKVCCrashVC : BaseViewController 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/KVC/TestKVCCrashVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestKVCCrashVC.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "TestKVCCrashVC.h" 10 | #import "KVCCrashObject.h" 11 | 12 | @interface TestKVCCrashVC () 13 | 14 | @end 15 | 16 | @implementation TestKVCCrashVC 17 | { 18 | NSArray *_titleArray; 19 | } 20 | 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | 24 | [self setupDataSource]; 25 | 26 | [self setupUI]; 27 | } 28 | 29 | - (void)setupDataSource { 30 | _titleArray = @[ 31 | @"1. key 不是对象的属性", 32 | @"2. keyPath 不正确", 33 | @"3. key 为 nil", 34 | @"4. value 为 nil,为非对象设值" 35 | ]; 36 | } 37 | 38 | - (void)setupUI { 39 | CGFloat buttonWidth = (UIScreenWidth-60); 40 | CGFloat buttonHeight = 44; 41 | CGFloat buttonSpace = 30; 42 | CGFloat buttonGap = 10; 43 | for (int i = 0; i < _titleArray.count; i++) { 44 | UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 45 | button.tag = 1000+i; 46 | [button setTitle:_titleArray[i] forState:UIControlStateNormal]; 47 | [button setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal]; 48 | button.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle; 49 | button.frame = CGRectMake(buttonSpace, 60+(buttonHeight+buttonGap)*i, buttonWidth, buttonHeight); 50 | [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; 51 | button.layer.cornerRadius = 5; 52 | button.titleLabel.font = [UIFont systemFontOfSize:14]; 53 | button.backgroundColor = [UIColor colorWithRed:214/255.0 green:235/255.0 blue:253/255.0 alpha:1]; 54 | [self.view addSubview:button]; 55 | } 56 | } 57 | 58 | - (void)buttonClick:(UIButton *)button { 59 | NSInteger buttonTag = button.tag; 60 | switch (buttonTag) { 61 | case 1000: { 62 | [self testKVCCrash1]; 63 | } 64 | break; 65 | case 1001: { 66 | [self testKVCCrash2]; 67 | } 68 | break; 69 | case 1002: { 70 | [self testKVCCrash3]; 71 | } 72 | break; 73 | case 1003: { 74 | [self testKVCCrash4]; 75 | } 76 | break; 77 | default: 78 | break; 79 | } 80 | } 81 | 82 | /** 83 | 1. key 不是对象的属性,造成崩溃 84 | */ 85 | - (void)testKVCCrash1 { 86 | // 崩溃日志:[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key XXX.; 87 | 88 | KVCCrashObject *objc = [[KVCCrashObject alloc] init]; 89 | [objc setValue:@"value" forKey:@"address"]; 90 | } 91 | 92 | /** 93 | 2. keyPath 不正确,造成崩溃 94 | */ 95 | - (void)testKVCCrash2 { 96 | // 崩溃日志:[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key XXX. 97 | 98 | KVCCrashObject *objc = [[KVCCrashObject alloc] init]; 99 | [objc setValue:@"后厂村路" forKeyPath:@"address.street"]; 100 | } 101 | 102 | /** 103 | 3. key 为 nil,造成崩溃 104 | */ 105 | - (void)testKVCCrash3 { 106 | // 崩溃日志:'-[KVCCrashObject setValue:forKey:]: attempt to set a value for a nil key 107 | 108 | NSString *keyName; 109 | // key 为 nil 会崩溃,如果传 nil 会提示警告,传空变量则不会提示警告 110 | 111 | KVCCrashObject *objc = [[KVCCrashObject alloc] init]; 112 | [objc setValue:@"value" forKey:keyName]; 113 | } 114 | 115 | /** 116 | 4. value 为 nil,造成崩溃 117 | */ 118 | - (void)testKVCCrash4 { 119 | // 崩溃日志:[ setNilValueForKey]: could not set nil as the value for the key XXX. 120 | 121 | // value 为 nil 会崩溃 122 | KVCCrashObject *objc = [[KVCCrashObject alloc] init]; 123 | [objc setValue:nil forKey:@"age"]; 124 | } 125 | 126 | @end 127 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/KVO/KVOCrashObject.h: -------------------------------------------------------------------------------- 1 | // 2 | // KVOCrashObject.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/23. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface KVOCrashObject : NSObject 14 | 15 | @property (nonatomic, copy) NSString *name; 16 | 17 | @end 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/KVO/KVOCrashObject.m: -------------------------------------------------------------------------------- 1 | // 2 | // KVOCrashObject.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/23. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "KVOCrashObject.h" 10 | 11 | @implementation KVOCrashObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/KVO/TestKVOCrashVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestKVOCrashVC.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "BaseViewController.h" 11 | 12 | @interface TestKVOCrashVC : BaseViewController 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/KVO/TestKVOCrashVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestKVOCrashVC.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "TestKVOCrashVC.h" 10 | #import "KVOCrashObject.h" 11 | 12 | @interface TestKVOCrashVC () 13 | 14 | @property (nonatomic, strong) KVOCrashObject *objc; 15 | 16 | @end 17 | 18 | @implementation TestKVOCrashVC 19 | { 20 | NSArray *_titleArray; 21 | } 22 | 23 | - (void)viewDidLoad { 24 | [super viewDidLoad]; 25 | // Do any additional setup after loading the view. 26 | 27 | [self setupDataSource]; 28 | 29 | [self setupUI]; 30 | 31 | } 32 | 33 | 34 | - (void)setupDataSource { 35 | _titleArray = @[ 36 | @"1.1 移除了未注册的观察者", 37 | @"1.2 重复移除多次,移除次数多于添加次数", 38 | @"1.3 重复添加多次,被观察多次。", 39 | @"2. 被观察者 dealloc 时仍然注册着 KVO", 40 | @"3. 观察者没有实现观察方法", 41 | @"4. 添加或者移除时 keypath == nil" 42 | ]; 43 | 44 | self.objc = [[KVOCrashObject alloc] init]; 45 | } 46 | 47 | - (void)setupUI { 48 | CGFloat buttonWidth = (UIScreenWidth-60); 49 | CGFloat buttonHeight = 44; 50 | CGFloat buttonSpace = 30; 51 | CGFloat buttonGap = 10; 52 | for (int i = 0; i < _titleArray.count; i++) { 53 | UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 54 | button.tag = 1000+i; 55 | [button setTitle:_titleArray[i] forState:UIControlStateNormal]; 56 | [button setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal]; 57 | button.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle; 58 | button.frame = CGRectMake(buttonSpace, 60+(buttonHeight+buttonGap)*i, buttonWidth, buttonHeight); 59 | [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; 60 | button.layer.cornerRadius = 5; 61 | button.titleLabel.font = [UIFont systemFontOfSize:14]; 62 | button.backgroundColor = [UIColor colorWithRed:214/255.0 green:235/255.0 blue:253/255.0 alpha:1]; 63 | [self.view addSubview:button]; 64 | } 65 | } 66 | 67 | - (void)buttonClick:(UIButton *)button { 68 | NSInteger buttonTag = button.tag; 69 | switch (buttonTag) { 70 | case 1000: { 71 | [self testKVOCrash11]; 72 | } 73 | break; 74 | case 1001: { 75 | [self testKVOCrash12]; 76 | } 77 | break; 78 | case 1002: { 79 | [self testKVOCrash13]; 80 | } 81 | break; 82 | case 1003: { 83 | [self testKVOCrash2]; 84 | } 85 | break; 86 | case 1004: { 87 | [self testKVOCrash3]; 88 | } 89 | break; 90 | case 1005: { 91 | [self testKVOCrash4]; 92 | } 93 | default: 94 | break; 95 | } 96 | } 97 | 98 | 99 | /** 100 | 1.1 移除了未注册的观察者,导致崩溃 101 | */ 102 | - (void)testKVOCrash11 { 103 | // 崩溃日志:Cannot remove an observer XXX for the key path "xxx" from XXX because it is not registered as an observer. 104 | [self.objc removeObserver:self forKeyPath:@"name"]; 105 | } 106 | 107 | /** 108 | 1.2 重复移除多次,移除次数多于添加次数,导致崩溃 109 | */ 110 | - (void)testKVOCrash12 { 111 | // 崩溃日志:Cannot remove an observer XXX for the key path "xxx" from XXX because it is not registered as an observer. 112 | [self.objc addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL]; 113 | self.objc.name = @"0"; 114 | [self.objc removeObserver:self forKeyPath:@"name"]; 115 | [self.objc removeObserver:self forKeyPath:@"name"]; 116 | } 117 | 118 | /** 119 | 1.3 重复添加多次,虽然不会崩溃,但是发生改变时,也同时会被观察多次。 120 | */ 121 | - (void)testKVOCrash13 { 122 | [self.objc addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL]; 123 | [self.objc addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:NULL]; 124 | self.objc.name = @"0"; 125 | } 126 | 127 | /** 128 | 2. 被观察者 dealloc 时仍然注册着 KVO,导致崩溃 129 | */ 130 | - (void)testKVOCrash2 { 131 | // 崩溃日志:An instance xxx of class xxx was deallocated while key value observers were still registered with it. 132 | // iOS 10 及以下会导致崩溃,iOS 11 之后就不会崩溃了 133 | KVOCrashObject *obj = [[KVOCrashObject alloc] init]; 134 | [obj addObserver: self 135 | forKeyPath: @"name" 136 | options: NSKeyValueObservingOptionNew 137 | context: nil]; 138 | } 139 | 140 | /** 141 | 3. 观察者没有实现 -observeValueForKeyPath:ofObject:change:context:导致崩溃 142 | */ 143 | - (void)testKVOCrash3 { 144 | // 崩溃日志:An -observeValueForKeyPath:ofObject:change:context: message was received but not handled. 145 | KVOCrashObject *obj = [[KVOCrashObject alloc] init]; 146 | 147 | [self addObserver: obj 148 | forKeyPath: @"title" 149 | options: NSKeyValueObservingOptionNew 150 | context: nil]; 151 | 152 | self.title = @"111"; 153 | } 154 | 155 | /** 156 | 4. 添加或者移除时 keypath == nil,导致崩溃。 157 | */ 158 | - (void)testKVOCrash4 { 159 | // 崩溃日志: -[__NSCFConstantString characterAtIndex:]: Range or index out of bounds 160 | KVOCrashObject *obj = [[KVOCrashObject alloc] init]; 161 | 162 | [self addObserver: obj 163 | forKeyPath: @"" 164 | options: NSKeyValueObservingOptionNew 165 | context: nil]; 166 | 167 | // [self removeObserver:obj forKeyPath:@""]; 168 | } 169 | 170 | 171 | - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context { 172 | 173 | NSLog(@"object = %@, keyPath = %@", object, keyPath); 174 | } 175 | 176 | @end 177 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/NSNull/TestNullVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestNullVC.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "BaseViewController.h" 11 | 12 | @interface TestNullVC : BaseViewController 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/NSNull/TestNullVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestNullVC.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "TestNullVC.h" 10 | 11 | @interface TestNullVC () 12 | 13 | @end 14 | 15 | @implementation TestNullVC 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | // Do any additional setup after loading the view. 20 | } 21 | 22 | /* 23 | #pragma mark - Navigation 24 | 25 | // In a storyboard-based application, you will often want to do a little preparation before navigation 26 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 27 | // Get the new view controller using [segue destinationViewController]. 28 | // Pass the selected object to the new view controller. 29 | } 30 | */ 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Notification/NotificationReceiver.h: -------------------------------------------------------------------------------- 1 | // 2 | // NotificationReceiver.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/3. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface NotificationReceiver : NSObject 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Notification/NotificationReceiver.m: -------------------------------------------------------------------------------- 1 | // 2 | // NotificationReceiver.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/3. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "NotificationReceiver.h" 10 | 11 | @implementation NotificationReceiver 12 | 13 | - (instancetype)init { 14 | self = [super init]; 15 | if (self) { 16 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"loadingCompelete" object:nil]; 17 | } 18 | return self; 19 | } 20 | 21 | - (void)receiveNotification:(NSNotification*)noti { 22 | int page = [noti.userInfo[@"page"] intValue]; 23 | NSLog(@"%d",page); 24 | } 25 | 26 | - (void)dealloc { 27 | NSLog(@"dealloc"); 28 | } 29 | @end 30 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Notification/TestNotificationCrashVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestNotificationCrashVC.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "BaseViewController.h" 11 | 12 | @interface TestNotificationCrashVC : BaseViewController 13 | 14 | @end 15 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Notification/TestNotificationCrashVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestNotificationCrashVC.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "TestNotificationCrashVC.h" 10 | 11 | @interface TestNotificationCrashVC () 12 | 13 | @end 14 | 15 | @implementation TestNotificationCrashVC 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | // Do any additional setup after loading the view. 20 | } 21 | 22 | @end 23 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Timer/TestTimerCrashVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestTimerCrashVC.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "BaseViewController.h" 11 | 12 | @interface TestTimerCrashVC : BaseViewController 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Timer/TestTimerCrashVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestTimerCrashVC.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "TestTimerCrashVC.h" 10 | 11 | @interface TestTimerCrashVC () 12 | 13 | @end 14 | 15 | @implementation TestTimerCrashVC 16 | 17 | - (void)viewDidLoad { 18 | [super viewDidLoad]; 19 | // Do any additional setup after loading the view. 20 | } 21 | 22 | /* 23 | #pragma mark - Navigation 24 | 25 | // In a storyboard-based application, you will often want to do a little preparation before navigation 26 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 27 | // Get the new view controller using [segue destinationViewController]. 28 | // Pass the selected object to the new view controller. 29 | } 30 | */ 31 | 32 | @end 33 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Unrecognized Selector/SelectorObject.h: -------------------------------------------------------------------------------- 1 | // 2 | // SelectorObject.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface SelectorObject : NSObject 12 | 13 | + (id)classFunc; 14 | 15 | - (id)instanceFunc; 16 | 17 | @end 18 | 19 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Unrecognized Selector/SelectorObject.m: -------------------------------------------------------------------------------- 1 | // 2 | // SelectorObject.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "SelectorObject.h" 10 | 11 | @implementation SelectorObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Unrecognized Selector/TestUnrecognizedSelVC.h: -------------------------------------------------------------------------------- 1 | // 2 | // TestUnrecognizedSelVC.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "BaseViewController.h" 11 | 12 | @interface TestUnrecognizedSelVC : BaseViewController 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Examples/Unrecognized Selector/TestUnrecognizedSelVC.m: -------------------------------------------------------------------------------- 1 | // 2 | // TestUnrecognizedSelVC.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/9/4. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "TestUnrecognizedSelVC.h" 10 | #import "SelectorObject.h" 11 | 12 | @interface TestUnrecognizedSelVC () 13 | 14 | @end 15 | 16 | @implementation TestUnrecognizedSelVC 17 | { 18 | NSArray *_titleArray; 19 | } 20 | 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | 24 | [self setupDataSource]; 25 | 26 | [self setupUI]; 27 | } 28 | 29 | - (void)setupDataSource { 30 | _titleArray = @[ 31 | @"找不到 button 响应事件", 32 | @"找不到控制器中的方法", 33 | @"找不到对象方法", 34 | @"找不到类方法", 35 | @"调用 null 对象的方法", 36 | ]; 37 | } 38 | 39 | - (void)setupUI { 40 | CGFloat buttonWidth = (UIScreenWidth-60); 41 | CGFloat buttonHeight = 44; 42 | CGFloat buttonSpace = 30; 43 | CGFloat buttonGap = 10; 44 | for (int i = 0; i < _titleArray.count; i++) { 45 | UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 46 | button.tag = 1000+i; 47 | [button setTitle:_titleArray[i] forState:UIControlStateNormal]; 48 | [button setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal]; 49 | button.titleLabel.lineBreakMode = NSLineBreakByTruncatingMiddle; 50 | button.frame = CGRectMake(buttonSpace, 60+(buttonHeight+buttonGap)*i, buttonWidth, buttonHeight); 51 | if (i == 0) { 52 | [button addTarget:self action:@selector(undefinedButtonClick:) forControlEvents:UIControlEventTouchUpInside]; 53 | } else { 54 | [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside]; 55 | } 56 | 57 | button.layer.cornerRadius = 5; 58 | button.titleLabel.font = [UIFont systemFontOfSize:14]; 59 | button.backgroundColor = [UIColor colorWithRed:214/255.0 green:235/255.0 blue:253/255.0 alpha:1]; 60 | [self.view addSubview:button]; 61 | } 62 | } 63 | 64 | - (void)buttonClick:(UIButton *)button { 65 | NSInteger buttonTag = button.tag; 66 | switch (buttonTag) { 67 | case 1000: { 68 | 69 | } 70 | break; 71 | case 1001: { 72 | [self performSelector:@selector(undefinedVCSelector)]; 73 | } 74 | break; 75 | case 1002: { 76 | SelectorObject *object = [[SelectorObject alloc] init]; 77 | [object instanceFunc]; 78 | } 79 | break; 80 | case 1003: { 81 | [SelectorObject classFunc]; 82 | } 83 | break; 84 | case 1004: { 85 | [[NSNull null] performSelector:@selector(undefinedSelector)]; 86 | } 87 | break; 88 | default: 89 | break; 90 | } 91 | } 92 | 93 | 94 | @end 95 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/NSObject+KVODefenderEasy.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+KVODefender.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/19. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface NSObject (KVODefenderEasy) 14 | 15 | @property (nonatomic,strong) NSHashTable *kvoHashTable; 16 | 17 | @end 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/NSObject+KVODefenderEasy.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+KVODefender.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/19. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "NSObject+KVODefenderEasy.h" 10 | #import "NSObject+MethodSwizzling.h" 11 | #import 12 | 13 | static char KVOHashTableKey; 14 | 15 | @implementation NSObject (KVODefenderEasy) 16 | 17 | 18 | //+ (void)load { 19 | // static dispatch_once_t onceToken; 20 | // dispatch_once(&onceToken, ^{ 21 | // 22 | // // 拦截 `addObserver:forKeyPath:options:context:` 方法,替换自定义实现 23 | // [NSObject yscDefenderSwizzlingInstanceMethod: @selector(addObserver:forKeyPath:options:context:) withMethod: @selector(ysc_easy_addObserver:forKeyPath:options:context:) withClass: [NSObject class]]; 24 | // 25 | // // 拦截 `removeObserver:forKeyPath:` 方法,替换自定义实现 26 | // [NSObject yscDefenderSwizzlingInstanceMethod: @selector(removeObserver:forKeyPath:) withMethod:@selector(ysc_easy_removeObserver:forKeyPath:) withClass: [NSObject class]]; 27 | // 28 | // // 拦截 `removeObserver:forKeyPath:context:` 方法,替换自定义实现 29 | // [NSObject yscDefenderSwizzlingInstanceMethod: @selector(removeObserver:forKeyPath:context:) withMethod: @selector(ysc_easy_removeObserver:forKeyPath:context:) withClass: [NSObject class]]; 30 | // 31 | // // 拦截 `observeValueForKeyPath:ofObject:change:context:` 方法,替换自定义实现 32 | // [NSObject yscDefenderSwizzlingInstanceMethod: @selector(observeValueForKeyPath:ofObject:change:context:) withMethod: @selector(ysc_easy_observeValueForKeyPath:ofObject:change:context:) withClass:[NSObject class]]; 33 | // }); 34 | //} 35 | 36 | - (void)ysc_easy_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context { 37 | if (!observer || !keyPath || 38 | ([keyPath isKindOfClass:[NSString class]] && keyPath.length <= 0)) { 39 | return; 40 | } 41 | 42 | @synchronized (self) { 43 | NSUInteger KVOHash = [self ysc_easy_hashOfObserver:observer keyPath:keyPath]; 44 | if (!self.kvoHashTable) { 45 | self.kvoHashTable = [NSHashTable hashTableWithOptions:(NSPointerFunctionsStrongMemory)]; 46 | } 47 | 48 | if (![self.kvoHashTable containsObject:@(KVOHash)]) { 49 | [self.kvoHashTable addObject:@(KVOHash)]; 50 | [self ysc_easy_addObserver:observer forKeyPath:keyPath options:options context:context]; 51 | } 52 | } 53 | } 54 | 55 | - (void)ysc_easy_removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context { 56 | [self removeObserver:observer forKeyPath:keyPath]; 57 | } 58 | 59 | - (void)ysc_easy_removeObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath { 60 | if (!observer || !keyPath || 61 | ([keyPath isKindOfClass:[NSString class]] && keyPath.length <= 0)) { 62 | return; 63 | } 64 | 65 | @synchronized (self) { 66 | NSUInteger KVOHash = [self ysc_easy_hashOfObserver:observer keyPath:keyPath]; 67 | NSHashTable *hashTable = [self kvoHashTable]; 68 | if (!hashTable) { 69 | return; 70 | } 71 | if ([hashTable containsObject:@(KVOHash)]) { 72 | [self ysc_easy_removeObserver:observer forKeyPath:keyPath]; 73 | [hashTable removeObject:@(KVOHash)]; 74 | } 75 | } 76 | } 77 | 78 | - (NSInteger)ysc_easy_hashOfObserver:(NSObject *)observer keyPath:(NSString *)keyPath { 79 | NSArray *KVOContentArr = @[observer,keyPath]; 80 | NSInteger hash = [KVOContentArr hash]; 81 | 82 | return hash; 83 | } 84 | 85 | - (void)ysc_easy_observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 86 | 87 | @try { 88 | [self ysc_easy_observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 89 | } @catch (NSException *exception) { 90 | NSString *reason = [NSString stringWithFormat:@"non fatal Error%@",[exception description]]; 91 | NSLog(@"reson = %@",reason); 92 | } 93 | } 94 | 95 | - (void)setKvoHashTable:(NSHashTable *)kvoHashTable { 96 | objc_setAssociatedObject(self, &KVOHashTableKey, kvoHashTable, OBJC_ASSOCIATION_RETAIN); 97 | } 98 | 99 | 100 | - (NSHashTable *)kvoHashTable { 101 | return objc_getAssociatedObject(self, &KVOHashTableKey); 102 | } 103 | 104 | @end 105 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/15. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | 12 | @interface ViewController : UIViewController 13 | 14 | 15 | @end 16 | 17 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/15. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "BaseDefine.h" 11 | #import "NotificationReceiver.h" 12 | 13 | @interface ViewController () 14 | 15 | /* tableView */ 16 | @property (nonatomic, strong) UITableView *tableView; 17 | 18 | @end 19 | 20 | @implementation ViewController 21 | { 22 | NSArray *_titleArray; 23 | } 24 | 25 | - (void)viewDidLoad { 26 | [super viewDidLoad]; 27 | // Do any additional setup after loading the view. 28 | 29 | _titleArray = @[ 30 | @{ 31 | @"title" : @"Unrecognized Selector Crash", 32 | @"class" : @"TestUnrecognizedSelVC" 33 | }, 34 | @{ 35 | @"title" : @"KVO Crash", 36 | @"class" : @"TestKVOCrashVC" 37 | }, 38 | @{ 39 | @"title" : @"KVC Crash", 40 | @"class" : @"TestKVCCrashVC" 41 | }, 42 | @{ 43 | @"title" : @"Notification Crash", 44 | @"class" : @"TestNotificationCrashVC" 45 | }, 46 | @{ 47 | @"title" : @"NSTimer Crash", 48 | @"class" : @"TestTimerCrashVC" 49 | }, 50 | @{ 51 | @"title" : @"Containers Crash", 52 | @"class" : @"TestContainersVC" 53 | }, 54 | @{ 55 | @"title" : @"NSNull Crash", 56 | @"class" : @"TestNullVC" 57 | } 58 | ]; 59 | 60 | [self.view addSubview:self.tableView]; 61 | 62 | @autoreleasepool { 63 | NotificationReceiver *receiver = [[NotificationReceiver alloc] init]; 64 | [[NSNotificationCenter defaultCenter] postNotificationName:@"loadingCompelete" object:nil userInfo:@{@"page":@(1)}]; 65 | 66 | NSLog(@"%@",receiver); 67 | } 68 | 69 | } 70 | 71 | /** 72 | * tableView初始化 73 | */ 74 | - (UITableView *)tableView { 75 | if (!_tableView) { 76 | _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeigh) style:UITableViewStyleGrouped]; 77 | _tableView.delegate = self; 78 | _tableView.dataSource = self; 79 | } 80 | return _tableView; 81 | } 82 | 83 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 84 | return 1; 85 | } 86 | 87 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 88 | return _titleArray.count; 89 | } 90 | 91 | - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 92 | return 44; 93 | } 94 | 95 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 96 | static NSString *CellID = @"mainCellID"; 97 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID]; 98 | if (cell == nil) { 99 | cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID]; 100 | } 101 | cell.textLabel.text = [_titleArray[indexPath.row] objectForKey:@"title"]; 102 | return cell; 103 | } 104 | 105 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 106 | NSDictionary *item = _titleArray[indexPath.row]; 107 | Class cls = NSClassFromString([item objectForKey:@"class"]); 108 | [self presentViewController:[[cls alloc] init] animated:YES completion:nil]; 109 | } 110 | 111 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 112 | 113 | [[NSNotificationCenter defaultCenter] postNotificationName:@"loadingCompelete" object:nil userInfo:@{@"page":@(2)}]; 114 | } 115 | 116 | 117 | 118 | @end 119 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/YSCDefender/NSObject+KVCDefender.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+KVCDefender.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/27. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface NSObject (KVCDefender) 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/YSCDefender/NSObject+KVCDefender.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+KVCDefender.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/27. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "NSObject+KVCDefender.h" 10 | #import "NSObject+MethodSwizzling.h" 11 | 12 | @implementation NSObject (KVCDefender) 13 | 14 | // 不建议拦截 `setValue:forKey:` 方法,会影响系统逻辑判断 15 | + (void)load { 16 | static dispatch_once_t onceToken; 17 | dispatch_once(&onceToken, ^{ 18 | 19 | // 拦截 `setValue:forKey:` 方法,替换自定义实现 20 | [NSObject yscDefenderSwizzlingInstanceMethod:@selector(setValue:forKey:) 21 | withMethod:@selector(ysc_setValue:forKey:) 22 | withClass:[NSObject class]]; 23 | 24 | }); 25 | } 26 | 27 | - (void)ysc_setValue:(id)value forKey:(NSString *)key { 28 | if (key == nil) { 29 | NSString *crashMessages = [NSString stringWithFormat:@"*** Crash Message: [<%@ %p> setNilValueForKey]: could not set nil as the value for the key %@. ***",NSStringFromClass([self class]),self,key]; 30 | NSLog(@"%@", crashMessages); 31 | return; 32 | } 33 | 34 | [self ysc_setValue:value forKey:key]; 35 | } 36 | 37 | - (void)setNilValueForKey:(NSString *)key { 38 | NSString *crashMessages = [NSString stringWithFormat:@"*** Crash Message: [<%@ %p> setNilValueForKey]: could not set nil as the value for the key %@. ***",NSStringFromClass([self class]),self,key]; 39 | NSLog(@"%@", crashMessages); 40 | } 41 | 42 | - (void)setValue:(id)value forUndefinedKey:(NSString *)key { 43 | NSString *crashMessages = [NSString stringWithFormat:@"*** Crash Message: [<%@ %p> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key: %@,value:%@'. ***",NSStringFromClass([self class]),self,key,value]; 44 | NSLog(@"%@", crashMessages); 45 | } 46 | 47 | - (nullable id)valueForUndefinedKey:(NSString *)key { 48 | NSString *crashMessages = [NSString stringWithFormat:@"*** Crash Message: [<%@ %p> valueForUndefinedKey:]: this class is not key value coding-compliant for the key: %@. ***",NSStringFromClass([self class]),self,key]; 49 | NSLog(@"%@", crashMessages); 50 | 51 | return self; 52 | } 53 | 54 | @end 55 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/YSCDefender/NSObject+KVODefender.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+KVODefender.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/19. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | 12 | NS_ASSUME_NONNULL_BEGIN 13 | 14 | @interface NSObject (KVODefender) 15 | 16 | @end 17 | 18 | 19 | NS_ASSUME_NONNULL_END 20 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/YSCDefender/NSObject+KVODefender.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+KVODefender.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/19. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "NSObject+KVODefender.h" 10 | #import "NSObject+MethodSwizzling.h" 11 | #import 12 | 13 | 14 | #pragma mark - YSCKVOProxy 相关 15 | 16 | @interface YSCKVOProxy : NSObject 17 | 18 | - (NSArray *)getAllKeyPaths; 19 | 20 | @end 21 | 22 | @implementation YSCKVOProxy 23 | { 24 | // 关系数据表结构:{keypath : [observer1, observer2 , ...](NSHashTable)} 25 | @private 26 | NSMutableDictionary *> *_kvoInfoMap; 27 | } 28 | 29 | - (instancetype)init { 30 | self = [super init]; 31 | if (self) { 32 | _kvoInfoMap = [NSMutableDictionary dictionary]; 33 | } 34 | return self; 35 | } 36 | 37 | // 添加 KVO 信息操作, 添加成功返回 YES 38 | - (BOOL)addInfoToMapWithObserver:(NSObject *)observer 39 | forKeyPath:(NSString *)keyPath 40 | options:(NSKeyValueObservingOptions)options 41 | context:(void *)context { 42 | 43 | @synchronized (self) { 44 | if (!observer || !keyPath || 45 | ([keyPath isKindOfClass:[NSString class]] && keyPath.length <= 0)) { 46 | return NO; 47 | } 48 | 49 | NSHashTable *info = _kvoInfoMap[keyPath]; 50 | if (info.count == 0) { 51 | info = [[NSHashTable alloc] initWithOptions:(NSPointerFunctionsWeakMemory) capacity:0]; 52 | [info addObject:observer]; 53 | 54 | _kvoInfoMap[keyPath] = info; 55 | 56 | return YES; 57 | } 58 | 59 | if (![info containsObject:observer]) { 60 | [info addObject:observer]; 61 | } 62 | 63 | return NO; 64 | } 65 | } 66 | 67 | // 移除 KVO 信息操作, 添加成功返回 YES 68 | - (BOOL)removeInfoInMapWithObserver:(NSObject *)observer 69 | forKeyPath:(NSString *)keyPath { 70 | 71 | @synchronized (self) { 72 | if (!observer || !keyPath || 73 | ([keyPath isKindOfClass:[NSString class]] && keyPath.length <= 0)) { 74 | return NO; 75 | } 76 | 77 | NSHashTable *info = _kvoInfoMap[keyPath]; 78 | 79 | if (info.count == 0) { 80 | return NO; 81 | } 82 | 83 | [info removeObject:observer]; 84 | 85 | if (info.count == 0) { 86 | [_kvoInfoMap removeObjectForKey:keyPath]; 87 | 88 | return YES; 89 | } 90 | 91 | return NO; 92 | } 93 | } 94 | 95 | // 添加 KVO 信息操作, 添加成功返回 YES 96 | - (BOOL)removeInfoInMapWithObserver:(NSObject *)observer 97 | forKeyPath:(NSString *)keyPath 98 | context:(void *)context { 99 | @synchronized (self) { 100 | if (!observer || !keyPath || 101 | ([keyPath isKindOfClass:[NSString class]] && keyPath.length <= 0)) { 102 | return NO; 103 | } 104 | 105 | NSHashTable *info = _kvoInfoMap[keyPath]; 106 | 107 | if (info.count == 0) { 108 | return NO; 109 | } 110 | 111 | [info removeObject:observer]; 112 | 113 | if (info.count == 0) { 114 | [_kvoInfoMap removeObjectForKey:keyPath]; 115 | 116 | return YES; 117 | } 118 | 119 | return NO; 120 | } 121 | } 122 | 123 | // 实际观察者 yscKVOProxy 进行监听,并分发 124 | - (void)observeValueForKeyPath:(NSString *)keyPath 125 | ofObject:(id)object 126 | change:(NSDictionary *)change 127 | context:(void *)context { 128 | 129 | NSHashTable *info = _kvoInfoMap[keyPath]; 130 | 131 | for (NSObject *observer in info) { 132 | @try { 133 | [observer observeValueForKeyPath:keyPath ofObject:object change:change context:context]; 134 | } @catch (NSException *exception) { 135 | NSString *reason = [NSString stringWithFormat:@"KVO Warning : %@",[exception description]]; 136 | NSLog(@"%@",reason); 137 | } 138 | } 139 | } 140 | 141 | // 获取所有被观察的 keypaths 142 | - (NSArray *)getAllKeyPaths { 143 | NSArray *keyPaths = _kvoInfoMap.allKeys; 144 | return keyPaths; 145 | } 146 | 147 | @end 148 | 149 | 150 | #pragma mark - NSObject+KVODefender 分类 151 | 152 | @implementation NSObject (KVODefender) 153 | 154 | + (void)load { 155 | static dispatch_once_t onceToken; 156 | dispatch_once(&onceToken, ^{ 157 | 158 | // 拦截 `addObserver:forKeyPath:options:context:` 方法,替换自定义实现 159 | [NSObject yscDefenderSwizzlingInstanceMethod: @selector(addObserver:forKeyPath:options:context:) 160 | withMethod: @selector(ysc_addObserver:forKeyPath:options:context:) 161 | withClass: [NSObject class]]; 162 | 163 | // 拦截 `removeObserver:forKeyPath:` 方法,替换自定义实现 164 | [NSObject yscDefenderSwizzlingInstanceMethod: @selector(removeObserver:forKeyPath:) 165 | withMethod: @selector(ysc_removeObserver:forKeyPath:) 166 | withClass: [NSObject class]]; 167 | 168 | // 拦截 `removeObserver:forKeyPath:context:` 方法,替换自定义实现 169 | [NSObject yscDefenderSwizzlingInstanceMethod: @selector(removeObserver:forKeyPath:context:) 170 | withMethod: @selector(ysc_removeObserver:forKeyPath:context:) 171 | withClass: [NSObject class]]; 172 | 173 | // 拦截 `dealloc` 方法,替换自定义实现 174 | [NSObject yscDefenderSwizzlingInstanceMethod: NSSelectorFromString(@"dealloc") 175 | withMethod: @selector(ysc_kvodealloc) 176 | withClass: [NSObject class]]; 177 | }); 178 | } 179 | 180 | static void *YSCKVOProxyKey = &YSCKVOProxyKey; 181 | static NSString *const KVODefenderValue = @"YSC_KVODefender"; 182 | static void *KVODefenderKey = &KVODefenderKey; 183 | 184 | // YSCKVOProxy setter 方法 185 | - (void)setYscKVOProxy:(YSCKVOProxy *)yscKVOProxy { 186 | objc_setAssociatedObject(self, YSCKVOProxyKey, yscKVOProxy, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 187 | } 188 | 189 | // YSCKVOProxy getter 方法 190 | - (YSCKVOProxy *)yscKVOProxy { 191 | id yscKVOProxy = objc_getAssociatedObject(self, YSCKVOProxyKey); 192 | if (yscKVOProxy == nil) { 193 | yscKVOProxy = [[YSCKVOProxy alloc] init]; 194 | self.yscKVOProxy = yscKVOProxy; 195 | } 196 | return yscKVOProxy; 197 | } 198 | 199 | // 自定义 addObserver:forKeyPath:options:context: 实现方法 200 | - (void)ysc_addObserver:(NSObject *)observer 201 | forKeyPath:(NSString *)keyPath 202 | options:(NSKeyValueObservingOptions)options 203 | context:(void *)context { 204 | 205 | if (!isSystemClass(self.class)) { 206 | objc_setAssociatedObject(self, KVODefenderKey, KVODefenderValue, OBJC_ASSOCIATION_RETAIN); 207 | if ([self.yscKVOProxy addInfoToMapWithObserver:observer forKeyPath:keyPath options:options context:context]) { 208 | // 如果添加 KVO 信息操作成功,则调用系统添加方法 209 | [self ysc_addObserver:self.yscKVOProxy forKeyPath:keyPath options:options context:context]; 210 | } else { 211 | // 添加 KVO 信息操作失败:重复添加 212 | NSString *className = (NSStringFromClass(self.class) == nil) ? @"" : NSStringFromClass(self.class); 213 | NSString *reason = [NSString stringWithFormat:@"KVO Warning : Repeated additions to the observer:%@ for the key path:'%@' from %@", 214 | observer, keyPath, className]; 215 | NSLog(@"%@",reason); 216 | } 217 | } else { 218 | [self ysc_addObserver:observer forKeyPath:keyPath options:options context:context]; 219 | } 220 | } 221 | 222 | // 自定义 removeObserver:forKeyPath:context: 实现方法 223 | - (void)ysc_removeObserver:(NSObject *)observer 224 | forKeyPath:(NSString *)keyPath 225 | context:(void *)context { 226 | 227 | if (!isSystemClass(self.class)) { 228 | if ([self.yscKVOProxy removeInfoInMapWithObserver:observer forKeyPath:keyPath context:context]) { 229 | // 如果移除 KVO 信息操作成功,则调用系统移除方法 230 | [self ysc_removeObserver:self.yscKVOProxy forKeyPath:keyPath context:context]; 231 | } else { 232 | // 移除 KVO 信息操作失败:移除了未注册的观察者 233 | NSString *className = NSStringFromClass(self.class) == nil ? @"" : NSStringFromClass(self.class); 234 | NSString *reason = [NSString stringWithFormat:@"*** Crash Message: Cannot remove an observer %@ for the key path '%@' from %@ , because it is not registered as an observer ***", observer, keyPath, className]; 235 | NSLog(@"%@",reason); 236 | } 237 | } else { 238 | [self ysc_removeObserver:observer forKeyPath:keyPath context:context]; 239 | } 240 | } 241 | 242 | // 自定义 removeObserver:forKeyPath: 实现方法 243 | - (void)ysc_removeObserver:(NSObject *)observer 244 | forKeyPath:(NSString *)keyPath { 245 | 246 | if (!isSystemClass(self.class)) { 247 | if ([self.yscKVOProxy removeInfoInMapWithObserver:observer forKeyPath:keyPath]) { 248 | // 如果移除 KVO 信息操作成功,则调用系统移除方法 249 | [self ysc_removeObserver:self.yscKVOProxy forKeyPath:keyPath]; 250 | } else { 251 | // 移除 KVO 信息操作失败:移除了未注册的观察者 252 | NSString *className = NSStringFromClass(self.class) == nil ? @"" : NSStringFromClass(self.class); 253 | NSString *reason = [NSString stringWithFormat:@"*** Crash Message: Cannot remove an observer %@ for the key path '%@' from %@ , because it is not registered as an observer ***", observer, keyPath, className]; 254 | NSLog(@"%@",reason); 255 | } 256 | } else { 257 | [self ysc_removeObserver:observer forKeyPath:keyPath]; 258 | } 259 | 260 | } 261 | 262 | // 自定义 dealloc 实现方法 263 | - (void)ysc_kvodealloc { 264 | @autoreleasepool { 265 | if (!isSystemClass(self.class)) { 266 | NSString *value = (NSString *)objc_getAssociatedObject(self, KVODefenderKey); 267 | if ([value isEqualToString:KVODefenderValue]) { 268 | NSArray *keyPaths = [self.yscKVOProxy getAllKeyPaths]; 269 | // 被观察者在 dealloc 时仍然注册着 KVO 270 | if (keyPaths.count > 0) { 271 | NSString *reason = [NSString stringWithFormat:@"*** Crash Message: An instance %@ was deallocated while key value observers were still registered with it. The Keypaths is:'%@' ***", self, [keyPaths componentsJoinedByString:@","]]; 272 | NSLog(@"%@",reason); 273 | } 274 | 275 | // 移除多余的观察者 276 | for (NSString *keyPath in keyPaths) { 277 | [self ysc_removeObserver:self.yscKVOProxy forKeyPath:keyPath]; 278 | } 279 | } 280 | } 281 | } 282 | 283 | [self ysc_kvodealloc]; 284 | } 285 | 286 | @end 287 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/YSCDefender/NSObject+MethodSwizzling.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MethodSwizzling.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/19. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface NSObject (MethodSwizzling) 14 | 15 | /** 交换两个类方法的实现 16 | * @param originalSelector 原始方法的 SEL 17 | * @param swizzledSelector 交换方法的 SEL 18 | * @param targetClass 类 19 | */ 20 | + (void)yscDefenderSwizzlingClassMethod:(SEL)originalSelector withMethod:(SEL)swizzledSelector withClass:(Class)targetClass; 21 | 22 | /** 交换两个对象方法的实现 23 | * @param originalSelector 原始方法的 SEL 24 | * @param swizzledSelector 交换方法的 SEL 25 | * @param targetClass 类 26 | */ 27 | + (void)yscDefenderSwizzlingInstanceMethod:(SEL)originalSelector withMethod:(SEL)swizzledSelector withClass:(Class)targetClass; 28 | 29 | @end 30 | 31 | // 判断是否是系统类 32 | static inline BOOL isSystemClass(Class cls) { 33 | BOOL isSystem = NO; 34 | NSString *className = NSStringFromClass(cls); 35 | if ([className hasPrefix:@"NS"] || [className hasPrefix:@"__NS"] || [className hasPrefix:@"OS_xpc"]) { 36 | isSystem = YES; 37 | return isSystem; 38 | } 39 | NSBundle *mainBundle = [NSBundle bundleForClass:cls]; 40 | if (mainBundle == [NSBundle mainBundle]) { 41 | isSystem = NO; 42 | }else{ 43 | isSystem = YES; 44 | } 45 | return isSystem; 46 | } 47 | NS_ASSUME_NONNULL_END 48 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/YSCDefender/NSObject+MethodSwizzling.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+MethodSwizzling.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/19. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "NSObject+MethodSwizzling.h" 10 | #import 11 | 12 | @implementation NSObject (MethodSwizzling) 13 | 14 | + (void)yscDefenderSwizzlingClassMethod:(SEL)originalSelector withMethod:(SEL)swizzledSelector withClass:(Class)targetClass { 15 | swizzlingClassMethod(targetClass, originalSelector, swizzledSelector); 16 | } 17 | 18 | + (void)yscDefenderSwizzlingInstanceMethod:(SEL)originalSelector withMethod:(SEL)swizzledSelector withClass:(Class)targetClass { 19 | swizzlingInstanceMethod(targetClass, originalSelector, swizzledSelector); 20 | } 21 | 22 | // 交换两个类方法的实现 23 | void swizzlingClassMethod(Class class, SEL originalSelector, SEL swizzledSelector) { 24 | 25 | Method originalMethod = class_getClassMethod(class, originalSelector); 26 | Method swizzledMethod = class_getClassMethod(class, swizzledSelector); 27 | 28 | BOOL didAddMethod = class_addMethod(class, 29 | originalSelector, 30 | method_getImplementation(swizzledMethod), 31 | method_getTypeEncoding(swizzledMethod)); 32 | 33 | if (didAddMethod) { 34 | class_replaceMethod(class, 35 | swizzledSelector, 36 | method_getImplementation(originalMethod), 37 | method_getTypeEncoding(originalMethod)); 38 | } else { 39 | method_exchangeImplementations(originalMethod, swizzledMethod); 40 | } 41 | } 42 | 43 | // 交换两个对象方法的实现 44 | void swizzlingInstanceMethod(Class class, SEL originalSelector, SEL swizzledSelector) { 45 | Method originalMethod = class_getInstanceMethod(class, originalSelector); 46 | Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector); 47 | 48 | BOOL didAddMethod = class_addMethod(class, 49 | originalSelector, 50 | method_getImplementation(swizzledMethod), 51 | method_getTypeEncoding(swizzledMethod)); 52 | 53 | if (didAddMethod) { 54 | class_replaceMethod(class, 55 | swizzledSelector, 56 | method_getImplementation(originalMethod), 57 | method_getTypeEncoding(originalMethod)); 58 | } else { 59 | method_exchangeImplementations(originalMethod, swizzledMethod); 60 | } 61 | } 62 | 63 | 64 | @end 65 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/YSCDefender/NSObject+SelectorDefender.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+SelectorDefender.h 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/19. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | NS_ASSUME_NONNULL_BEGIN 12 | 13 | @interface NSObject (SelectorDefender) 14 | 15 | @end 16 | 17 | NS_ASSUME_NONNULL_END 18 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/YSCDefender/NSObject+SelectorDefender.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+SelectorDefender.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/19. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import "NSObject+SelectorDefender.h" 10 | #import "NSObject+MethodSwizzling.h" 11 | #import 12 | 13 | @implementation NSObject (SelectorDefender) 14 | 15 | + (void)load { 16 | static dispatch_once_t onceToken; 17 | dispatch_once(&onceToken, ^{ 18 | 19 | // 拦截 `+forwardingTargetForSelector:` 方法,替换自定义实现 20 | [NSObject yscDefenderSwizzlingClassMethod:@selector(forwardingTargetForSelector:) 21 | withMethod:@selector(ysc_forwardingTargetForSelector:) 22 | withClass:[NSObject class]]; 23 | 24 | // 拦截 `-forwardingTargetForSelector:` 方法,替换自定义实现 25 | [NSObject yscDefenderSwizzlingInstanceMethod:@selector(forwardingTargetForSelector:) 26 | withMethod:@selector(ysc_forwardingTargetForSelector:) 27 | withClass:[NSObject class]]; 28 | 29 | }); 30 | } 31 | 32 | // 自定义实现 `+ysc_forwardingTargetForSelector:` 方法 33 | + (id)ysc_forwardingTargetForSelector:(SEL)aSelector { 34 | SEL forwarding_sel = @selector(forwardingTargetForSelector:); 35 | 36 | // 获取 NSObject 的消息转发方法 37 | Method root_forwarding_method = class_getClassMethod([NSObject class], forwarding_sel); 38 | // 获取 当前类 的消息转发方法 39 | Method current_forwarding_method = class_getClassMethod([self class], forwarding_sel); 40 | 41 | // 判断当前类本身是否实现第二步:消息接受者重定向 42 | BOOL realize = method_getImplementation(current_forwarding_method) != method_getImplementation(root_forwarding_method); 43 | 44 | // 如果没有实现第二步:消息接受者重定向 45 | if (!realize) { 46 | // 判断有没有实现第三步:消息重定向 47 | SEL methodSignature_sel = @selector(methodSignatureForSelector:); 48 | Method root_methodSignature_method = class_getClassMethod([NSObject class], methodSignature_sel); 49 | 50 | Method current_methodSignature_method = class_getClassMethod([self class], methodSignature_sel); 51 | realize = method_getImplementation(current_methodSignature_method) != method_getImplementation(root_methodSignature_method); 52 | 53 | // 如果没有实现第三步:消息重定向 54 | if (!realize) { 55 | // 创建一个新类 56 | NSString *errClassName = NSStringFromClass([self class]); 57 | NSString *errSel = NSStringFromSelector(aSelector); 58 | 59 | NSLog(@"*** Crash Message: +[%@ %@]: unrecognized selector sent to class %p ***",errClassName, errSel, self); 60 | 61 | 62 | NSString *className = @"CrachClass"; 63 | Class cls = NSClassFromString(className); 64 | 65 | // 如果类不存在 动态创建一个类 66 | if (!cls) { 67 | Class superClsss = [NSObject class]; 68 | cls = objc_allocateClassPair(superClsss, className.UTF8String, 0); 69 | // 注册类 70 | objc_registerClassPair(cls); 71 | } 72 | // 如果类没有对应的方法,则动态添加一个 73 | if (!class_getInstanceMethod(NSClassFromString(className), aSelector)) { 74 | class_addMethod(cls, aSelector, (IMP)Crash, "@@:@"); 75 | } 76 | // 把消息转发到当前动态生成类的实例对象上 77 | return [[cls alloc] init]; 78 | } 79 | } 80 | return [self ysc_forwardingTargetForSelector:aSelector]; 81 | } 82 | 83 | // 自定义实现 `-ysc_forwardingTargetForSelector:` 方法 84 | - (id)ysc_forwardingTargetForSelector:(SEL)aSelector { 85 | 86 | SEL forwarding_sel = @selector(forwardingTargetForSelector:); 87 | 88 | // 获取 NSObject 的消息转发方法 89 | Method root_forwarding_method = class_getInstanceMethod([NSObject class], forwarding_sel); 90 | // 获取 当前类 的消息转发方法 91 | Method current_forwarding_method = class_getInstanceMethod([self class], forwarding_sel); 92 | 93 | // 判断当前类本身是否实现第二步:消息接受者重定向 94 | BOOL realize = method_getImplementation(current_forwarding_method) != method_getImplementation(root_forwarding_method); 95 | 96 | // 如果没有实现第二步:消息接受者重定向 97 | if (!realize) { 98 | // 判断有没有实现第三步:消息重定向 99 | SEL methodSignature_sel = @selector(methodSignatureForSelector:); 100 | Method root_methodSignature_method = class_getInstanceMethod([NSObject class], methodSignature_sel); 101 | 102 | Method current_methodSignature_method = class_getInstanceMethod([self class], methodSignature_sel); 103 | realize = method_getImplementation(current_methodSignature_method) != method_getImplementation(root_methodSignature_method); 104 | 105 | // 如果没有实现第三步:消息重定向 106 | if (!realize) { 107 | // 创建一个新类 108 | NSString *errClassName = NSStringFromClass([self class]); 109 | NSString *errSel = NSStringFromSelector(aSelector); 110 | 111 | NSLog(@"*** Crash Message: -[%@ %@]: unrecognized selector sent to instance %p ***",errClassName, errSel, self); 112 | 113 | NSString *className = @"CrachClass"; 114 | Class cls = NSClassFromString(className); 115 | 116 | // 如果类不存在 动态创建一个类 117 | if (!cls) { 118 | Class superClsss = [NSObject class]; 119 | cls = objc_allocateClassPair(superClsss, className.UTF8String, 0); 120 | // 注册类 121 | objc_registerClassPair(cls); 122 | } 123 | // 如果类没有对应的方法,则动态添加一个 124 | if (!class_getInstanceMethod(NSClassFromString(className), aSelector)) { 125 | class_addMethod(cls, aSelector, (IMP)Crash, "@@:@"); 126 | } 127 | // 把消息转发到当前动态生成类的实例对象上 128 | return [[cls alloc] init]; 129 | } 130 | } 131 | return [self ysc_forwardingTargetForSelector:aSelector]; 132 | } 133 | 134 | // 动态添加的方法实现 135 | static int Crash(id slf, SEL selector) { 136 | return 0; 137 | } 138 | 139 | @end 140 | -------------------------------------------------------------------------------- /YSC-Avoid-Crash/YSC-Avoid-Crash/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // YSC-Avoid-Crash 4 | // 5 | // Created by WalkingBoy on 2019/8/15. 6 | // Copyright © 2019 bujige. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------