Faking an iOS Framework

Ever wanted to share your iOS code with others without having them copy your code and headers and hope it compiles properly? Well you can easily do that with frameworks. I had found several guides on how to do this but they were either out-of-date or incomplete. While the iOS App Store doesn't allow you include dynamic frameworks, you can create a relocatable static framework which functions very simliar with one key difference (more on this later). One more thing we can do to simplify distribution is to create a universal framework so that they same file works on the iOS device (armv7) and in the simulator (i386).

Let's get started by creating a new project in Xcode.

Continue reading "Faking an iOS Framework"

jruby with 1.9 mode as the default with rvm

Thanks to recent commits from Wayne, you can now pass a build flag when install jruby with rvm and it will build jruby instead of downloading a prebuilt copy. Make sure you rvm get head && rvm reload first. Then we can install jruby with 1.9 mode as the default:

rvm install jruby -C -Djruby.default.ruby.version=1.9

And if you want to use jruby-head:

rvm install jruby-head -C -Djruby.default.ruby.version=1.9

Cocoa Entitlements and EXC_BAD_INSTRUCTION

So I've been playing around with Lion sandboxing and using entitlements for a Cocoa app. (In case you didn't hear, all apps submitted to the App Store have to be sandboxed come this November.) The first thing you may notice when you enable sandboxing for your app in Xcode is that it turns on code signing. It has to do this because entitlements don't work unless you sign your code. Once code signing is turned on and you try to compile you may get an error:

[BEROR]Code Sign error: The identity '3rd Party Mac Developer Application' doesn't match any valid certificate/private key pair in the default keychain

This because by default Xcode will try to sign your code with an identity in your keychain that starts with 3rd Party Mac Developer Application. To be able to submit apps to the App Store you have to get a certificate from Apple so this is a sane default but I wasn't signed up of the Mac Developer Program. I was however signed up for the iOS Developer Program so to fix the error for now I changed the Code Signing Identity build setting to iPhone Developer and away I went.

Fast forward a few days after I had signed up for the Mac Developer Program and I got my certificate. So I got rid of my change Code Signing Identity and restored the default. But I soon ran into a problem, my application now crashed while starting up no matter what I changed with a EXC_BAD_INSTRUCTION error in some random Apple code.

I quickly localized the problems to having entitlements on but the non-existance of any documentation on debugging sandbox errors from Apple I was frustrated. So I fired up my application without Xcode so there would be a error trace I could view in Console. This was a great idea because I was actually able to see what was the error was.

What immediately jumped out at me was

Code identity not in ACL for container ~/Library/Containers/org.samuelkadolph.Foo/Data

and I suddenly remembered something I read in the sandbox documentation. By default in the Lion sandbox, you can only access files in a special directory under ~/Library/Containers which is named after the bundle identifier for your app. And it's protected to prevent someone from accessing the data in there with a fake app. This protection is achieved by using info from the app which was signed by your code signing identity to confirm a genuine app from a fake. So when I changed the keychain identity used to sign the code I was creating a fake app and trying to access the data.

The solution was simple after determining that was the problem. Just delete the container for your app (and only your app, every other sandboxed app has a container too and may store data in them). In my case it was rm -rf ~/Library/Containers/org.samuelkadolph.Foo.

It's unfortunate that Xcode isn't more helpful with this error and unfortunate that OS X will kill (with SIGKILL too) an application instead of using some error handling in the app itself. I hope this post helps you if you ever run into this problem.

Parsing Proxy Auto-Config files

One thing I've always been wanting to be able to do was parse a proxy auto-config file from the command line.

I only recently found the pacparser library but found it limiting because it isn't easy to install and use. So I decided to write a rubygem that would parse a pac file.

I started using the execjs gem but one thing I quickly figured out is that you cannot implement all of the pac functions in pure JavaScript. Most notably is the dnsResolve(host) function which you cannot write in JavaScript because it lacks a way to make DNS queries.

So I took the code from execjs that deals with the four native runtimes and modified it to make ruby methods available in the JavaScript runtime. And now it's available in the pac gem.

You will need a JavaScript runtime installed to be able to use the pac gem, I recommend therubyracer gem for ruby and therubyrhino for jruby. johnson only works with ruby 1.8 and mustang requires the v8 library to be installed.

gem install therubyracer pac

And now you can use the parsepac executable.

parsepac http://cloud.github.com/downloads/samuelkadolph/ruby-pac/sample.pac http://samuel.kadolph.com

Or in ruby.

