iOS From Scratch With Swift: Exploring the iOS SDK

A good understanding of the iOS SDK is key when developing native iOS applications. Not only will it help you choose the right tools to tackle a particular problem, it will also make sure that you don't get lost in the dozens of frameworks that are included in the SDK. In this article, we zoom in on the iOS architecture and find out what powers iOS applications under the hood.

What Is the iOS SDK?

I am pretty sure that even a few experienced iOS developers would have a hard time defining the iOS SDK in one sentence. The acronymSDKstands forSoftware?Development?Kit. The iOS SDK contains the tools and resources to develop native iOS applications, which means that the SDK enables developers to develop, install, run, and test applications in the simulator and on physical devices.

The two driving forces powering native iOS applications are Swift (or Objective-C) and the native iOS system frameworks. In the previous articles, we explored the Swift programming language. In this article, I want to explore the frameworks that power native iOS applications.

This includes high level frameworks, such as theUIKitandMap Kitframeworks, but also frameworks that are closely tied to the hardware, such as theAccelerateandCore Locationframeworks.

What Is a Native iOS Application?

You now know what the iOS SDK is, but what makes an application qualify as a native iOS application? The simple answer is that an iOS application is an application that runs on an iOS device. That's only half the truth, though. What about web applications that run in Safari?

An iOS application is a Cocoa application developed for the iOS platform. Great. What is a Cocoa application? A Cocoa application is a bit harder to define. Is it the language in which the application is written? Not really. Is it the tools with which a Cocoa application is built? No. It is possible to develop a Cocoa application without the help of Xcode.

Apple defines a Cocoa application as an application that is composed of objects that ultimately inherit fromNSObject, a root class declared in theFoundationframework, and that is tightly integrated with the Objective-C runtime.

In this article, I want to focus on the frameworks that are used to create native iOS applications. If you're interested in learning more about the Objective-C runtime, which is also used by Swift, I recommend taking a look at Apple'sObjective-C Runtime Referenceor read theObjective-C Runtime Programming Guide.

The Foundation framework provides a second root class,NSProxy. However, you will rarely, if ever, use it in any of your projects.

iOS Architecture

Another difference with web applications running in Safari is that native applications interact directly with the iOS operating system and the native system frameworks of iOS. The operating system acts as a mediator between the application and the underlying hardware. A key advantage of this mediation or abstraction is that native applications don't need to worry about future hardware changes or device specifications.

The operating system provides native applications with the necessary information about the hardware capabilities (Does the device have a camera?) and device specifications (Is the application running on an iPhone or an iPad?).

The iOS architecture can be broken down into four distinct layers:

Cocoa Touch

Media

Core Services

Core OS

This layered architecture illustrates that level of abstraction, with the higher level layers more abstracted and the lower level layers more fundamental, closely tied to the hardware. It goes without saying that the higher level layers rely on the lower level layers for some of their functionality.

Apple recommends using the higher level frameworks whenever?possible, because they are often object-oriented abstractions of the lower level frameworks. In other words, the higher level layers interact indirectly with the hardware through the lower level layers, which are inherently more complex. Of course, it's still possible to fall back to the lower level frameworks if the higher level frameworks don't meet your needs.

As a reminder, a framework is a directory that contains a dynamic shared library and the resources associated with it, such as header files, images, etc. Frameworks are the access points to various system interfaces, such as the iOS address book, the device's camera roll, and the music library.

Cocoa Touch Layer

In the previous article, I wrote about Cocoa Touch and its relation to Swift and Objective-C. In this article, I would like to discuss Cocoa Touch from a more functional standpoint. How do applications rely on the Cocoa Touch layer? What is the role of Cocoa Touch in the iOS architecture?

The Cocoa Touch layer is the topmost layer of the iOS architecture. It contains some of the key frameworks native iOS applications rely on, with the most prominent being theUIKitframework.

The Cocoa Touch layer defines the basic application infrastructure and provides a number of vital technologies, such as multitasking and touch-based input.

