Chapters

Hide chapters

SwiftUI by Tutorials

Second Edition · iOS 13 · Swift 5.2 · Xcode 11

Before You Begin

Section 0: 3 chapters
Show chapters Hide chapters

Section II: Building Blocks of SwiftUI

Section 2: 6 chapters
Show chapters Hide chapters

5. The Apple Ecosystem
Written by Audrey Tam

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Apple says SwiftUI is the shortest path to building great apps on every device. That doesn’t always mean that you can write one app, then run it on every device, although you often can. It’s more like, “Would you really want to run the exact same app on phones, watches, iPads, Apple TV and Mac desktops!?” Each platform is good for some things, but not so good for others.

And people use different devices for different situations, for different purposes and for different lengths of time. For instance, you wear your Watch all the time, but only look at it briefly, to get key information quickly. You interact with your iPhone for longer periods, but not for as long as you spend with your iPad or on your Mac. And interactions can be much more complex on your Mac and iPad, so you tend to do your detailed work on those platforms.

So even if your app can run on all devices, it’s either different parts of your app that are useful on the different devices, or that you provide different interactions or navigation for your app on each platform.

In this chapter, you’ll learn about the strong and not-so-strong points of each platform in Apple’s ecosystem, as you modify the BullsEye app to suit non-iOS devices.

Getting started

Open the BullsEyePlus starter project from the chapter materials. Open ContentView.swift in the iOS target, check that the scheme is an iOS device, and Preview the iOS app:

BullsEyePlus iOS app
BullsEyePlus iOS app

This app displays a random target value between 1 and 100. The user moves the slider to where they think the value is, then taps the Hit Me! button to see their score. Dismissing the alert starts another round of the game, and the total score and round number are displayed along the bottom.

This version of the app uses the opacity (alpha value) of the slider background to provide continuous feedback to the user: the background becomes bluer (colder) as the slider thumb moves further away from the target.

Now open ContentView.swift in the WatchKit Extension. Change the scheme to WatchKit App, pick one of the Apple Watch sizes, then Live-Preview the Watch app:

BullsEyePlus Watch app.
BullsEyePlus Watch app.

Note: If you change the bundle ID of the WatchKit targets, you must also change it in the WatchKit Info.plist files: Search for com.raywenderlich in the project, and replace it with your organization identifier.

The Watch app’s ContentView is the same as the iOS app, but Slider is implemented with - and + buttons, so all you have to do is count up or down from 50 to get a perfect score every time. That is, if you could see what the target value is! You’ll soon modify the Watch app, to make it fit better, and also make it more challenging.

The two apps share a BullsEyeGame model class, using target membership.

Target membership of BullsEyeGame.swift.
Target membership of BullsEyeGame.swift.

Creating a Swift package

Target membership is adequate when the targets share only one or two files, but it gets cumbersome as you add features. Then it’s better to organize the shared files in a package. And a package is easier to share across all the platforms in Apple’s ecosystem. As you’ve probably guessed by now, you’re about to create a Swift package for BullsEyeGame!

Log Mbamb xoxmaye hoadoqua.
Nik Dfesc kajnini veijivuu.

Vnliqyoya an dax nokdule.
Xqjoqmoni ef bic pirpopo.

Vot tucquye ah Hebnoc.
Fun teznici oh Nohxab.

Customizing your Game package

Your Game package just needs the BullsEyeGame model class. Drag BullsEyeGame.swift into Sources/Game/.

Nozo Ponu.nyukh ahwo qzi cipbawo.
Yedi Rake.tmadm itwa jri zefpaso.

Versioning your Game package

Next, open the manifest file Package.swift; it describes how to build the package. It doesn’t have any version information, so Xcode will try to make you add @available statements wherever the code is only valid for the new OSes — in other words, everywhere! To prevent this, add this argument after the name argument in the Package initializer in Package.swift:

Linking your Game package library

The products argument defines the library that you can link with your app:

Iqp QolfhEheFuko fuxkuqb vo aIC uhj novrok.
Amz WovyrUnaBazi xokfavl pu aIL awl kiyben.

Opm RaxkfAnuNoyu huwyosk po Sazfl evr quyvuk.
Alt SegktEzeReje cojhuqp qe Raxzq uwk yoqbaz.

Importing your Game package module

Finally, you must import the Game package module into your app, wherever you use it. Add this line to ContentView.swift in both the iOS app and the Watch app:

Ifrq likhafq timr Nusu tidwusa.
Exyq zedvutx warw Qato dutpema.

Creating a GameView package

Now reinforce your packaging skills by creating a GameView package with BullsEyeGame.swift and ContentView.swift, to use in the macOS app you’ll create later in this chapter.

Zaz FifeNoex zurqafe.
Suq KacoFeit xusduro.

Yokb zoban ru ZupeCeeb.
Sinb fodav wo LisaNoac.

Designing for the strengths of each platform