require "rubygems"
require "pac"

pac = PAC.load("http://cloud.github.com/downloads/samuelkadolph/ruby-pac/sample.pac")
pac.find("http://samuel.kadolph.com") # => "DIRECT"

pac = PAC.read("sample.pac")

pac = PAC.source <<-JS
  function FindProxyForURL(url, host) {
    return "DIRECT";
  }
JS

Introducing TrueCrypt Mounter for OS X

After playing around with using TrueCrypt and syncing the volume over Dropbox I was disappointed to discover that it doesn't let you mount the volume by double clicking on the file. You have to open TrueCrypt, select the file and then type in your password.

To further expand my knowledge of OS X application bundles I set out to write an application that associated with the .tc file extension and when you opened a .tc file it would prompt your for your password and once you are done and eject the disk, automatically dismount the TrueCrypt volume.

The fruit of my labour is available in this GitHub repository and you can download the application here. Once you have copied the application to your Applications directory, it will associate with any *.tc file. You must add this extension to any of your TrueCrypt volumes because (unfortunately) TrueCrypt does not do this for you automatically.

Speeding up jruby with nailgun

I had always wondered why when I installed jruby using rvm it always built something called Nailgun but I never bothered to search about it.

That was a mistake.

Nailgun is an amazing idea that greatly speeds up the start up time of the JVM and subsequently: jruby.

Without Nailgun

$ time jruby -e ''

real        0m1.336s
user        0m2.608s
sys         0m0.205s

With Nailgun

$ time jruby --ng -e ''

real        0m0.265s
user        0m0.001s
sys         0m0.003s

As you can see, nailgun reduced the start up time for jruby by 500%. Now you may be asking "How do I get started using nailgun?". Well, if you are using rvm then all you need to do is enable the after_use_jruby hook which will start up a nailgun server for you.

    chmod +x "$rvm_hooks_path/after_use" "$rvm_hooks_path/after_use_jruby"

And that's all you need to do. rvm jruby or rvm use jruby will now start up a nailgun server if there isn't one running and it set the --ng switch for all jruby runs.

If you aren't using rvm, you will have to compile nailgun and start up a nailgun server with jruby --ng-server. Now whenever you run jruby you just add the --ng switch and it will use the nailgun server. You may want to export JRUBY_OPTS="--ng" to set the switch for all jruby runs.

Addendum: mysql2 ruby gem and Mac OS X: image not found

Back at the start of April I wrote mysql2 ruby gem and Mac OS X: image not found to deal with the extremely relative path to libmysqlclient.16.dylib. I had said I would prefer not putting libmysqlclient.16.dylib in /usr/lib but I couldn't find a dylib path that ruby uses. That is until today when I decided to try again.

I found libexecdir in RbConfig::CONFIG which is the directory where ruby can load dylib files from.

libexecdir=$(ruby -rrbconfig -e 'puts RbConfig::CONFIG["libexecdir"]')
sudo mkdir -p $libexecdir
sudo ln -s /usr/local/mysql/lib/libmysqlclient.16.dylib $libexecdir

Now ruby can load libmysqlclient.16.dylib without putting it in /usr/lib.

I forgot to mention it in the first post but if your ruby wants libmysqlclient.18.dylib, just replace the 16 with 18. Same with any other number.

mysql2 ruby gem and Mac OS X: image not found

If you are using the mysql2 ruby gem on Mac OS X you may have run into this problem before.

> require 'mysql2'
LoadError: dlopen(mysql2-0.2.7/lib/mysql2/mysql2.bundle, 9):
  Library not loaded: libmysqlclient.16.dylib
  Referenced from: mysql2-0.2.7/lib/mysql2/mysql2.bundle
  Reason: image not found - mysql2-0.2.7/lib/mysql2/mysql2.bundle

So far the only solution I have found online is to use install_name_tool to update the (extremely) relative libmysqlclient.16.dylib reference to be absolute (usually to /usr/local/mysql/lib/libmysqlclient.16.dylib).

While this solves the problem, it only works until you reinstall the mysql2 gem or install a newer version and then you have to do it again. To permanently solve it you need to create a symlink of libmysqlclient.16.dylib to /usr/lib so that it can be found with that default relative path.

Ideally you wouldn't put it in /usr/lib but I haven't be able to find a path inside of ruby that will let you load the dylib.

sudo ln -s /usr/local/mysql/lib/libmysqlclient.16.dylib /usr/lib

Enter your password and then you can use the mysql2 gem without needing to edit the compiled bundle afterwards.