One of the best advantages of having a shared runtine between Objective-C and Ruby in RubyMotion, is that it allows us to use the huge number of Ojective-C libraries available.
One way to include them in our RubyMotion project is through
CocoaPods, a dependency manager similar to RubyGems, but for Objective-C libraries. RubyMotion supports CocoaPods out-of-the-box, through the motion-cocoapods gem.
Here you can learn how to use them.
However, not every Objective-C library is supported by CocoaPods, but we can still use them.
These are the basic steps:*Find an Objective-C library that fits your needs (probably on
- Create a directory named “vendor” inside your project directory.
Clone the repository of the library to a subdirectory in “vendor/”.
You can add projects in two ways. Compiled static libraries (these files have “.a” extension) along with the project header files (.h), or Xcode projects.
Depening of which type we choose, the Rakefile configuration changes a little:
For statically compiled libraries:
app.vendor_project( "vendor/PROJECT-NAME", :producs => ['staticFileOne.a', 'staticFileTwo.a'], :headers_dir => "HEADERS-DIR") # Directory with .h files relative to vendor/PROJECT-NAME
For Xcode projects:
app.vendor_project( "vendor/PROJECT-NAME", :xcode, :xcodeproj => "PROJECT-NAME.xcodeproj", :target => "PROJECT-NAME", :products => ["libPROJECT-NAME.a"], :headers_dir => "PROJECT-NAME")
Rubymotion’s developer center documentation
explains what each parameter does.
If you chose to add a Xcode project, it will be compiled before your Ruby code when we run “rake”. However, if you change any of the Objective-C code and run “rake” again, the project won’t be compiled again. You have to run “rake clean” every time you want to recompile it.
However, RubyMotion’s support for some C basic types and structures is limited, and you will find difficulties making some libraries work. Enter
BridgeSupport generates XML files that help Objective-C bridges (RubyCocoa, PyObjc) and languages (MacRuby, RubyMotion) understand those C structures. It comes installed with Lion. If you have Snow Leopard you can download and install BridgeSupport Preview 3
here. You should then be able to call the
gen_bridge_metadata command line tool.
RubyMotion comes with pre-generated BridgeSupport files for iOS SDK frameworks. However, for 3rd party libraries you have to generate them manually.
To do that, simply
cd to the directory where the .h files of the project are located. then run the following command
gen_bridge_metadata -F complete --no-64-bit -c '-I.' *.h -o BridgeSupport.bridgesupport
This will generate a file called
BridgeSupport.bridgesupport. Put that file in the root directory of the Objective-C library. If you were getting some weird Objective-C exceptions when trying to compile your app, they should be gone now!