Category: Tools

Creating a UIKit Xcode Project

A common problem people learning UIKit have when following a tutorial is that what they see in Xcode does not match what the tutorial shows. The tutorial shows a view controller file, but they see a content view file.

The most common cause of this problem is creating an Xcode project that uses SwiftUI instead of UIKit. When you create a new project in Xcode, the default is to create a SwiftUI project.

To create an Xcode project that uses UIKit, you must create an iOS app project and tell Xcode to use a storyboard for the user interface. UIKit uses storyboards, and SwiftUI doesn’t.

Create an iOS App Project

In Xcode choose File > New > Project to create a new project. The New Project Assistant opens.

XcodeCreateiOSAppProject

Click on iOS at the top of the assistant to see the iOS app project templates. Choose either App or Document App. If you’re not sure, choose App. Click the Next button.

Choose Storyboard for the User Interface

When you click the Next button, the New Project Assistant takes you to the next step in creating the project.

XcodeChooseStoryboard

Choose Storyboard from the Interface menu to create a UIKIt project.

If you don’t see an Interface menu (or a menu with a similar name), click the Previous button and make sure you choose an iOS app project. Xcode’s multi-platform project templates create SwiftUI projects.

Deleting an Xcode Project’s Git Repository

Deleting a git repository for an Xcode project is relatively easy: move the folder that holds the git repository out of the project folder. But the Finder makes locating the repository folder difficult. In this article you will learn how to find the repository folder so you can delete it.

Where is the git repository?

The git repository is in a .git folder inside the project folder. If you go to the project folder in the Finder, you won’t see the .git folder. Why can’t you see it?

You can’t see the .git folder in the Finder because the Finder hides files and folders that start with a dot. To see the .git folder in the Finder, press Cmd-Shift-Dot. Pressing Cmd-Shift-Dot shows hidden files and folders.

Press Cmd-Shift-Dot a second time to stop showing the hidden files and folders in the Finder.

Deleting the repository

Move the .git folder out of the project folder, and git will stop tracking changes in your project. You will normally move the .git folder to the Trash.

If you want to create a new git repository for your project after removing the previous one, choose Integrate > New Git Repository in Xcode. Older versions of Xcode have a Source Control menu instead of an Integrate menu.

Want to learn more about git?

Check out my version control book. It shows you how to do the most common git tasks without leaving Xcode. Some of the material covered in the book includes cloning projects from GitHub, branching, and going back to earlier versions of your project.

Where is the Info.plist file?

Recent versions of Xcode do not add an Info.plist file to new iOS app projects. How do you add settings to your app that you would normally place in Info.plist?

Xcode’s project editor has an Info section for the app target, where you can add the settings. Take the following steps to access the Info section:

  1. Select your project from the project navigator on the left side of the project window to open the project editor.
  2. Select your app from the target list on the left side of the project editor.
  3. Click the Info button at the top of the project editor.

InfoPlistTargetSettings

At the top of the Info section is a Custom Target Properties list. To add an item to the Info section, move the mouse arrow over one of the keys in the list. Tiny buttons to add and remove an item will appear on the right side of the key where the mouse arrow is. Click the Add (+) button to add a setting to the Info section.

Working on a SwiftUI Project in Xcode and Swift Playgrounds

You have a Mac with Xcode and an iPad with Swift Playgrounds. You have a project that you want to develop using both devices. How do you create a project that you can edit in both Xcode and Swift Playgrounds?

Create a Swift Playgrounds App project in Xcode. This project template is designed to work in both Xcode and Swift Playgrounds.

ChooseSwiftPlaygroundsAppProject

The Swift Playgrounds App project is in the iOS section of the New Project Assistant in Xcode.

Replace Swift print Statements with Xcode Breakpoint Actions

When you run into problems with your Swift code, your first instinct may be to add print statements. There’s nothing wrong with print statements, but you can also use Xcode breakpoint actions to print to Xcode’s debug console. Using Xcode breakpoint actions instead of print statements has the following advantages:

  • You don’t have to write any code.
  • They fire only when debugging.
  • You can turn them on and off easily without having to rebuild your project.