As I mentioned, iOS applications rely heavily on the UIKit framework. Native iOS applications cannot operate if they are not linked against the UIKit and the Foundation frameworks.

The UIKit framework, or UIKit for short, is tailored to the iOS platform. There is an equivalent framework for the OS X platform, theApplication Kitframework orAppKit. UIKit provides the infrastructure for graphical, event-driven iOS applications.?It also takes care of other core aspects that are specific to the iOS platform, such as multitasking, push notifications, and accessibility.

The Cocoa Touch layer provides developers with a large number of high level features, such as Auto Layout, printing, gesture recognizers, and document support. In addition to UIKit, it contains the Map Kit, Event Kit, and Message UI frameworks, among others.

For a complete list of all the frameworks of the Cocoa Touch layer, I'd like to refer to Apple'siOS Technology Overviewguide.

Media Layer

Graphics, audio, and video are handled by the Media layer. This layer contains a number of key technologies, such as Core Graphics, OpenGL ES and OpenAL, AV Foundation, and Core Media.

The Media layer contains a large number of frameworks including the Assets Library framework to access the photos and videos stored on the device, the Core Image framework for image manipulation through filters, and the Core Graphics framework for 2D drawing.

Take a look at Apple'siOS Technology Overviewguide for a complete list of all the frameworks of the Media layer.

Core Services Layer

The Core Services layer is in charge of managing the fundamental system services that native iOS applications use. The Cocoa Touch layer depends on the Core Services layer for some of its functionality. The Core Services layer also provides a number of indispensable features, such as block objects, Grand Central Dispatch, In-App Purchase, and iCloud Storage.

One of the most welcomed additions to the Core Services layer is ARC orAutomatic?Reference?Counting. ARC is a compiler-level feature, introduced in 2011 alongside the release of iOS 5. ARC simplifies the process of memory management in Objective-C.

Memory management is a topic we haven't covered in this series, but it's important that you understand the basics of memory management when developing Cocoa applications. Automatic Reference Counting is a great addition, but it's important that you know what ARC is doing for you.

You can read more about memory management inProgramming with Objective-Cand I strongly recommend that you do.

The Foundation framework, or Foundation for short, is another essential framework for iOS and OS X applications. In the next article, we explore this framework in more detail. The Foundation framework is more than a collection of useful classes, such asNSArray,NSDictionary, andNSDate.

Foundation provides theNSObjectroot class, which provides a basic interface to the Objective-C runtime, and it also introduces several paradigms, such as policies for object ownership. Like Core Foundation (see below), Foundation makes it possible for the different libraries and frameworks to easily share data and code.

Another important framework of the Core Services layer, and closely tied to the Foundation framework, is the C-basedCore Foundationframework, or Core Foundation for short. Like the Foundation framework, it enables the various libraries and frameworks to share data and code.

Core Foundation has a feature, known astoll-free bridging, that enables the substitution of Cocoa objects for Core Foundation objects in function parameters and vice versa.

For a complete list of all the frameworks of the Core Services layer, I'd like to refer to theiOS Technology Overviewguide.

Core OS Layer

Most of the functionality provided by the three higher level layers is built on the Core OS layer and its low level features. The Core OS layer provides a handful of frameworks that your application can use directly, such as the Accelerate and Security frameworks.

The Core OS layer also encapsulates the kernel environment and low level UNIX interfaces to which your application has no access, for obvious security reasons. However, through the C-basedlibSystemlibrary many low level features can be accessed directly, such as BSD sockets, POSIX threads, and DNS services.

Documentation

Your closest ally when developing native iOS applications is the documentation bundled with the iOS SDK. For the most part, the documentation is outstanding and it will help you get up to speed with a new technology or framework with little effort.

Even though the documentation is available online, most developers use the documentation browser that's included in Xcode. In Xcode 7, You can find the documentation browser by selectingDocumentation and API Referencefrom Xcode'sWindowmenu.

