1 Star 0 Fork 0

raychow-github/JQSafeKit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

JQSafeKit

CircleCI Version Coveralls branch Packagist Platform Apps Using CocoaPods

前言

一个已经发布到AppStore上的App,最忌讳的就是崩溃问题。为什么在开发阶段或者测试阶段都不会崩溃,而发布到AppStore上就崩溃了呢?究其根源,最主要的原因就是数据的错乱。特别是 服务器返回数据的错乱,将严重影响到我们的App。


Foundation框架存在许多潜在崩溃的危险

  • 将 nil 插入可变数组中会导致崩溃。
  • 数组越界会导致崩溃。
  • 根据key给字典某个元素重新赋值时,若key为 nil 会导致崩溃。
  • ......

JQSafeKit简介

  • 这个框架利用runtime技术对一些常用并且容易导致崩溃的方法进行处理,可以有效的防止崩溃。
  • 并且打印出具体是哪一行代码会导致崩溃,让你快速定位导致崩溃的代码。
  • 你可以获取到原本导致崩溃的主要信息<由于这个框架的存在,并不会崩溃>,进行相应的处理。比如:
  • 你可以将这些崩溃信息发送到自己服务器。
  • 你若集成了第三方崩溃日志收集的SDK,比如你用了腾讯的Bugly,你可以上报自定义异常。

下面先来看下防止崩溃的效果吧

可导致崩溃的代码

NSString *nilStr = nil;
    NSArray *array = @[@"HaRi", nilStr];
  • 若没有JQSafeKit来防止崩溃,则会直接崩溃,如下图

崩溃截图.png

  • 若有JQSafeKit来防止崩溃,则不会崩溃,并且会将原本会崩溃情况的详细信息打印出来,如下图

防止崩溃输出日志.png


Installation【安装】

From CocoaPods【使用CocoaPods】

pod  “JQSafeKit”

Manually【手动导入】

  • Drag all source files under floder JQSafeKit to your project.【将JQSafeKit文件夹中的所有源代码拽入项目中】

##使用方法

  • 在AppDelegate的didFinishLaunchingWithOptions方法中添加如下代码,让JQSafeKit生效
//这句代码会让JQSafeKit生效,若没有如下代码,则JQSafeKit就不起作用
[JQSafeKit becomeEffective];
  • 若你想要获取崩溃日志的所有详细信息,只需添加通知的监听,监听的通知名为:JQSafeKitNotification
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [JQSafeKit becomeEffective];
    
    //监听通知:JQSafeKitNotification, 获取JQSafeKit捕获的崩溃日志的详细信息
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealwithCrashMessage:) name:JQSafeKitNotification object:nil];
    return YES;
}

- (void)dealwithCrashMessage:(NSNotification *)note {
//注意:所有的信息都在userInfo中
//你可以在这里收集相应的崩溃信息进行相应的处理(比如传到自己服务器)
    NSLog(@"%@",note.userInfo);
}
  • 下面通过打断点的形式来看下userInfo中的信息结构,看下包含了哪些信息

userInfo信息结构.png

  • 再看下控制台输出日志来看下userInfo中的包含了哪些信息

userInfo详细信息


目前可以防止崩溃的方法有


  • NSArray

  • 1. NSArray的快速创建方式 NSArray *array = @[@"HaRi", @"JQSafeKit"]; //这种创建方式其实调用的是2中的方法

  • 2. +(instancetype)arrayWithObjects:(const id  _Nonnull __unsafe_unretained *)objects count:(NSUInteger)cnt

  • 3. - (id)objectAtIndex:(NSUInteger)index


  • NSMutableArray
  • 1. - (id)objectAtIndex:(NSUInteger)index
  • 2. - (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)idx
  • 3. - (void)removeObjectAtIndex:(NSUInteger)index
  • 4. - (void)insertObject:(id)anObject atIndex:(NSUInteger)index

  • NSDictionary
  • 1. NSDictionary的快速创建方式 NSDictionary *dict = @{@"frameWork" : @"JQSafeKit"}; //这种创建方式其实调用的是2中的方法
  • 2. +(instancetype)dictionaryWithObjects:(const id _Nonnull __unsafe_unretained *)objects forKeys:(const id<NSCopying> _Nonnull __unsafe_unretained *)keys count:(NSUInteger)cnt

  • NSMutableDictionary
  • 1. - (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey
  • 2. - (void)removeObjectForKey:(id)aKey

  • NSString
  • 1. - (unichar)characterAtIndex:(NSUInteger)index
  • 2. - (NSString *)substringFromIndex:(NSUInteger)from
  • 3. - (NSString *)substringToIndex:(NSUInteger)to {
  • 4. - (NSString *)substringWithRange:(NSRange)range {
  • 5. - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
  • 6. - (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange
  • 7. - (NSString *)stringByReplacingCharactersInRange:(NSRange)range withString:(NSString *)replacement

  • NSMutableString
  • 1. 由于NSMutableString是继承于NSString,所以这里和NSString有些同样的方法就不重复写了
  • 2. - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString
  • 3. - (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc
  • 4. - (void)deleteCharactersInRange:(NSRange)range

  • NSAttributedString
  • 1.- (instancetype)initWithString:(NSString *)str
  • 2.- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr
  • 3.- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs

  • NSMutableAttributedString
  • 1.- (instancetype)initWithString:(NSString *)strutableString是继承于NSString,所以这里和NSString有些同样的方法就不重复写了
  • 2.- (instancetype)initWithString:(NSString *)str attributes:(NSDictionary<NSString *,id> *)attrs

  • KVC
  • 1.- (void)setValue:(id)value forKey:(NSString *)key
  • 2.- (void)setValue:(id)value forKeyPath:(NSString *)keyPath
  • 3.- (void)setValue:(id)value forUndefinedKey:(NSString *)key //这个方法一般用来重写,不会主动调用
  • 4.- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *,id> *)keyedValue

##维护成员:小韩哥 小磊

##手机加iOS开发者交流QQ群:446310206

MIT License Copyright (c) 2016 HaRi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
README
MIT
取消

发行版

暂无发行版

贡献者 (3)

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/raychow-github/JQSafeKit.git
git@gitee.com:raychow-github/JQSafeKit.git
raychow-github
JQSafeKit
JQSafeKit
master

搜索帮助