Take the following steps to create a breakpoint action equivalent of a print statement.

  1. Add a breakpoint.
  2. Add a breakpoint action.
  3. Choose Log Message from the breakpoint action menu.
  4. Enter what you want to print in the text field.
  5. Select the Automatically continue after running checkbox.

Add a Breakpoint

Click on the left side of Xcode’s editor next to the line of code where you want to set the breakpoint.

EnabledBreakpoint

The breakpoint appears as a blue arrow in the editor.

Add a Breakpoint Action

Right-click on a breakpoint in Xcode’s editor or in Xcode’s breakpoint navigator (Press Cmd–8) and choose Edit Breakpoint. A popover opens to edit the breakpoint.

XcodeBreakpointEditor

Click the Add Action button to add a breakpoint action.

Printing Text

Clicking the Add Action button expands the breakpoint editor.

XcodeBreakpointActionEditor

Choose Log Message from the Action menu. The Log Message action is the equivalent of a Swift print statement.

If you want to show that you entered a function in your code, enter the text in the text field.

If you want to print the value of a variable, wrap it with the @ character. The following message:

Prints the following in Xcode’s debug console if the x position is 12:

Automatically Continue

At the bottom of the breakpoint editor is a checkbox to automatically continue after running the breakpoint action. Select the checkbox.

Now if you run your project, the messages will appear in Xcode’s Debug console. You created the equivalent of a print statement without writing any code.

Turning off Printing

To turn off printing, disable the breakpoint. Click on the breakpoint in Xcode’s source editor to disable the breakpoint. Click a disabled breakpoint to enable it.

Related Reading

Genrerating Swift Code for REST APIs with CreateAPI

If you are starting to develop a Swift app that works with a REST API, you may be overwhelmed thinking about all the code you have to write. What if you forget a data structure? What if you forget to add a property to a data structure? Wouldn’t it be nice to have someone create the Swift code for you?

The command-line app CreateAPI makes this possible. Supply a JSON or YAML file that contains the REST API, and CreateAPI creates a Swift package with Swift code for the REST API. By using CreateAPI you can make faster progress on a Swift app that uses REST APIs.

Installing CreateAPI

The CreateAPI GitHub page has installation instructions. You can install it using Homebrew or Mint or by cloning the GitHub repository and installing it from source. Installing from source worked for me.

Generating a Swift Package with CreateAPI

Generating a Swift package with CreateAPI has the following steps:

  1. Download a JSON or YAML file that contains the REST API.
  2. Open the Terminal app.
  3. Navigate to the folder that contains the file.
  4. Run CreateAPI.

The most common way to run CreateAPI is to run the create-api generate command with the --output and --config-option options. The following example demonstrates running CreateAPI:

The name of the JSON file must be in quotes. Replace APIFile.json with the name of the file containing the REST API. Replace PackageName with the name you want for your Swift package.

The CreateAPI GitHub page has a tutorial that contains more detailed instructions.

NOTE

CreateAPI is in its early stages of development. It may not work with every REST API. I was unable to generate a Swift package for GitHub’s REST API.

Add the Package to Your Xcode Project

After creating the Swift package you can add it to your project like any other Swift package. If you have never added a Swift package to a project in Xcode, take the following steps:

  1. Select the project file from the left side of the project window to open the project editor.
  2. Select the project from the left side of the project editor.
  3. Click the Package Dependencies button at the top of the project editor to show a list of Swift packages in the project.
  4. Click the Add button at the bottom of the package list.
  5. Click the Add Local button in the sheet to add your package.

Import the package module in your source code files, and you can start using the package in your app.

If you want to see an example of a Swift package that CreateAPI creates, I have a package for Jira’s REST API on GitHub.

Xcode Multiplatform App Targets

Starting in Xcode 14, when you create a multiplatform app project, Xcode creates a single app target with destinations for each platform you want to support: iPhone, iPad, Apple TV and Mac. This article provides an introduction to multiplatform app targets.

When to Use the Multiplatform App Target

Use the Multiplatform App Target if you want people to buy one version of your app on the App Store and run the app on all the platforms you support: iPhone, iPad, AppleTV, and/or Mac. Use separate targets if you plan to charge for each platform separately.

