iOS support

Not yet ready to merge, but bringing in @thenikso's changes and tidying up as we go.