Sunday, February 26, 2012

Portability between iOS and OS X

Last week Apple released the developer preview of OS X Mountain Lion. I don't have it and I don't run it, but from reviews I gather that the ‘iOSification’ of OS X is continuing. OS X gets more and more features from iOS and eventually, the two will supposedly merge. Applications from the desktop cross over to mobile devices and vice versa. This may seem perfectly logical, but from a programmer's perspective this is remarkable, because iOS and OS X are very different.

iOS is a phone OS, OS X is for desktop computers and its portable brother, the laptop computer. The tablet is like an oversized phone, it's not a laptop without keyboard. This is the major difference between todays tablets and yesterdays tablet PCs. The tablet PC failed because it had a desktop interface on a tablet device. Why does this make such a big difference? The answer is that the tablet has no mouse. The mouse is a perfect pointing device. It has much greater precision than your fingertips. With a mouse, you can point at the exact pixel that you want to point at. Using a mouse you can also right click, click and drag, and click while pressing a modifier key. Try that on a tablet. Sure, you can compensate somewhat by using gestures but the lack of precision when using your fingers still gets in the way. This becomes painfully obvious when using Photoshop with a trackpad. It just isn't convenient.
So, we might think up a new kind of interface for Photoshop so that it will play nicely with a touch interface. Fair enough. The supposed merge of iOS and OS X still is remarkable in another way.

Under the hood, iOS and OS X both run the Darwin kernel. Darwin for mobile devices probably has a number of tweaks (like virtual memory management, or lack thereof) but it apparently is largely the same software. Apps for both platforms are programmed in Objective-C. You can use Xcode and Interface Builder to develop for both platforms. But while the platforms appear similar in these respects, they simply are not the same. For the user interface OS X has Cocoa and AppKit, but iOS has Cocoa Touch and UIKit. AppKit has NSView, NSButton and NSTextField, while UIKit has UIView, UIButton and UITextField. Here it clearly shows that iOS is very different from OS X. Porting an app means changing everything in the user interface. Applications should be designed around the model-view-controller paradigm so that you might reuse a lot of code, but it's not going to be easy — there's certainly more to it than just changing ‘NS’ to ‘UI’.

Okay, so let's not use the Cocoa GUI elements and stick to OpenGL. OS X has full blown OpenGL support and iOS supports OpenGL ES. For portability reasons you might stick to using plain OpenGL in an ‘ES’ish way, using only a common subset of functions. But there is another problem. OS X has the Cocoa NSOpenGLView class, while iOS uses a CAEAGLLayer. The first is a view and the second is a layer. In OS X it should be possible to use a Core Animation Layer with OpenGL too. It's however common practice to use NSOpenGLView because it integrates with the desktop windowing system. A consequence is that the code will be very different, mostly in respect to keyboard and mouse input.

Coming back to the mouse; the trackpad on OS X is handled in a very different way than the touch screens in iOS. The trackpad is actually a mouse (much like the surface of a magic mouse) and it is not the same as a touch screen. When you point your finger at a touch screen, the software gets the coordinates of the touch event. When you point at a trackpad, the software sees the mouse pointer position, which is something entirely different. When you swipe a touch screen, you generate a swipe event. When you swipe a trackpad, it actually produces a mouse scroll wheel event. This makes sense because the trackpad is a replacement for the mouse for laptop computers in the first place.

Even when you overcome this issues, you will find that the coordinate systems between the two platforms are different: OS X puts the origin of a view in the bottom left corner, while on iOS it's in the top left corner. And there is more. These are just a few differences between the two; the complete list is much longer.

It's quite an undertaking to port apps between iOS and OS X. iOS and OS X were never meant to be merged into one. They are two separate platforms for two separate ways of computing. But then the tablet came and things changed. People expect it to be a modern reinvention of the laptop computer, and in a way, it is. Trying to put a desktop experience on a mobile device didn't work; having a mobile device's interface on the desktop isn't pleasing either. Porting involves major application interface design changes. Apple acknowledges this and has some good documentation on the subject; see their pages on Migrating from Cocoa Touch if you want to know more.