Build Location-Based Projects for iOSmedia.pragprog.com/titles/dhios/storyboard.pdfXcode defines the...

8
Extracted from: Build Location-Based Projects for iOS GPS, Sensors, and Maps This PDF file contains pages extracted from Build Location-Based Projects for iOS, published by the Pragmatic Bookshelf. For more information or to purchase a paperback or PDF copy, please visit http://www.pragprog.com. Note: This extract contains some colored text (particularly in code listing). This is available only in online versions of the books. The printed versions are black and white. Pagination might vary between the online and printed versions; the content is otherwise identical. Copyright © 2020 The Pragmatic Programmers, LLC. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher. The Pragmatic Bookshelf Raleigh, North Carolina

Transcript of Build Location-Based Projects for iOSmedia.pragprog.com/titles/dhios/storyboard.pdfXcode defines the...

Page 1: Build Location-Based Projects for iOSmedia.pragprog.com/titles/dhios/storyboard.pdfXcode defines the initial storyboard of an app in the section Deployment Info, next to Main Interface.

Extracted from:

Build Location-Based Projects for iOSGPS, Sensors, and Maps

This PDF file contains pages extracted from Build Location-Based Projects for iOS,published by the Pragmatic Bookshelf. For more information or to purchase a

paperback or PDF copy, please visit http://www.pragprog.com.

Note: This extract contains some colored text (particularly in code listing). Thisis available only in online versions of the books. The printed versions are blackand white. Pagination might vary between the online and printed versions; the

content is otherwise identical.

Copyright © 2020 The Pragmatic Programmers, LLC.

All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted,in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise,

without the prior consent of the publisher.

The Pragmatic BookshelfRaleigh, North Carolina

Page 2: Build Location-Based Projects for iOSmedia.pragprog.com/titles/dhios/storyboard.pdfXcode defines the initial storyboard of an app in the section Deployment Info, next to Main Interface.
Page 3: Build Location-Based Projects for iOSmedia.pragprog.com/titles/dhios/storyboard.pdfXcode defines the initial storyboard of an app in the section Deployment Info, next to Main Interface.
Page 4: Build Location-Based Projects for iOSmedia.pragprog.com/titles/dhios/storyboard.pdfXcode defines the initial storyboard of an app in the section Deployment Info, next to Main Interface.

Build Location-Based Projects for iOSGPS, Sensors, and Maps

Dominik Hauser

The Pragmatic BookshelfRaleigh, North Carolina

Page 5: Build Location-Based Projects for iOSmedia.pragprog.com/titles/dhios/storyboard.pdfXcode defines the initial storyboard of an app in the section Deployment Info, next to Main Interface.

Many of the designations used by manufacturers and sellers to distinguish their productsare claimed as trademarks. Where those designations appear in this book, and The PragmaticProgrammers, LLC was aware of a trademark claim, the designations have been printed ininitial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer,Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trade-marks of The Pragmatic Programmers, LLC.

Every precaution was taken in the preparation of this book. However, the publisher assumesno responsibility for errors or omissions, or for damages that may result from the use ofinformation (including program listings) contained herein.

Our Pragmatic books, screencasts, and audio books can help you and your team createbetter software and have more fun. Visit us at https://pragprog.com.

The team that produced this book includes:

Publisher: Andy HuntVP of Operations: Janet FurlowExecutive Editor: Dave RankinCopy Editor: Rachel MonaghanLayout: Gilson Graphics

For sales, volume licensing, and support, please contact [email protected].

For international rights, please contact [email protected].

Copyright © 2020 The Pragmatic Programmers, LLC.

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system,or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording,or otherwise, without the prior consent of the publisher.

ISBN-13: 978-1-68050-781-2Encoded using the finest acid-free high-entropy binary digits.Book version: P1.0—August 2020

Page 6: Build Location-Based Projects for iOSmedia.pragprog.com/titles/dhios/storyboard.pdfXcode defines the initial storyboard of an app in the section Deployment Info, next to Main Interface.

Removing the StoryboardThe user interface of the app we’re going to build is quite simple. This is agood opportunity to practice building apps without storyboards. In the nextchapter we’ll use storyboards again.

Pro Tip: Always Practice

A good developer can build apps with storyboards, with XIBs, incode, and with SwiftUI. You should practice your abilities in allthese different approaches. Only when you are proficient in eachapproach can you decide which is best for the project at hand.

