Showing posts with label iOS. Show all posts
Showing posts with label iOS. Show all posts

Xcode iPhone beginner projects with GitHub integration

I decided to follow a different path to learn software development for the iPhone - instead of online tutorials and Apple docs I got a book. I postponed my desire to learn but it’s time to revive it. I grabbed a beginner’s book on the subject: A Beginner's Guide to iOS SDK Programming by James A. Brannan & Black Ward. This book covers iOS 4.2 + Xcode 4. iOS 5 is on the verge of being released…Smiley confuso

I had to download the recent Xcode and its accompanying SDK bits again ( 3.17 GB ) as the ones I had installed were out of date (from September 2010) Smiley pensativo. It was just a matter of hitting Mac App Store and looking for Xcode. The download has everything you need to install to be able to follow the book samples.

As I started creating the sample projects in Xcode I thought it’d be an excellent opportunity to store these samples in an online repository ( repo ) with source code control for further reference and to share/allow the beginner developer to download and study all the samples. It’s also a good chance I have to play with Git since I’ve been using Subversion during the last years.

This post covers the basics to integrate Xcode with GitHub for the Mac OS user. GitHub is a web-based hosting service for software development projects that use the Git revision control system.

I try to synthetize lengthy and scattered docs you find on the subject and provide links to key docs and posts…

I learned how to use Xcode Organizer to integrate my online GitHub repository with Xcode built in support for software control management and started sending the projects to GitHub right after the second book sample. It’s better to start early or you’ll never do it!

This article in Mac OS Developer Library: Managing Versions of Your Project has everything you need to configure your project to use it with Git.

When you create an online/remote repository in GitHub you get instructions on how to set up the repo as getting a copy of the repo to work locally in your computer or sending an existing project to the remote repo. This help article from GitHub clarifies some things: Set up Git in Mac OS.

This post has the steps you have to follow to get a GitHub repo to work with Xcode projects: Version Control System with XCode 4 and Git Tutorial.

You’ll have to use Mac OS Terminal to type some commands. Nothing difficult.
As a matter of fact you should familiarize yourself with Terminal if you haven’t yet. The real fun is when you play with git commands in Terminal (take for example the powerful rebase command). Later in this post I’m going to use the support offered by Xcode which has UI for basic git commands as commit, push, pull, merge, etc - but Xcode doesn’t give you the power of the full set of git commands that are only available through the command line.

These are the Terminal commands I typed to send the book’s initial sample projects (QuickStart and C Main Project) to my remote repository located at GitHub: https://github.com/leniel/iPhone-Beginner-Guide
Take a special look at the highlighted commands:

Last login: Fri Aug 19 19:29:32 on ttys001
Leniel-Macaferis-Mac-mini:~ leniel$ cd /
Leniel-Macaferis-Mac-mini:/ leniel$ cd iPhone
Leniel-Macaferis-Mac-mini:iPhone leniel$ cd Local
Leniel-Macaferis-Mac-mini:Local leniel$ ls
C Main Project                iPhone Beginner's Guide.xcworkspace
QuickStart
Leniel-Macaferis-Mac-mini:Local leniel$ cd QuickStart
Leniel-Macaferis-Mac-mini:QuickStart leniel$ ls
QuickStart        QuickStart.xcodeproj
Leniel-Macaferis-Mac-mini:QuickStart leniel$ git remote add origin git@github.com:leniel/iPhone-Beginner-Guide.git
Leniel-Macaferis-Mac-mini:QuickStart leniel$ git push -u origin master
Counting objects: 16, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (16/16), 8.27 KiB, done.
Total 16 (delta 2), reused 0 (delta 0)
To git@github.com:leniel/iPhone-Beginner-Guide.git
* [new branch]      master -> master
Branch master set up to track remote branch master from origin.

Leniel-Macaferis-Mac-mini:Local leniel$ cd "C Main Project"
Leniel-Macaferis-Mac-mini:C Main Project leniel$ ls
C Main Project            C Main Project.xcodeproj
Leniel-Macaferis-Mac-mini:C Main Project leniel$ git push -u origin master
To git@github.com:leniel/iPhone-Beginner-Guide.git
! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:leniel/iPhone-Beginner-Guide.git'
To prevent you from losing history, non-fast-forward updates were rejected.
Merge the remote changes (e.g. 'git pull') before pushing again.
  See the 'Note about fast-forwards' section of 'git push --help' for details.
