Hooking into TiApp
One of the pains with using Titanium is that they make it really quite difficult to hook into methods within the Application Delegate (TiApp). This is not a file that you want to change, as it is modified with each version of Titanium.
Normally, this is not a problem, but sometimes you need to integrate some 3rd
party SDK, and all the instructions advise you to simply drop a line of code
into applicationDidBecomeActive:
. Sometimes, you can jump through hoops and
find the right JS event to listen for, but often (as was the case when iOS7 was
first released) there is simply no way to extend TiApp.
Or so I thought.
I recently stumbled across github.com/rubenfonseca/titanium-dropboxsync and realized that this module was actually extending TiApp in a way I didn’t realize was possible. So, after some reading up on “Swizzling” in Objective-C, I landed with the following solution. Perhaps this will be useful to someone else.
- Create your new module using
ti create
- Clone JRSwizzle from github.com/rentzsch and add
JRSwizzle.m/h
to your project. - Add methods to TiApp with a category, name your override methods with a prefix.
- Add a
load
method to your module that will “Swizzle” your implementation into place.
YourModule.m
References
- Matt Thompson @ NSHipster had a great article on Swizzling
- Jonathan ‘Wolf’ Rentzsch’s amazing JRSwizzle project
- github.com/rubenfonseca/titanium-dropboxsync