Join Steven Lipton for an in-depth discussion in this video Use Xcode Git repositories, part of iOS Development Tips Weekly.
- [Instructor] In Xcode 9, Apple made some significant changes concerning repositories in GitHub. So, I'm going to explore repositories in Xcode and how to move them directly to GitHub without using the GitHub website or desktop application. Not everyone understands Git and GitHub. If you want to learn more, check out the course Learning Git and GitHub. I'm going to gloss over that topic here. We'll start with a new Xcode project. Before we do, head over to Preferences and Source Control and make sure that Source Control is enabled.
Close Preferences. Head to File, New, Project. We'll pick an iOS project of Game, since there's already usable SpriteKit code there. If you're not familiar with SpriteKit code, don't worry. I'll walk you through what changes we'll make. Click Next. For a project name, call it RepoDemo. Set your team, organization name, and organization identifier as you usually would. Set the language to Swift, and we're going to use SpriteKit for the game technology.
Go ahead and hit Next. I'm going to save it to the desktop and most important of all here is make sure where it says Source Control you have a check next to Create Git repository on my Mac. This will let you have the source control capabilities we're interested in. Go ahead and click Create, and the project appears in Xcode. Run the app so you can see it is an animated Hello, World app. Click your mouse on the app, and the text pulses with a small square with rounded corners rotating.
Stop the app. Click on the GameScene.swift file. Give yourself some room by closing the Attributes if it's open. This code has two nodes, the shape and the label. The shape is generated here in code; the label in a GameScene.sks file. We'll change both of these in the GameScene.swift code. Under the if let label = self.label at line 21, I'm going to change the text to Hello Pizza and the fontName to Georgia.
Then I'll scroll on down to line 29 and put right underneath that let w = another line and I'm going to call it let h = w * 2.0. This'll give us a height. The original code actually has both a width and a width so it stays square, but I'm going to make this oblong to make it twice as high as it is wide. And then underneath that you'll see this shape node here.
And it starts with rectOf. Change rect to ellipse, okay? Change the height to h, and this only has that one parameter; the cornerRadius doesn't apply. So delete the cornerRadius. So that's our changes to our code. We can go ahead and run that. And we get a different experience. Okay, and close that up.
And take a look at this little M over here. This says that it's a modified file. You can see what the modifications are by going over to the version editor, clicking it once, or clicking Comparison, and you'll see a screen that has two sides of code, one the old code, one the new code. In between, you'll see things with little numbers. So, for example, here we have the one for the shape node and it's got two changes. I can discard those changes if I want, which I'm not going to do.
And if you go up a little further, you can see one change because they were blocked together of the two changes to the label. You'll also find at the top of the screen a menu for source control. And here you've got all the classic things that you'll find for Git or GitHub, where you've got Commit, Push, Pull, Fetch and Refresh, etc. And, of course, Clone on the bottom. Go ahead and hit Commit and you get a new window with the listed change files over here on the side.
If you click on the GameScene.swift file, which is the only change we've got, you'll see again, it'll show you your changes. On the bottom, you'll find space for a commit message, and I'm going to change to an oval shape and change the message to Hello Pizza. I can now go down to the bottom here, which is Commit 1 File and commit the files. You'll find another little icon next to the Navigator icon, which is over here.
This is the Source Control Navigator, and you can open up its box and open up Branches, and you can see under Branches there's a master branch. And you can see the initial commit and our commit where we changed the shape. Besides storing your project locally in Xcode, starting with Xcode 9, you can store and share your project in GitHub. You'll need an account from github.com if you don't already have one. Once you have your GitHub account, in Xcode, go to Preferences, Accounts.
Click the plus and select GitHub. Click Continue. Once selected, add your account and password. Mine is MakeApp, and I'll type in my password. You'll see you now have a Source Control account. You can go ahead and close the preferences. Right-click on Remotes, go to Create "RepoDemo" Remote on GitHub, then you get a creation screen. Set your account and the owner, the repository name, and a description, which I'll put, "An Example Repository from Xcode." You can set it to public or private, and the remote name.
Now you can change the repository name if you don't want to call it RepoDemo. So, for example, I'm going to change it to my exercise name here, and hit Create. I can go over to GitHub now, check my repositories, and right there is Tips_01_GitHub. So this is how you save your project into GitHub. Once there, this another GitHub project and acts like any other GitHub project. As of now, you can't do this to playgrounds.
For a project like the one we just made, that's how you set up integration with GitHub from an Xcode repository.