Leniel-Macaferis-Mac-mini:C Main Project leniel$ git pull origin master
warning: no common commits
remote: Counting objects: 16, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 16 (delta 2), reused 16 (delta 2)
Unpacking objects: 100% (16/16), done.
From github.com:leniel/iPhone-Beginner-Guide
* branch            master     -> FETCH_HEAD
Merge made by recursive.
QuickStart.xcodeproj/project.pbxproj             |  288 ++++++++++++++
QuickStart/QuickStart-Info.plist                 |   38 ++
QuickStart/QuickStart-Prefix.pch                 |   14 +
QuickStart/QuickStartAppDelegate.h               |   19 +
QuickStart/QuickStartAppDelegate.m               |   73 ++++
QuickStart/QuickStartViewController.h            |   13 +
QuickStart/QuickStartViewController.m            |   44 +++
QuickStart/en.lproj/InfoPlist.strings            |    2 +
QuickStart/en.lproj/MainWindow.xib               |  444 ++++++++++++++++++++++
QuickStart/en.lproj/QuickStartViewController.xib |  156 ++++++++
QuickStart/main.m                                |   17 +
11 files changed, 1108 insertions(+), 0 deletions(-)
create mode 100644 QuickStart.xcodeproj/project.pbxproj
create mode 100644 QuickStart/QuickStart-Info.plist
create mode 100644 QuickStart/QuickStart-Prefix.pch
create mode 100644 QuickStart/QuickStartAppDelegate.h
create mode 100644 QuickStart/QuickStartAppDelegate.m
create mode 100644 QuickStart/QuickStartViewController.h
create mode 100644 QuickStart/QuickStartViewController.m
create mode 100644 QuickStart/en.lproj/InfoPlist.strings
create mode 100644 QuickStart/en.lproj/MainWindow.xib
create mode 100644 QuickStart/en.lproj/QuickStartViewController.xib
create mode 100644 QuickStart/main.m
Leniel-Macaferis-Mac-mini:C Main Project leniel$ git push -u origin master
Counting objects: 14, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (12/12), done.
Writing objects: 100% (13/13), 3.95 KiB, done.
Total 13 (delta 3), reused 0 (delta 0)
To git@github.com:leniel/iPhone-Beginner-Guide.git
   05ec270..fe84a7a  master -> master
Branch master set up to track remote branch master from origin.
Leniel-Macaferis-Mac-mini:C Main Project leniel$

Great! With the above commands I have sent both projects to my repo located at GitHub.

It’s important to note that for each project I created in Xcode I selected the option to create a local git repository as shown in Figure 1:

Xcode - Selecting Create local git repository for this projectFigure 1 - Xcode - Selecting Create local git repository for this project

With this in place I can now safely delete my local copy of both projects (folder Local I used above in Terminal commands) and work directly with the code of my remote repository. Let’s do it:

Open Xcode Organizer selecting the menu Window => Organizer:

Organizer window accessible through Xcode’s Window menuFigure 2 - Organizer window accessible through Xcode’s Window menu

I suppose you have already configured and added (+ button in the bottom left of Figure 2) your GitHub repo (green circle in Figure 2) to the Organizer following the docs I linked above.

To get a local working copy of your remote repository you must click the Clone button (see bottom part of Figure 2) and choose a location to place the repo files. After doing this you’ll get a new repo (mine is located in the folder /iPhone/iPhone-Beginner-Guide as you see in Figure 2). When I click my local copy of the repo I get this beautiful screen where I can see commit comments and changes I made to each file along the way (click to enlarge):

Local ( Clone ) copy of my online GitHub repository seen in Xcode OrganizerFigure 3 - Local ( Clone ) copy of my online GitHub repository seen in Xcode Organizer

Now it’s just a matter of working and modifying the project files or adding new projects and commit them to the repository through the menu File => Source Control => Commit…

One more important note is: when you commit something, it’s just committed in your local copy. You need one additional step: push the changes to GitHub. In Xcode you can select the file(s) or project(s) you want and go to File => Source Control => Push… For more on this, read: Commit Files to Add Them to a Repository.

In my case, when I select Push I get this Xcode dialog where I can select the Remote endpoint (GitHub repository) to which my committed files will go:

Xcode Push user interface and GitHub remote locationFigure 4 - Xcode Push user interface and GitHub remote location

As a bonus I created a Workspace as seen in Figure 5 to have all the sample projects at hand in a single Xcode window. The workspace has references to the projects and work somewhat like Microsoft Visual Studio’s solution file if you’re used to Microsoft developer tools. The workspace helps a lot during the commit and push tasks!

Xcode workspace with projects at left side paneFigure 5 - Xcode workspace with projects at left side pane

Well, I’m new to this new Xcode world and I think I’ll learn a lot from these simple sample beginner projects.