If you plan to sell a Mac version of your app outside the App Store, create a separate app target. Use the multiplatform app target to sell on the App Store and the Mac app target to sell outside the App Store.

Viewing, Adding, and Removing Platform Destinations

Select the target from the target list on the left side of the project editor and click the General button at the top of the project editor to see a list of the app’s platform destinations:

AppTargetSupportedDestinations

The supported destinations list has buttons to add and remove destinations. If you created a project in an older version of Xcode, you must add destinations to have a single app target that supports iPhone, iPad, AppleTV, and Mac. Older versions of Xcode have separate targets for each platform.

Mac Destination Choices

When adding the Mac destination to an app target, you have the following choices:

  • Mac
  • Mac Catalyst
  • Designed for iPad

Choosing Mac uses SwiftUI and/or AppKit for the Mac app. Mac is the best choice for a new app, especially one that uses SwiftUI.

Choosing Mac Catalyst uses UIKit for the Mac app. Mac Catalyst is the best choice if you want to make a Mac version of an existing iOS app.

Choosing Designed for iPad runs the iPad version of the app on Macs with Apple Silicon chips. Choose Designed for iPad if you want a Mac version of your iOS app and don’t want to do any work converting the app.

Choosing the Platform to Build and Run

Xcode can build and run for only platform at a time. How do you specify the platform to build and run?

There’s a jump bar in the project window toolbar.

ChoosePlatformToRun

Click the right part of the jump bar to choose the platform: Mac, a connected iOS device, or an iOS simulator.

Compiling Files for Specific Platforms

Previous versions of Xcode have separate targets for each platform. If you have separate targets with code that should be compiled for a specific platform, make the file a member of that target. But you can’t do that with the multiplatform app target because there’s only one target. What do you do if you have platform-specific source code files?

Tell Xcode what platforms a source code file should compile for. Click the Build Phases button at the top of the project editor and examine the Compile Sources build phase.

FilterDestinations

Xcode initially sets each source code file to build for each destination. Click on the Filter column for a source code file to open a popover.

PlatformFilterPopover

Deselect the Allow any platform checkbox and deselect the destinations you don’t want to use. Now that file compiles only for the platforms you specified.

Getting Started with the Get Networking Framework

The Get framework is a lightweight Swift framework for working with web APIs. Get is a nice framework, but the README file doesn’t provide much explanation on how to use the framework. This article shows how to start using the Get framework in your apps.

Basic Workflow

Using the Get framework involves the following basic steps:

  1. Create an API client instance
  2. Create a network request
  3. Send the request

Create an API Client Instance

Get provides an APIClient class for API clients. Create an instance, supplying the base URL to the network API you want to use. The following example creates an API client for the Jira REST API:

Create a Network Request

Get has a Request struct that represents a network request. Fill the Request struct to create a network request. The Request struct has the following fields:

  • The body, which you use to send data from your app to the API. POST and PUT methods require you to supply a body.
  • Headers for the request. The headers field is a Swift dictionary where each key and value are strings.
  • An ID for the request. You probably won’t need to set an ID.
  • The method, such as GET, POST, or PUT.
  • Query items. The query field is an array of Swift tuples, consisting of two strings.
  • The URL for the network call.

The Get framework defaults to using GET methods and leaving the other fields blank so the only fields you have to fill are the ones you need to make the network call.

To create a request for a GET method, start by creating a variable of type Request. Place the data type you want to return in angle brackets and supply the URL for the network call.

Replace T with the type you want to return from the network call. Replace urlPath with the URL string for the API call to make.

After creating the request, fill the fields your network call requires.

If you have a method that sends data to the server, such as a POST or PUT method, you must create the body data and supply it when creating the request. The body property is a let constant so you can’t set the body after creating the request.

The body must conform to the Encodable protocol.

Sending the Request

The API client has a send method to send the network request. Supply the request.

Usually a network call returns data. By attaching the value property when sending the request, you will get decoded data back in the type you specified when creating the request.

You’re going to use this code a lot when using Get. In one line of code you can send a network request and get data back in a form that your app can use.

Dealing with “Failed to prepare device for development” Error Message in Xcode

