Cross-Platform iOS/Mac OS X Objective-C development?
I'll be starting medium-sized iOS project soon, a document-centric app.
Later, the software might get ported to Mac OS X. Of course, th开发者_开发技巧e whole UI will need to change; that's not what I'm asking.
Is it possible to write the core logic in such a way that it's cross-platform (Mac/iOS only), or more easily ported? Has anybody done this before, or is the usual way to just write different code for both platforms?
I'm thankful for any tips regarding this, be they about Code, Frameworks, Version Control - anything. I'd hate to realize I've painted myself into a corner only after the fact, so I want to start as well as possible.
The first step is to adhere to the MVC pattern. Views often need rewriting between iOS and OS X as you say, but the model layer and some part of controllers can be reused.
Foundation (NSArray
etc) and Core Data are shared between iOS and OS X, so it should be no problem to write a common Objective-C code which manages the model layer, such as saving the data to the disk, retrieving them from the network, etc.
One important point is to resist the urge to write a long-ish code segment which mainly deals with the model layer directly in your view controller. That long-ish code segment can be abstracted into a method acting on the model layer by itself; then you can reuse that code in an OS X app later.
For example, suppose I create a view controller managing the document shown. Then in an action method defined in the view controller corresponding to an edit operation, I might write a code directly delving into the detail of the model layer and implement the edit operation. Then I would need to perform a rather big change to the source code when the view controller is ported to a window controller (or NSDocument
subclass). Rather, I would implement a method for that particular edit operation inside the document object itself, so that I can call that method both from the view controller in an iOS app and from the window controller in an OS X app.
Any pure logic and "model" level classes you create will, in the main, work on both iOS and Mac OS X, as long as you stick to the Cocoa classes, and SQLite/Core Data, etc. methods that are common to both systems. That said, the iOS file system is heavily sandboxed, which isn't a restriction on OS X and that's something you'll also need to deal with in a "document centric" app.
However, as you state the UI level classes are completely different on both systems, with iOS using the various UIKit classes in comparison to the various NS classes on OS X. As such, it's unlikely you'll be able to share much at that level.
In terms of sharing the code between the two projects, you could create a "common" project of lower level objects, etc., add and this as a reference within the iOS and Mac OS X projects, although this may end up proving somewhat painful if things diverge (which I suspect they might). Irrespective, there's a good blog post on this topic called "Easy, Modular Code Sharing Across iPhone Apps: Static Libraries and Cross-Project References" by Clint Harris which is worth a read, although it does focus primarily on the iOS side of things.
you could just write in C, then port it to ios, osx, java, android, ...
I don't know much about this, the other guys probably know more, but if you ever wanted to port the project from iOS over to Mac then maybe UMEKit might help you...
精彩评论