iOS9的那些坑 — — WeiboSDK registerApp启动就崩溃

在升级Xcode7.2.1后,在App加载时直接崩掉,仔细看了,发现是在在注册微博SDK的时候报错:

  [WeiboSDK registerApp:WBAPPKey]; 

 

查了很多资料,最后在github的微博SDk issue讨论区中发现有人已经解决了。

 

解决方法:

在注册 微博 sdk 之前

1 [NSDictionary aspect_hookSelector:NSSelectorFromString(@"weibosdk_WBSDKJSONString")
2                           withOptions:AspectPositionInstead
3                            usingBlock:^(id<AspectInfo> info)
4      {
5          return [(NSDictionary*)[info instance] tt_jsonString];
6      }
7                                 error:nil];
tt_jsonString 是自一个字典转字符串的 category;

 aspect 可以 pod 安装;

有人解释说,猜测weibosdk使用了旧版本的jsonkit.导致了json的解析兼容性出了问题直接升级微博SDK也可以。

这里提供一个字典转字符串的方法吧:

1 /** 字典转字符串 */
2 
3 - (NSString*)dictionaryToJson:(NSDictionary *)dic{
4 
5     NSError *parseError = nil;
6 
7     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
8 
9 }

Aspects的github链接:https://github.com/steipete/Aspects

参考链接: https://github.com/sinaweibosdk/weibo_ios_sdk/issues/133

原文地址:https://www.cnblogs.com/A--G/p/5292919.html