Last updated: February 13, 2018
Cocoa and Cocoa Touch: How to Get Started Build Mac and iOS Apps
Cocoa is a development API native to the Mac OS X operating system. Cocoa Touch is the closely-related analogous platform for the iOS. It is written in Objective-C, and acts as a sort of “top layer” to each operating system.
Cocoa can be thought of as an application platform or an application development framework, but it is more integrated into the operating system than either of these would be in a Linux or Windows environment.
This tight integration between the API, the operating system, and (of course) Apple’s own hardware is intended to provide a seamless development environment and enforce “the Apple way” of doing things.
History of Cocoa
Cocoa starts with C. Literally.
In the early 1980s, a pair of developers named Brad Cox and Tom Love sought to add Object Orientation to the C programming language, which did not support OOP at that time.
Their solution was to create a pre-processor that would render C code with some Smalltalk-like additions into plain compilable C code. (Smalltalk was an early object-oriented language.)
This preprocessor quickly evolved into a full-blown language extension and became known as Objective-C. NeXT software, which was founded by Steve Jobs after he was fired from Apple in 1985, licensed Objective-C from its creators and used it as the basis for several development frameworks.
These formed the NeXTSTEP and OpenStep application platforms, which NeXT sold through the 80s and 90s.
Apple acquired NeXT in 1996, and Steve Jobs returned as CEO. The NeXTSTEP frameworks made their way into the Mac operating system.
Apple had already trademarked the name Cocoa, for a separate project which was discontinued. In order to quickly put a trademarked brand name on Apple’s newer version of the frameworks, they called it Cocoa.
Cocoa has been a part of all of Apple’s operating systems since the late 1990s, and has evolved along with the OS and the new device platforms since then.
What Does Cocoa Do?
Cocoa does a lot of things, but broadly speaking these fall into four categories:
Acting as an expanded standard library for Objective-C, providing support for richer objects and features of modern computing languages not present in the design of Objective-C.
Providing a unified user-interface and user-experience.
Allowing an application to access other applications, services, and device features.
Encouraging particular architectural and data management patterns.
These broad design goals are implemented in several “frameworks.” This term might be a bit confusing. In contemporary parlance, Cocoa is more like a single framework, with several modules or libraries. But Apple calls each of these libraries a “framework.”
Let’s explore each of these functional goals a bit, and how the various Cocoa frameworks achieve them.
Cocoa as the Standard Library
Objective-C has a standard library. In the Cocoa environment, the standard library and the runtime are actually provided by Cocoa. Additionally, a set of expanded language features are provided by the Foundation Kit which, in practice, act as a de facto standard library.
This provides more advanced language features than would normally be available in Objective-C. This includes a wide range of basic classes for commonly used types of objects — numbers, dates, strings, URLs, regular expressions, and error messages.
This Foundation Kit framework is essential in apps written in Objective-C. However, app development in Apple platforms is moving away from Objective-C, as Apple promotes its newer Swift development language.
Swift natively provides many of the modern language features added by Foundation Kit, and it is unclear what role Foundation, and Objective-C, will play in a Swift-centric future.
Cocoa as a UI/UX Interface
This aspect of Cocoa is perhaps the most obviously important to app developers.
In both cases, these provide graphical elements, user interface controls, window and panel management, multi-touch interfaces, and all the other details of interaction between the user and the system.
Use of AppKit and UIKit is what makes an application look and feel native to the Apple system, and respond to inputs (gestures, clicks, movement) the “right” way.
The big difference between Cocoa and Cocoa Touch is the difference between AppKit and UIKit. As much as Apple has worked to unify the user experience across platforms, you still interact with an iPhone differently than you do with a desktop Mac.
Besides slightly different GUI elements, UIKit includes support for things like compass direction, GPS, accelerometer (device movement), and touch screen.
Cocoa as Operating System Interface
Apple apps also need to be able to interact with other apps and operating system services.
If you want to be able to click on a phone number in your chat app and have that launch a phone call, apps need to be able to talk to each other.
If you want a music editing app to be able to pick up sound from a microphone and play to your speakers, apps need to be able to interact with operating system services.
Cocoa provides a whole long list of frameworks which provide access to apps and services. These are what allow apps to not just be standalone executables, but to exist and operate in an integrated ecosystem.
Cocoa as Architect
Cocoa supports two important architectural patterns, one within an application and one in how an application communicates outside of itself.
Cocoa is written with the Model-View-Controller architectural paradigm in mind, and supports MVC development at all levels.
View is implemented by the AppKit or UIKit frameworks. Controller modules are supported through a family of classes built into the Foundation Kit. Models, and their required connection to a data persistence layer, are handled by another framework called Core Data.
This design encourages (some might say enforces) a Model-View-Controller architecture within iOS and OS X applications.
Between different applications, and also between applications and services, the various frameworks mediate interaction in an essentially service-oriented fashion. This makes it both easier and safer to integrate apps with each other, promoting a robust, resilient application ecosystem.
Cocoa Controls: open source UI components for iOS and OS X.
Cocoa Pods: a package manager for Cocoa projects.
Community and Continued Learning
CocoaHeads: a worldwide network of local meeting and discussion groups dedicated to Cocoa and Cocoa Touch development.
Other OS X and iOS Development Resources
You cannot use Cocoa or Cocoa Touch in a vacuum. It is a piece of the larger Apple App development environment, which includes Xcode, Objective-C, and Swift. These different tools and technologies work together to form a development ecosystem.
Xcode is a required Integrated Development Environment (IDE) for building OS X and iOS applications. It is tightly integrated with Swift, Cocoa, the Apple App store, and the larger Apple ecosystem.
There aren’t a lot of tutorials about Xcode specifically, as it is covered in every general Apple development resource. One great resource, though, is this Xcode Tutorial, which is regularly updated as new versions of Xcode come out.
About Objective-C: an introductory tutorial from the Apple Developer center
We also have a more complete list of Objective-C resources.
The Swift Programming Language: the official Apple guide
Swift Programming: The Big Nerd Ranch Guide: the definitive book on the subject.
We also have a thorough list of Swift resources.
General Apple Development Resources
Many of the best books on Mac and iOS development come from Big Nerd Ranch. Their books on specific topics are mentioned above, and you should also check out their books on Advanced Mac OS X Programming and iOS Programming.
Other good general resources:
Ray Wenderlich Tutorials: one of the relatively few providers that goes back and updates old tutorials when Apple updates the platform.
Apple Programming Channel on YouTube: not official, but very helpful.
WeHeartSwift: nominally about Swift, but this site covers all aspects of OS X and iOS development.
NSHipster: a popular blog about Mac and iOS development, with a lot of tutorials on “overlooked bits in Objective-C, Swift, and Cocoa.” They are also very opinionated in their coverage, which is probably a strength.
ObjC.io: books and articles on advanced techniques and practices for iOS and OS X development.
Cocoa and Cocoa Touch are the foundations of native App development for Mac and iOS.
If you want to build the next big iPhone app, or create Mac apps that integrate well with OS X and conform to user expectations, you really need to dig into Cocoa and Cocoa Touch. For native app development, there really are no substitutes.