The next thing I'm gonna do is learn what file types I can ignore when committing… Thanks to StackOverflow there’s already a question addressing this very topic: Git ignore file for Xcode projects

Edit: following the advice of the above StackOverflow question, I added a .gitignore file to the repo.

Hope this helps.

Learning to develop for iPhone with a Mac mini

I think you should read this one too:
Productivity and happiness going from 4 to 8 GB RAM

My 2010 Unibody Mac mini arrived on Sep 9. Now it's time to get to coding for the iPhone. That's why I bought it after all. Go code guy instead of being here writing this somewhat long post. :) Go figure. Oh wait, I need to share my experience with the rest of the world so that I can contribute to people that like to do what I do, that is, software development. So, let's share and learn together.

In this post I’ll write about hardware, software, virtual machines and iPhone development. All this takes into consideration my Windows background and my need of having Windows around while I’m using the Mac.

New hardware
I needed new hardware to learn how to develop for the iPhone platform. My Windows PC is an "old" Intel DualCore Pentium E2180 2 GHz and so I couldn't get MAC operating system also known as Mac OS X running on this hardware because the processor lacks Intel VT-x (hardware virtualization) feature. VT-x is necessary if you plan to run Mac OS in your Windows PC hardware through a virtual machine. I needed Mac OS X because currently iPhone development can only be done the proper way (read with Apple official development tools) within the Mac operating system.

I didn't want to spend a lot of money buying a new computer. So I opted for the bare minimum necessary to develop for iPhone.

While buying the mini that comes with a “somewhat” powerful Core 2 Duo P8600 2.4 GHz processor I also bought the Magic mouse and the Wireless keyboard. Mini doesn't come with a mouse or keyboard. I customized Mini with 4 GB RAM¹. Memory customization made the shipping take 2 weeks more. I bought Mini at the Brazilian Apple online store. Because I customized Mini it shipped from China and that's a long way to Brazil. If I had opted for the standard 2 GB RAM version I'd get the Mini the week I placed the order since there's stock right here in Brazil.

All this wait was to get a new development box that could make me capable of developing for iPhone and that's cheap for my pocket.

¹ - It's OK to work with 4 GB of RAM, but if you can afford 8 GB of RAM, go for that. Don't think twice. NVidia graphics card that comes with Mini also gets 256 MB of RAM. Virtual machines also like RAM. Continue reading to know why I'm mentioning this.

Mac OS Activity Monitor showing the System Memory
Figure 1 - Mac OS Activity Monitor showing the System Memory

To better understand what those Wired, Inactive memory are, read this doc:
Mac OS X: Reading system memory usage in Activity Monitor

The quality of Apple products is undeniable! I also own an iPhone and it makes me second that.

