Reader small image

You're reading from  iOS 17 Programming for Beginners - Eighth Edition

Product typeBook
Published inOct 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781837630561
Edition8th Edition
Languages
Tools
Right arrow
Author (1)
Ahmad Sahar
Ahmad Sahar
author image
Ahmad Sahar

Ahmad Sahar is a trainer, presenter, and consultant at Tomafuwi Productions, specializing in conducting training courses for macOS and iOS, macOS Support Essentials certification courses, and iOS Development courses. He is a member of the DevCon iOS and MyCocoaHeads online communities in Malaysia and has conducted presentations and talks for both groups. In his spare time, he likes building and programming LEGO Mindstorms robots.
Read more about Ahmad Sahar

Right arrow

Getting Started with visionOS

During Apple’s World Wide Developer Conference in 2023, Apple introduced Apple Vision Pro, a mixed-reality headset running visionOS. visionOS blends existing Apple development frameworks with brand new concepts, which enables you to build great apps for a new computing paradigm named spatial computing. In spatial computing, apps appear in a virtual space, digital media is blended with the real world, and users can use eye tracking, hand gestures, and voice input to interact with an app.

In this chapter, you’re going to modify the JRNL app that you completed in Chapter 22, Getting Started with Collection Views to work with visionOS.

First, you’ll learn about visionOS and spatial computing. Next, you’ll learn how to build your app to run on visionOS, and how to handle platform differences in your code. After that, you’ll modify your app’s appearance while running in visionOS. Finally, you’ll use SwiftUI...

Technical requirements

You will continue working on the JRNL project that you modified in Chapter 22, Getting Started with Collection Views.

The resource files and completed Xcode project for this chapter are in the Chapter26 folder of the code bundle for this book, which can be downloaded here:

https://github.com/PacktPublishing/iOS-17-Programming-for-Beginners-Eighth-Edition

At the time this book was written, the release version of Xcode did not support visionOS, so you will need to use Xcode 15.1 beta, which can be downloaded from Apple’s Developer website at this link:

https://developer.apple.com/services-account/download?path=/Developer_Tools/Xcode_15.1_beta/Xcode_15.1_beta.xip

Check out the following video to see the code in action:

https://youtu.be/9pHFUWZ6OtM

Let’s start by learning about visionOS in the next section.

Introducing visionOS

Apps using visionOS run in an infinite spatial canvas. You can interact with your app while being aware of your surroundings, or be immersed completely in it, and you can transition between different environments at any time.

When using visionOS, your apps can contain windows, volumes, and spaces. Windows contain traditional views and controls, and you can add 3D content to them. Volumes can display 3D content that may be viewed from any angle. A space is the virtual environment into which apps are launched. By default, apps run side by side in a single shared space, but you can also dedicate a full space, which only contains your app’s content.

To learn more about the fundamental building blocks that make up spatial computing, watch this video: https://developer.apple.com/videos/play/wwdc2023/10260/.

To learn more about running UIKit apps on visionOS, watch this video: https://developer.apple.com/videos/play/wwdc2023/111215/.

...

Adding a visionOS target to your project

At present, you can run your JRNL app in Simulator running iOS or iPadOS. You can even run it in Simulator running visionOS, but this will not take advantage of all the features that visionOS has to offer. For the best experience, you’ll need to add a visionOS target to your project.

You may find that when you try to build your app, build errors will appear. This is because some iOS and iPadOS APIs are not available on visionOS. Two common areas to watch out for are APIs that have been deprecated (such as APIs prior to iOS 14) and APIs that don’t work well in visionOS. Examples of the latter are UIDeviceOrientation and UIScreen. To resolve this, it is best you use the latest available APIs or use APIs that are known to work well with visionOS.

Let’s add a visionOS target to your app and modify it to make it run on visionOS. Follow these steps:

  1. Open the completed Xcode project in the Chapter22...

Improving your app’s appearance in visionOS

Your app is now running in visionOS, and it looks great! As you have been using the native user interface elements configured to use default colors, Xcode was able to automatically optimize your app’s appearance for visionOS. There are still areas that can be improved, though. When you position the mouse pointer over a cell in the Journal List screen, the hover effect has square edges:

Figure 26.10: Simulator running visionOS showing Journal List screen

When you add a new journal entry in the Add New Journal Entry screen, the appearance of the text view is missing the dark background of the text field:

Figure 26.11: Simulator running visionOS showing Add New Journal Entry screen

You’ll modify the hover effect for the cell to have rounded edges and modify the text view’s appearance to match the text field while using visionOS. Follow these steps:

  1. In the Project navigator, click...

Adding 3D objects to your app

You have updated your app’s appearance, and it looks great on visionOS. Now, you’ll add a 3D model to your app. To do so, you’ll combine SwiftUI and UIKit together to load and display a 3D model on the Map screen.

Follow these steps:

  1. The SketchFab website (https://sketchfab.com/) has many 3D models available for download. Search for and download a USDZ 3D model of Earth. For example, you can download one made by matousekfbto from this link: https://sketchfab.com/3d-models/earth-globe-98d2b04d46474bafb4250cc75dc583b3.
  2. Rename the downloaded file globe.usdz and drag it to the Model group in your project:

Figure 26.14: Navigator area showing globe.usdz

  1. In the Project navigator, right-click the Views group and add a new SwiftUI View file named GlobeView to it. Modify its contents as shown:
    import SwiftUI
    import RealityKit
    struct GlobeView: View {
      var body: some View {
        #if os...

Summary

In this chapter, you modified the JRNL app that you completed in Chapter 22, Getting Started with Collection Views to work with visionOS.

First, you learned about visionOS and spatial computing. Next, you built and modified it to run on visionOS. After that, you modified your app’s appearance to make it look better on visionOS. Finally, you used SwiftUI with UIKit to add a 3D globe to your app.

You now know how to update your UIKit app to run on visionOS. Fantastic!

In the next chapter, you’ll learn how to test and submit your app to the App Store.

Learn more on Discord

To join the Discord community for this book – where you can share feedback, ask questions to the author, and learn about new releases – follow the QR code below:

https://packt.link/8hBmp

lock icon
The rest of the chapter is locked
You have been reading a chapter from
iOS 17 Programming for Beginners - Eighth Edition
Published in: Oct 2023Publisher: PacktISBN-13: 9781837630561
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Ahmad Sahar

Ahmad Sahar is a trainer, presenter, and consultant at Tomafuwi Productions, specializing in conducting training courses for macOS and iOS, macOS Support Essentials certification courses, and iOS Development courses. He is a member of the DevCon iOS and MyCocoaHeads online communities in Malaysia and has conducted presentations and talks for both groups. In his spare time, he likes building and programming LEGO Mindstorms robots.
Read more about Ahmad Sahar