Telling Git to Ignore Files in Xcode Projects

February 28th, 2018

Filed under: Version Control, Xcode | Be the first to comment!

When you create a project in Xcode and create a git repository, git initially tracks every file in the project, but this behavior isn’t what you want. Xcode projects contain a lot of files. Some of these files shouldn’t be tracked by git because they change every time you build your project. Having files you don’t want in git being marked as updated and ready to commit is annoying at best. At worst Xcode won’t let you switch branches because of uncommitted changes.

How do you get git to track the files you’re interested in tracking, such as your source code files, and not track the files you don’t care about? Create a git ignore file and add it to your git repository.

Generating a Git Ignore File

The git ignore file is just a text file with the name .gitignore. It contains a list of files and directories. Git will ignore any of the files or directories it finds in the git ignore file.

What files and directories do you put in a git ignore file? That depends on the tools and languages you use to build your software. Fortunately the website gitignore.io has done the hard work for you. Go to gitignore.io and enter a programming language or an IDE to create a git ignore file. If you go to the site and enter Xcode, gitignore.io creates the following git ignore file:

# Created by https://www.gitignore.io/api/xcode

### Xcode ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, 
#   Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint

### Xcode Patch ###
*.xcodeproj/*
!*.xcodeproj/project.pbxproj
!*.xcodeproj/xcshareddata/
!*.xcworkspace/contents.xcworkspacedata
/*.gcno

# End of https://www.gitignore.io/api/xcode

Xcode project files have a lot of files in them, don’t they?

Saving the Git Ignore File

Now you need to copy the git ignore file into a text file. Open a text editor (not a word processor like Pages or Word). If you don’t have a text editor, download BBEdit or Visual Studio Code. They should do the job.

Copy the contents of the git ignore file into the text document. Save it in the project folder with the name .gitignore. Files that start with . are invisible in the Finder. To show hidden files in the Finder, hold down the Command and Shift keys and press the period (.) key. If you navigate to your project folder, you should see the git ignore file. Press Cmd-Shift-Period to go back to hiding the invisible files.

Adding the Git Ignore File to Your Repository

After creating the git ignore file, add it to your repository. In Xcode choose Source Control > Commit. A sheet opens.

XcodeCommitGitIgnoreHighlighted

Select the checkbox next to the git ignore file and deselect any other checkboxes. Add a commit message and click the Commit button.

Learn More About Version Control

Facebooktwittergoogle_plusredditmail

Xcode 9: Adding Your Project to GitHub

February 21st, 2018

Filed under: Version Control, Xcode | Be the first to comment!

Before Xcode 9, setting up your project to work on GitHub involved a lot of work. You had to login to GitHub, create a repository, go back to your Mac, launch the Terminal application, navigate to your project directory, and run multiple git commands.

Xcode 9 makes working with GitHub much easier. You can add your Xcode project to GitHub without leaving Xcode.

Start by going to the source control navigator by choosing View > Navigators > Show Source Control Navigator.

SourceCodeNavigatorAtStart

Select the Remotes folder, right-click, and choose Create Remote on GitHub. A sheet similar to the following will open:

CreateGitHubRemoteBlur

Unless you’ve already configured a GitHub account in Xcode, choose Add a GitHub Account from the Account menu.

GitHubLogin

Enter your GitHub account name and password. Click the Sign In button to use your GitHub account for this repository.

Choose public visibility unless you have a paid GitHub account. You must have a paid account to create private GitHub repositories.

Click the Create button to create the remote branch. Your Xcode project is now on GitHub.

Learn More About Version Control

Facebooktwittergoogle_plusredditmail

Handling Swipe Gestures in a SpriteKit Game

February 16th, 2018

Filed under: Game Development, iOS Development | Be the first to comment!

When you create a SpriteKit Xcode project, the GameScene.swift file contains functions to handle touch and/or mouse events, depending on the type of SpriteKit project you create. You can also use swipe gestures in SpriteKit games. Supporting swipe gestures requires you to perform two tasks.

The first task is to create a swipe gesture recognizer. The swipe gesture recognizer takes two arguments: a target and a selector (function) to call when someone performs the gesture. Set the swipe gesture recognizer’s direction and add it to the view. The following code demonstrates how to create a swipe gesture recognizer for swiping right:

override func didMove(to view: SKView) {
    let swipeRight = UISwipeGestureRecognizer(target: self, 
        action: #selector(GameScene.swipeRight(sender:)))
    swipeRight.direction = .right
    view.addGestureRecognizer(swipeRight)
}

The second task is to write a function to handle the swipe.

@objc func swipeRight(sender: UISwipeGestureRecognizer) {
    // Handle the swipe
}

The functions to handle the swipes need the @objc at the start because they’re selectors that access the Objective-C runtime.

Facebooktwittergoogle_plusredditmail

Creating a Git Repository for an Existing Xcode Project

February 12th, 2018

Filed under: Version Control, Xcode | Be the first to comment!

When you create a new Xcode project, Xcode gives you the option to create a git repository on your Mac.

CreateGitRepoNewProject

How do you create a git repository for a project after the project has been created?

Choose Source Control > Create Git Repositories. A sheet opens.

Xcode9AddGitRepoExistingProject

Click the Create button and Xcode will create a git repository for your project.

Learn More About Version Control

Facebooktwittergoogle_plusredditmail

Using agvtool to Update Version and Build Numbers

February 5th, 2018

Filed under: Xcode | Be the first to comment!

Apple bundles a command-line tool with Xcode, agvtool, to automatically update your project’s version and build numbers. This article shows you how to use agvtool in your Xcode projects.

Enabling agvtool in Your Project

Before you can use agvtool, you must tell Xcode to use it for your project. To enable agvtool, change the following build settings:

  • Set the Versioning System build setting to Apple Generic.
  • Give the Current Project Version build setting an initial value. Agvtool works best with integer build numbers. 1 is a good choice.

AgvtoolProjectSettings

Updating the Build Number

Because agvtool is a command-line tool, you have to run it from the Terminal application. Navigate to your project’s directory before running agvtool.

The most common thing you will be doing in agvtool is incrementing the build number. Run agvtool with the next-version -all option to increment the build number.

agvtool next-version -all

To set the build number to a specific value, supply the desired build number. The most common reason for explicitly setting a build number is to use non-integer build numbers. The following example sets the build number to 8.2:

agvtool next-version -all 8.2

The what-version option lets you see the current build number.

agvtool what-version

Updating the Version Number

To update the version number of your project, run agvtool with the new-marketing-version option and supply the version number. The following example sets the version to 0.5:

agvtool new-marketing-version 0.5

The what-marketing-version option lets you see the current version number.

agvtool what-marketing-version
Facebooktwittergoogle_plusredditmail