Things I really appreciate about the Mac mini set (unordered):
- Being wireless with Apples’s magic mouse and wireless keyboard. I think they're a must have with Mini.
- No noise approach when it comes to working with computers. Mini makes no noise. The only sound you'll ever listen is the hard disk and you'll only listen to it when there's no other environment sound, for example, during the night. If you opt for a SSD drive you won't get any noise at all. SSD is still expensive so that was not an option for me.
- HDMI port that allows me to connect it to my 32'' Sony Bravia HD TV for crystal bright images.
- Sound output quality (it's better than my dedicated Creative Audigy sound card on the PC)
- Small size. See the photo bellow where Mini’s size is compared with my Playstation 3 joystick (photo taken with iPhone 3GS):

Mac mini's size compared with a Playstation 3 joystick  
Figure 2 - Mac mini’s size compared with a Playstation 3 joystick

Mac + Windows
As a user of the PC movement for the past 15 years (I started with Windows 3.1 in 1995) I got it going really fast with Mac. The transition is being straightforward.

I could for sure only use Mac OS but that wouldn't give me the power I need. As I still work with Windows only applications as is the case of Microsoft Visual Studio and Windows Live Writer (that I use to write these posts) I need a way of having Windows around. For that I got Parallels Desktop. This software enabled me to have a Windows 7 virtual machine running alongside Mac OS with a beautiful integration between the two operating systems.

Virtual machine
Remember the amount of RAM memory I mentioned and showed above? It plays a big part when you’re using virtual machines because the system RAM is shared with virtual machines. So if you plan to run virtual machines in your Mac computer, you’d better get more RAM. This will give you better performance (read responsive applications) while you work avoiding the infamous Page outs.

Using a virtual machine there’s no need to reboot the machine to go to other operating system – this is cumbersome (if using Boot Camp and installing Windows in other partition I would have to do that). Actually I first installed Windows 7 using Boot Camp but then I realized that Parallels could move that Windows 7 Boot Camp partition over to a virtual machine. So I decided to go this way and used my Boot Camp partition as a virtual machine.

After going to Boot Camp solution and then using a Boot Camp partition as virtual machine I saw that Parallels Desktop also allows us to move an existing Windows PC entirely to a Mac based computer. This means that all the software, configurations and files you have on your Windows PC disk partition can be migrated to a virtual machine. I tried this but it just didn't work for me. The migration that is done using Parallels Transporter Agent failed twice in a process that takes at least 5 hours to complete. In my case it tried to migrate 197 GB of data over my wired home network. The error message I got was that the Transporter migration failed/The migration could not be completed (no me info). I tried to run this resolution but that didn’t do the trick (no problems were found on my Windows PC). If it had worked for me I wouldn’t need to install anything as everything that I had on the Windows PC would be just copied over to the virtual machine. That’d freed me of having to install everything again on the virtual machine.

No problem at all as I created a brand new virtual machine with a clean install of Windows 7.

You see, all this work allowed me to learn a lot of things along the way… Things that I wouldn’t have tried.

Tip: I found that using a new virtual machine with a clean install of Windows instead of a Boot Camp partition as a virtual machine is really better. Windows runs faster this way.

Now, the only thing I have to do is to start the virtual machine and there I'm using Windows 7 just inside Mac OS as if I were using the PC. So far so good. This satisfies my needs as a multiplatform developer.

You can put your Mac mini to sleep and you'll never have to start the virtual machine again because while sleeping Mini keeps the memory in "On" state, which means that all applications that you have open will be kept in the same state they were before you put Mini to sleep. Isn't it good? It's a kind of Hibernate we have in Windows but it's not Hibernate at all. While Mini is sleeping you get a beautiful white glowing blinking light that shows you that it's sleeping of course.

Mac software
Applications look more polished in Mac OS. Better native windows design, beautiful icons, etc. Some things are simpler in Mac OS. Take for example the application installation process. In most cases you just copy the application file to your Application folder in the dock and you’re ready to use it.

Some Windows software I use and that I can't live without are available for Mac too:
- µTorrent
- Firefox
- Microsoft Office
- Babylon
- Eclipse
- SnagIt

Mac only software that I'm using:
- TextMate
- BBEdit
- QuickSilver
- UnRarX
- Pixelmator

I'm using keyboard shortcuts a lot more with Mac.

iPhone development
Before I could feel comfortable to start learning how to develop for iPhone I had to configure my work environment first and that’s why I had to go over things such as virtual machines, etc. Now that it’s done it’s time to engage!

For those that are starting in this new world just like me, here goes what you need to get going and coding:

1st - get the official iPhone SDK that stands for software development kit. You’ll have to create an account with Apple to be able to download it and use all the features that Apple iOS Dev Center has to offer. The SDK is a big download - for the current version (Xcode 3.2.4 and iOS SDK 4.1) its size is: 2.9 GB.

Xcode is the name of the the IDE used to develop applications for iPhone. iOS is the name of the operating system that iPhone uses.

2nd - learn how to develop in this platform and that’s totally a personal path you have to follow. After learning the basics you can follow the more diverse paths depending on what types of application you want to develop.

As for learning iPhone development I'm understanding how things work with some easiness. It's always good to learn new programming languages as Objective-C, a new development environment as Xcode, etc. The easiness I'm finding here is a result of some years of coding and my familiarity with the Model View Controller pattern that is extensively used in iPhone applications.

The main learning source has been the iOS Reference Library.

Tutorials I have followed ²:

Your first iOS Application
Core Data Tutorial for iOS: Introduction
Introduction to The Objective-C Programming Language

² - I'll try to update this list while I make progress.

Updated on 8-26-2011

As I explain in a recent post titled Xcode iPhone beginner projects with GitHub integration, I decided to follow a new path to learn iPhone development. I’m learning with sample projects taken from a book and I’m committing those projects to a GitHub repository. Feel free to clone the repo: https://github.com/leniel/iPhone-Beginner-Guide

Final note
This is a new world that opens up just in front of me. In the coming weeks and months I'll be immersed in this new world.

I won't let my Windows developer part behind because I'm always answering some questions at StackOverflow and every now and then I need to write some apps to test some code.

I really love programming. Any programming language that you learn opens a new door of possibilities. This is what motivates me and moves me forward. I'm thirsty for knowledge.

Let's see what the future reserves for me in this new endeavor.

If you're also planning to develop for iPhone in the near future (because things change really fast in the technology world) this post shows you a possible start point.

Hope you've found this an interesting read and that you can learn something from my own experience.