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 · 4756 次点击
    这是一个创建于 3991 天前的主题,其中的信息可能已经有所发展或是发生改变。
    具体的做法是:会先把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   ·   我们的愿景   ·   实用小工具   ·   984 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 20:48 · PVG 04:48 · LAX 13:48 · JFK 16:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.