只需 2 步,定位集成融云时的崩溃与错误 - iOS 篇

只需 2 步,定位集成融云时的崩溃与错误 - iOS 篇

在集成融云 iOS SDK 时候,多多少少都会遇到一些问题,可能是崩溃,也可能是功能接口错误回调,对于刚接触 SDK 的开发者,难免束手无策,下面分享一个快速定位问题的方法,只需 2 步哟~

#####方法的中心思想就是“快速拿到 log,通过分析 log 定位问题“

#####步骤 1:

​ 添加下面代码,将 log 写入沙盒

- (BOOL)application:(UIApplication *)application

    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

        //重定向 log 到本地问题

        //在 info.plist 中打开 Application supports iTunes file sharing

      if (![[[UIDevice currentDevice] model] isEqualToString:@"iPhone Simulator"]) {

          [self redirectNSlogToDocumentFolder];

      }

    //设置Log级别,开发阶段打印详细log

    [RCIMClient sharedRCIMClient].logLevel = RC_Log_Level_Info;

}

- (void)redirectNSlogToDocumentFolder {

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);

  NSString *documentDirectory = [paths objectAtIndex:0];

  NSDate *currentDate = [NSDate date];

  NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];

  [dateformatter setDateFormat:@"MMddHHmmss"];

  NSString *formattedDate = [dateformatter stringFromDate:currentDate];

  NSString *fileName = [NSString stringWithFormat:@"rc%@.log", formattedDate];

  NSString *logFilePath = [documentDirectory stringByAppendingPathComponent:fileName];

  freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stdout);

  freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);

}

#####步骤 2:

​ 加入第 1 步代码后运行项目,复现问题后,从”沙盒/Documents“路径下导出 rc 开头的 log 文件,使用文本编辑工具打开该文件。

  • 如果是崩溃,在 log 中能找到对应的崩溃信息,例如:

    • 找不到方法 unrecognized selector sent to instance
    • 数组越界 [__NSArrayM objectAtIndex:]: index4beyond bounds [0..1] 等等。
  • 如果是 SDK 功能接口错误,可以在 log 中搜索到错误码,融云的错误码基本都是五位数字,以 ”3“开头,例如:

    • 31004:“Token 无效”,是使用的 token 和 appkey 不匹配。
    • 33001:“SDK 没有初始化”,是需要先初始化 SDK 才能调用其他接口。
    • 33003:“开发者接口调用时传入的参数错误”,是调用接口时候传入的参数有误,有空对象的可能性比较大。

    更多错误码链接:https://docs.rongcloud.cn/v4/views/im/ui/code/ios.html

总结,通过上面简单的 2 个步骤,就可以定位大部分问题,只是错误码解释如果不够明确的话,就还得提工单问,但是也能节省一部分交流成本,毕竟如果创建工单就把 log 贴身,得到针对性回复去解决问题的效率也会高很多。希望今天的分享可以帮助到大家。

猜你喜欢

转载自blog.51cto.com/15056506/2678297