Since you'll be using the documentation extensively, you might want to learn a few shortcuts to find what you are looking for in the documentation. As I mentioned in the previous paragraph, the documentation browser provides easy access to the documentation. To quickly access the documentation, pressCommand + Shift + 0in Xcode.

During development, it can quickly become cumbersome to switch back and forth between the code editor and the documentation browser every time you need to look up a symbol, such as a class, method, or structure.

There are two solutions for efficiently browsing the documentation. Whenever you place your cursor on a class or method name in Xcode's code editor, theQuick Help Inspectorin the right sidebar shows a summary of the respective symbol. The summary contains several useful links that take you to the documentation browser.

Because I usually hide the right sidebar when I'm working in the code editor, I use an alternative method to show the documentation of a class or method. Whenever you press theOptionkey and hover over a symbol, the cursor?changes to a question mark and the symbol is highlighted.

By clicking a symbol with the question mark, a new window pops up containing the same summary as theQuick Help Inspector. Clicking one of the links in the window takes you to the documentation browser. This is a fast and convenient way to switch between the code editor and the documentation browser, especially when working with two monitors.

Conclusion

You should now have a good understanding of the iOS SDK and the various layers of the iOS architecture. The two core frameworks of an iOS application, UIKit and Foundation, are the focus of the next two installments of this series.

Not only are these frameworks indispensable for every iOS application, they contain dozens of classes that you will find yourself using often when developing native iOS applications.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末直奋,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子烘豹,更是在濱河造成了極大的恐慌回季,老刑警劉巖界斜,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡只泼,警方通過查閱死者的電腦和手機(jī)狞玛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門软驰,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人心肪,你說我怎么就攤上這事锭亏。” “怎么了硬鞍?”我有些...
    開封第一講書人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵慧瘤,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我固该,道長(zhǎng)锅减,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任伐坏,我火速辦了婚禮怔匣,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘桦沉。我一直安慰自己每瞒,他們只是感情好金闽,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著剿骨,像睡著了一般代芜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上懦砂,一...
    開封第一講書人閱讀 51,258評(píng)論 1 300
  • 那天蜒犯,我揣著相機(jī)與錄音,去河邊找鬼荞膘。 笑死罚随,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的羽资。 我是一名探鬼主播淘菩,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼屠升!你這毒婦竟也來了潮改?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤腹暖,失蹤者是張志新(化名)和其女友劉穎汇在,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體脏答,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡糕殉,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了殖告。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片阿蝶。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡黄绩,死狀恐怖羡洁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情爽丹,我是刑警寧澤筑煮,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布,位于F島的核電站习劫,受9級(jí)特大地震影響咆瘟,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜诽里,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一袒餐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦灸眼、人聲如沸卧檐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)霉囚。三九已至,卻和暖如春匕积,著一層夾襖步出監(jiān)牢的瞬間盈罐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工闪唆, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留盅粪,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓悄蕾,卻偏偏與公主長(zhǎng)得像票顾,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子帆调,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,471評(píng)論 5 6
  • 活的出彩奠骄,活的精致
    michaelshan閱讀 149評(píng)論 0 2
  • 追逐名與利 意義何在 我只愿瀟灑自如 但事與愿違 凡事總要與錢財(cái)掛鉤 嘆一切如泡沫如幻影 然終是與世俗同流合污 自...
    蒼之泱泱閱讀 1,004評(píng)論 0 0
  • 這兩天溫度驟降,路上人們行色匆匆番刊,各自奔忙含鳞。公交車上,一張張陌生的臉毫無(wú)表情芹务,或低頭看手機(jī)民晒,或抬頭看窗外,...
    和風(fēng)細(xì)雨_0e83閱讀 407評(píng)論 0 0
  • 人類對(duì)死亡的恐懼锄禽,是惡魔力量的來源。 【1】 “我們接下來該怎么辦靴姿?”李斯捂著腳沃但,扯著哭腔坐在帳篷邊。 “怎么辦佛吓?...
    尹熙真閱讀 1,244評(píng)論 2 26