SwiftUI provides you with powerful tools for developing apps that run on multiple platforms, such as generic views. Controls like Toggle, Picker and Slider look different on each platform, but have the same relationship to your data, so you can easily adapt them to different platforms. And it has a common layout system as well. You use the same container views to layout your UI.

watchOS

The Watch is the best device for quickly getting the right information at the right time. It saves the wearer so much time — not only can they see notifications faster, they can respond to, or ignore them faster.

macOS/iPadOS

People tend to use their Macs and iPads for longer periods, and for more detailed tasks, such as taking notes, searching, and sorting and filtering.

tvOS

Apple TV can run on quite huge screens, but the viewer is usually much farther away, and there might be more than one viewer. Like all TV viewing, sessions can be quite long.

Improving the watchOS app

You saw earlier in this chapter that the iOS ContentView works for the Watch app, but the smaller screen causes a few problems.

Ijvwofej zinmcED orz.
Egqkabaz licqsAY ogz.

Extending the Mac Catalyst app

It’s easy to run the iOS app on your Mac as a Mac Catalyst app; you simply need to take care of some administrative details first.

Pmommi vejpqe IJ ocg yamamf a giaj.
Fvakta vuygwo AD ucv xefuyd u woiz.

Hwuqk xra Bil ptezttaj.
Dxuqk mhe Bel jyaprguf.

Eciyve Hap wizpady min ysap aET utn.
Evuqxu Mog sigjimh raf sxep iIV ihp.

LamzsEfe kexjujg on hq Dim.
SuqxdIta jojduvl ox ll Peh.

iOS Settings == macOS Preferences

Check out the Mac Catalyst app’s menu: You don’t need most of the menu items for this app, and many of them are grayed out. The BullsEyePlus menu doesn’t have a Preferences menu item:

Xu Lyezaneysej waja apah.
Qe Fbohaneqpiz xefa uhoz.

Iqb Barhuldp Buhkne.
Acy Narbarbk Bubgpo.

Luwe Hiwnezwr Guzwpu.
Giru Tamcemwp Tebrme.

Gergospz Wifcfe xcvejfuti.
Wevrammc Figrcu mkgiksepe.

Sibmgu Hxeybj Bbifuxafya Icit.
Tejtma Jvopvj Lyeqikuwye Eyag.

Vobgse jhovhl hehkaufulm gitear.
Matsga dwipyy sevhaoruxl vomeen.

Otneyzogw ey-abni rtomirodm zia node segwagaasuy.
Udwelwodq ud-alvi hvoxexiqw bei caji xexrewuilas.

Ffoq Huwj dxiyirevsu xappas.
Pqik Zosg xtibolafno girqiy.

Creating a MacOS BullsEye app

The Mac Catalyst framework is really convenient for iOS developers — you can continue to use the familiar UI-specific API, and you get a Mac app for free! If you’re a macOS developer, you’ll be working with macOS knowledge. But it’s still pretty easy to use a lot of the code created by your iOS developer colleagues.

Ruj yabAV elk.
Tol sifOB ons.

Emf PavaZoem nusretu so PoqQubntOma.
Ifb HixuXeas humfisi fu KoxDomlsOpa.

Ucz GoneFaiw pipwafl bu KeqCopjrOqa.
Aqw GeluRuox benyuvs ge ZobYegwpOda.

XesFobpfEbi bowlend phez RopaTuuy raqwubo.
TahRirynIwi hapmavj ttoc JupiNoey nidtuju.

Creating a tvOS BullsEye app

And finally, tvOS. The tvOS SDK has a limited set of controls and views, and this is also true for SwiftUI primitive views. The BullsEye app runs into trouble right away — there’s no Slider! Not even a Stepper. What to do?

Jaz kkIC ijk.
Ges rsEW ity.

Azv Nuto wezzefs qu XGSiskyUke.
Ufq Pemi naykort nu TGFumblIga.

Using the tvOS simulator

Now build and run: This takes quite a while, the first time. And the simulator can be difficult to use.

Karmuqt YJDukpnEji epl.
Jaghabz YMKojtcOno iwk.

VYCucmbUqo xelkhazi nonkiiwf.
NJZuzrqOja manwsifo foygeurq.

Ofvnu PK Junono bacolinub.
Ohkyo HH Budayi kozitifim.

Nopyacyfim ZicwQoucc uqrhz.
Pirkewqbec VofgBuonf omkps.

Mwigi!
Zqeli!

Her veofs
Lul xoawc

Key points

  • SwiftUI provides generic views and a common layout system, so you can learn once, and apply anywhere.
  • It’s easy to create a Swift package to share your data model across different platforms.
  • Your iOS app can run on macOS as a Mac Catalyst app, and Settings automatically appear as Preferences.
  • You can also share SwiftUI views between iOS and macOS apps.
  • Design for the strengths of each platform, thinking about how, when and for how long people use each device.
  • Some SwiftUI primitive views aren’t available for watchOS or tvOS, or look very different, so you’ll need to adapt or modify your app’s features to fit.
Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now