我们的项目中勾了很多ObjectC的私有API,导致有不少crash。想问一下如何防止这种问题? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
feihu
V2EX    iDev

我们的项目中勾了很多ObjectC的私有API,导致有不少crash。想问一下如何防止这种问题?

  •  
  •   feihu 2013-05-20 17:09:28 +08:00 5283 次点击
    这是一个创建于 4534 天前的主题,其中的信息可能已经有所发展或是发生改变。
    具体的做法是:会先把objectc的API勾出来,调用我们的写的函数,然后在去调用原来的API。
    第 1 条附言    2013-05-20 17:52:02 +08:00
    抱歉,没描述清楚,如@thedevil5032 所说的:hook的方式来做。当系统调用一个函数的时候会先经过楼主他们写的,再由楼主他们写的代码去调用原生的。
    16 条回复    1970-01-01 08:00:00 +08:00
    fly2never
        1
    fly2never  
       2013-05-20 17:23:19 +08:00
    什么叫勾起来?
    offspring
        2
    offspring  
       2013-05-20 17:25:57 +08:00
    表示看不懂...
    coupgar
        3
    coupgar  
       2013-05-20 17:27:10 +08:00
    @fly2never method swizzle 这样的方式吧
    thedevil5032
        4
    thedevil5032  
       2013-05-20 17:31:17 +08:00
    @fly2never
    @offspring
    猜猜应该是 hook 的意思?

    我猜楼主是不是这个意思:
    当系统调用一个函数的时候会先经过楼主他们写的,再由楼主他们写的代码去调用原生的?
    alexrezit
        5
    alexrezit  
       2013-05-20 17:34:41 +08:00
    楼主是这个意思么?
    if ([oo respondsToSelector:@selector(xx:)] { ... }
    anerevol
        6
    anerevol  
       2013-05-21 09:25:39 +08:00
    楼主是想问为什么会crash? 你要是用的mobileSubstrate hook的话,如果该类没有这个方法也不会crash的, 只是不会调用而已。 觉得crash跟hook本身没关系的,是其他地方的问题吧
    alexrezit
        7
    alexrezit  
       2013-05-21 09:52:33 +08:00
    @coupgar
    你确定这样能上架?
    coupgar
        8
    coupgar  
       2013-05-21 10:51:55 +08:00
    @alexrezit 显然不能。
    alexrezit
        9
    alexrezit  
       2013-05-21 12:46:33 +08:00
    @coupgar
    =.= 还以为这样就能呢... 我用 API 都是先判断一下是否 responds, 这样就能在上架应用中调用私有 API 了.
    method swizzle 有什么实际应用么?
    kshatriya
        10
    kshatriya  
       2013-05-21 17:27:38 +08:00
    method swizzle是方法混合器, 通过SEL查IMP, 然后把IMP搅在一起
    kshatriya
        11
    kshatriya  
       2013-05-21 17:27:56 +08:00
    void Swizzle(Class c, SEL orig, SEL new)
    {
    Method origMethod = class_getInstanceMethod(c, orig);
    Method newMethod = class_getInstanceMethod(c, new);
    if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
    class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    else
    method_exchangeImplementations(origMethod, newMethod);
    }
    kshatriya
        12
    kshatriya  
       2013-05-21 17:28:13 +08:00
    强烈要求v2ex可以有代码格式化+高亮语法
    loading
        13
    loading  
       2013-05-21 17:31:30 +08:00
    挂钩都是作恶的开始吧…
    kshatriya
        14
    kshatriya  
       2013-05-21 17:32:34 +08:00
    crash普通情况下都是发送了空消息导致, 发之前做检查吧

    hook一般都是先把orig函数的指针存一份, 然后覆盖, 在返回的时候返回给之前保存的orig函数指针, 只处理入参!别做多余的操作!

    好混乱, 语死早啊
    wezzard
        15
    wezzard  
       2013-05-21 21:45:03 +08:00
    method swizzling 不能上架?
    coupgar
        16
    coupgar  
       2013-05-21 23:29:22 +08:00
    @wezzard 没有替换私有方法是可以上架的。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2568 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 02:30 PVG 10:30 LAX 19:30 JFK 22:30
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86