You update your iOS device to the latest version of iOS. Now you try to run your Xcode project on that device. An alert pops up in Xcode with the message Failed to prepare device for development.

Why Are You Getting This Message?

You are getting the Failed to prepare device for development message because the device is running a newer version of iOS than Xcode supports.

Each version of Xcode ships with a version of the iOS SDK. The Xcode Releases site has a list of each version of Xcode along with the iOS SDK. The version of the SDK is the latest version of iOS that version of Xcode supports. You won’t be able to run your Xcode project on a device running a newer version of iOS.

For example Xcode 13.2.1 ships with the iOS 15.2 SDK. If you are running Xcode 13.2.1, you won’t be able to run your project on a device running anything newer than iOS 15.2. If you have a device running iOS 15.4, you won’t be able to run the project on that device with Xcode 13.2.1.

How Do You Fix the Error?

The easiest solution is to install the latest version of Xcode. The latest version of Xcode has the latest version of the iOS SDK so you’ll be able to run and debug your project on a device running the latest version of iOS.

If your Mac is running an older version of macOS, you might not be able to update to the latest version of Xcode. Xcode 13.3 requires macOS 12. If your Mac is running macOS 11 and can’t run macOS 12, you won’t be able to update to Xcode 13.3. What do you do then?

There are two workarounds. The first is to download iOS support files for the newer iOS version and copy them to your Xcode app bundle.

The second workaround is to deselect the Debug executable checkbox in the Run step of the scheme. After the app launches, attach it to the debugger. The following article provides details and screenshots:

Debugging on iOS 15 with Xcode 12

iOS 17 Update

Starting with iOS 17 Apple no longer includes iOS support files so the first workaround won’t work. According to the following Stack Overflow question:

How to run on iOS 17 Device using Xcode 14

The solution is to run the following command in the Terminal and restart Xcode:

Turn off Automatic Updates on Your Device

Keeping up with the latest version of iOS requires using the latest version of Xcode. Xcode takes a long time to install. If you don’t want to be constantly updating Xcode, turn off automatic updates on your iOS device.

Why Won’t My Xcode Project Build?

It’s frustrating to click the Run button in Xcode and find that your project won’t build and run. Xcode initially shows you a message like Command CompileSwiftSources failed with a nonzero exit code, which really doesn’t help much. How do you find where the error is so you can fix it and run your project?

Get a List of the Errors

The first step to figuring out why your project won’t build is to find the compiler errors in your project. Open Xcode’s issue navigator by pressing Cmd–5.

IssueNavigator

The issue navigator shows the compiler errors in your project. Select an error to show the line where the error is.

Seeing More Detailed Information about an Error

To see more detailed information about a compiler error, open the report navigator by pressing Cmd–9. Select a build from the report navigator to see the build steps and errors for that specific build.

ReportNavigatorHighlighted

Click the Log button (the small button with the horizontal lines) to see a detailed log of that particular build step.

Tips for Fixing Errors

You found the compiler errors in your project. How do you fix them?

I can’t tell you specifically how to fix the errors because I can’t see your code. But I have some general tips to help you fix the compiler errors in your Xcode projects.

Make Sure Argument Types Match

When calling functions, make sure the data types for the function arguments match. When the argument types don’t match, you get a compiler error message like the following:

Where X and Y data types.

If a function takes an integer as the first argument and you pass a string, you’re going to get the Can't convert value compiler error.

Check for Typos

If I make a typographical error in this article, you’ll still be able to read the article. But if you make one small typo in your code, the project won’t build.

Make sure you spell function names correctly when calling them. If you have a function called doSomething and you call it like the following:

You are going to get a compiler error because you made a typo when calling the function. There is no doSomethng function.

Make sure that you spell variable names correctly when accessing them. If you have a player variable in your app and misspell it,

You are going to get a compiler error because there is no playr variable.

Make sure each left brace, bracket, parenthesis, and quotation mark has a matching right one. An extra left or right brace can lead to compiler errors with bizarre messages.

Import Missing Modules

Make sure you import the modules for the frameworks your app uses. If you’re writing an app that works with PDF documents, you must import the PDFKit framework to call its functions in your app.