When setting up a new app with Xcode, you can choose only between story-board and SwiftUI for building the user interface. If you want to build theuser interface in code instead, you have to choose one of the two options andthen remove the files and settings you don’t need. What you choose mainlydepends on your preferences. The amount of work to remove the storyboardis comparable to that for removing SwiftUI support.

The first entry in the project navigator, with the blue icon and the nameWalk2Draw, is the project itself. Click it to open the project settings. Next,select the target in the TARGETS section to open the target settings. Selectthe General tab if it’s not already selected. Xcode defines the initial storyboardof an app in the section Deployment Info, next to Main Interface. RemoveMain from the text field to tell Xcode that the app shouldn’t load a storyboardwhen it launches.

As of Xcode 11, the storyboard is defined in a second place, so we’ll have tomake changes there as well. Open the file Info.plist and navigate to ApplicationScene Manifest > Scene Configuration > Application Session Role > Item 0.Delete the whole line with the setting for Storyboard Name. The result shouldlook like what’s shown in the image on page 2.

Once we’ve made the changes, the storyboard won’t be used anymore, so wecan delete it. Select the file Main.storyboard and remove it from the project bydeleting it.

If you think removing a storyboard from a project should be easier, I feel you.I hope Apple adds the option to use neither a storyboard nor SwiftUI in afuture version of Xcode, but I won’t hold my breath.

Now that the storyboard is gone, we need to create the window and assignits rootViewController when the app loads. Open SceneDelegate.swift, look for the

• Click HERE to purchase this book now. discuss

Page 7: Build Location-Based Projects for iOSmedia.pragprog.com/titles/dhios/storyboard.pdfXcode defines the initial storyboard of an app in the section Deployment Info, next to Main Interface.

method scene(_:willConnectTo:options:), and replace the underscore (which acts asa placeholder) in the following line of code with scene:

Map/Walk2Draw/Walk2Draw/SceneDelegate.swiftguard let _ = (scene as? UIWindowScene) else { return }

Afterward, the line should look like this:

Map/Walk2Draw/Walk2Draw/SceneDelegate.swiftguard let scene = (scene as? UIWindowScene) else { return }

With this line we try to cast the scene variable passed into scene(_:willConnect-To:options:) to the type UIWindowScene. If it fails, we return from this method. Nowchange the contents of scene(_:willConnectTo:options:) so that it looks like the fol-lowing code. The changed lines are highlighted.

Map/Walk2Draw/Walk2Draw/SceneDelegate.swiftfunc scene(_ scene: UIScene,

willConnectTo session: UISceneSession,options connectionOptions: UIScene.ConnectionOptions) {

guard let scene = (scene as? UIWindowScene) else { return }

window = UIWindow(windowScene: scene)➤

window?.rootViewController = ViewController()➤

window?.makeKeyAndVisible()➤

#if DEBUGtrigger = LogTrigger(in: window)#endif

}

• 2

• Click HERE to purchase this book now. discuss

Page 8: Build Location-Based Projects for iOSmedia.pragprog.com/titles/dhios/storyboard.pdfXcode defines the initial storyboard of an app in the section Deployment Info, next to Main Interface.

In this code, we first instantiate a new instance of UIWindow using the scene.As of iOS 13, a scene represents the user interface—or more precisely, aninstance of the user interface. If we don’t provide the window with the scene,the screen remains black. Next we assign an instance of ViewController to therootViewController property of the window to define the initial view controller ofour app. Then we call makeKeyAndVisible() to tell the window that it should becomevisible and that it is the key window. The key window of an app is responsiblefor handling nontouch and keyboard-related events. Remember, when creatingthe window of the application in code, we always have to call makeKeyAndVisible().

Setting up the user interface in code might be something you haven’t done alot. Let’s see if our changes work. We can use an easy trick to figure out ifXcode launched the correct view controller.

Open ViewController.swift and add the following line at the end of viewDidLoad():

Map/Walk2Draw/Walk2Draw/ViewController.swiftview.backgroundColor = .red

Select your favorite simulator and build and run the code by clicking the playbutton in Xcode. After the app is loaded, you should see a red screen in thesimulator. If you don’t, retrace your steps and compare them with your codeand settings.

Pro Tip: Alternate Simulators

Alternate the simulators you use during development to find user-interface bugs or layout problems related to false assumptions asearly as possible.

• Click HERE to purchase this book now. discuss

Removing the Storyboard • 3