Quick Notes on Porting Mac OS X code to iOS
Recently I ported a bunch of code written just for Mac OS X/Cocoa to iOS. Most of this code was foundation level code or code that called Core Graphics. Once I figured out how to #def away sections of code it was easy to get it working on iOS. The amazing thing was, most of this was written without knowing what was/wasn’t compatible with iOS and the vast majority of it just worked fine and compiles for iOS without issue. I did have to make some changes or not include some code on iOS…
- isEqualTo doesn’t exist on iOS so all instances had to be changed to isEqual (no biggie)
- TARGET_OS_MAC is defined on iOS so you have to use ’#if !(TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)’ for code you only want compiled on OS X
- NSTask and NSWorkspace obviously don’t exist on iOS so there was no way to port code that used them
- a bunch of “#import <Cocoa/Cocoa.h>”’s got changed to ’#import <Foundation/Foundation.h>’
- NSInteger and NSUInteger are used whenever possible throughout the code and made it easy to get some things working on iOS
- a big downside of getting some code to work on iOS was that I took advantage of SecTransform on OS X Lion which made it really easy to do things like Base64/ZLib encoding & decoding, without that suddenly I had to find some code that did that for iOS or leave it out. Right now CWURLRequest simply doesn’t support setting a Base64 encoded http authorization header because Sec Transform is missing so the NSString category isn’t there so it can’t be used by my URL Utilities which means that can’t be used in CWURLRequest.
That’s pretty much it. I hadn’t included AppKit specific things in this package so that made the work very easy. Though soon I may start including UIKit stuff as I start to work more and more with Chameleon. It targets iOS 5 and later, and no I have no plans to support anything earlier than that. Overall once I got over the big hump in my development process it was easy to support both OS X and iOS.
The Zangetsu Library is here on Github: https://github.com/Machx/Zangetsu