不修改projects的情况下使用reveal

reveal是一款可以查看iOS app各个元素层次结构的工具,以前的查看方法,需要将reveal的framework嵌入到iOS工程里面,如果工程代码发生变更,需要先删掉导入的framework,将工程代码pull到本地,再将reveal framework导入,非常麻烦。

最近reveal对此种模式进行了改变,改进后的方法如下:

So you use Reveal and you love it. But you’ve just fired up a new app in Xcode’s debugger and you want to have a quick look at the internals of the app’s view hierarchy, but you haven’t integrated Reveal via the static or dynamic library yet. What to do?

No problem! LLDB to the rescue.

LLDB can execute arbitrary code inside your running application. So you can just load Reveal into any iOS process you are currently debugging.

Just hit ^⌘Y to pause the iOS app in the Xcode debugger and then at the (lldb) prompt type:

LLDB Commands
1
2
3
expr (void*)dlopen("/Applications/Reveal.app/Contents/SharedSupport/iOS-Libraries/libReveal.dylib", 0x2);
expr [(NSNotificationCenter*)[NSNotificationCenter defaultCenter] postNotificationName:@"IBARevealRequestStart" object:nil];
continue

Simple, right?

OK, so maybe not so simple and easy to remember. Wouldn’t it be better if we could just type reveal_load at the LLDB prompt or something like that?

Well, we can! LLDB supports command aliases. Just like with bash, you can create an alias in LLDB for a more complex command. The syntax is a bit different but the concept is the same. Also, just like bash, LLDB has a ‘dot file’ that it loads every time it starts which is the ideal place to put your LLDB command alises. That dot file is ~/.lldbinit.

Open up your favourite text editor, create the file .lldbinit file in your home directory and chuck the following LLDB command aliases into it:

~/.lldbinit
1
2
3
4
command alias reveal_load_sim expr (void*)dlopen("/Applications/Reveal.app/Contents/SharedSupport/iOS-Libraries/libReveal.dylib", 0x2);
command alias reveal_load_dev expr (void*)dlopen([(NSString*)[(NSBundle*)[NSBundle mainBundle] pathForResource:@"libReveal" ofType:@"dylib"] cStringUsingEncoding:0x4], 0x2);
command alias reveal_start expr [(NSNotificationCenter*)[NSNotificationCenter defaultCenter] postNotificationName:@"IBARevealRequestStart" object:nil];
command alias reveal_stop expr [(NSNotificationCenter*)[NSNotificationCenter defaultCenter] postNotificationName:@"IBARevealRequestStop" object:nil];

The above snippet creates four command aliases: reveal_load_simreveal_load_devreveal_start and reveal_stop.

reveal_load_sim assumes that you’ve installed Reveal into your system’s standard /Applications folder and reveal_load_dev assumes you have added the Reveal dylib to your application’s Copy Resources build phase. Yes, I was fibbing a little about not having to modify your Xcode project. Sorry.

With these aliases defined you can now issue these quick commands at the LLDB prompt in Xcode. LLDB even auto-completes them for you!

Note that the reveal_load_dev command alias will work with the iOS Simulator too, but it requires libReveal.dylib be bundled in your application as a resource. See the Reveal Integration Guide (in Reveal’s help menu) for more information on using the Reveal dylib.

Ok so now you have some quick LLDB command aliases and can trigger them manually, how do you make it more automatic?

Breakpoints!

Xcode (LLDB really) can execute commands in response to breakpoints. Using this knowledge we can add breakpoints to our application that load and start Reveal automatically.

The image above shows how you can add the Reveal LLDB commands to a break point in Xcode.

Some things to note about this breakpoint:

  • The break point is set to continue automatically so that the debugger doesn’t stop your app from executing when it loads Reveal.
  • Both the load and start commands are being executed. If you place your breakpoint in (or before) application:didFinishLaunchingWithOptions: you don’t have to actually issue the reveal_start command. Reveal automatically listens for UIApplicationDidFinishLaunchingNotification and starts automatically when this notification is fired by an app. There is no harm in trying to start Reveal twice. You might just see duplicate log messages.

When the breakpoint commands are successfully executed by LLDB you will see something like the following in the debugger console:

1
2
(void *) $0 = 0x0000000109022350
2013-11-07 15:18:20.687 Test[18049:70b]  INFO: Reveal server started.

Now you’re armed with LLDB and Reveal super powers!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值