How to enable remote web inspector for UIWebView

Remote debugging UIWebView with the iOS simulator

For a little while people have figured out how to enable the remove inspector in iOS applications. With this trick, you can get a Webkit inspector (Opt+Cmd+I) for a UIWebView running in an app on the iPhone simulator. This will give you Weinre like abilities but it works much more reliably. Then, it didn’t take long before someone came up with an app to do all the GDB debugger attaching for you.

However, enabling the Webkit remote inspector is a private API call and if you have it in your application when you submit it to the App Store, your application will be rejected. Also, it only works on the simulator, if it runs on a device you cannot access the remote inspector because of firewall rules.

An easy solution is to include some precompiler directives that will only add the private API call when you compiled your app for the iOS simulator:

#if (TARGET_IPHONE_SIMULATOR)
    [NSClassFromString(@"WebView") _enableRemoteInspector]; // Private API call.
#endif

You need to import the TargetConditionals.h file too:

#ifdef __APPLE__
    #include "TargetConditionals.h"
#endif

You can access the remote inspector by loading your app in the iOS simulator and navigating to http://localhost:9999/.

Advertisement

About this entry