<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Code Perspective</title>
	<atom:link href="http://codeperspective.ca/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeperspective.ca</link>
	<description>Coding is awesome</description>
	<lastBuildDate>Wed, 25 Jan 2012 20:51:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Installing Rails 3.2 with RVM</title>
		<link>http://codeperspective.ca/2012/01/25/installing-rails-3-2-with-rvm/</link>
		<comments>http://codeperspective.ca/2012/01/25/installing-rails-3-2-with-rvm/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 20:21:52 +0000</pubDate>
		<dc:creator>Andrew Hanna</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=278</guid>
		<description><![CDATA[RVM (Ruby Version Manager) is a great tool that lets you install multiple versions of Ruby, Gems (Rails, etc.) all inside your home directory, so that it lives only within your user&#8217;s account and does not affect any system files at all. You can also create gemsets to maintain separate repositories of gems for different [...]]]></description>
			<content:encoded><![CDATA[<p>RVM (Ruby Version Manager) is a great tool that lets you install multiple versions of Ruby, Gems (Rails, etc.) all inside your home directory, so that it lives only within your user&#8217;s account and does not affect any system files at all. You can also create gemsets to maintain separate repositories of gems for different applications.</p>
<p><strong>NOTE</strong>: You should never run any of these RVM related commands as <code>sudo</code>. All of these files will live in your home directory and should not be owned by <code>root</code> or any other user. If you <code>sudo</code> any of these commands, you will create lots of permission problems that will haunt you in the future.</p>
<h3>Install RVM</h3>
<p>To start us off, let&#8217;s install RVM. The official installation instructions have you run a shell script directly off their website. I was a little sketchy about this at first, but it makes the installation so ridiculously simple that I didn&#8217;t care.</p>
<h6>Install RVM</h6>
<pre>
$ bash -s stable &lt; &lt;(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
</pre>
<p>After the install is completed, you&#8217;ll need to source your .bashrc/profile/etc. file to bootstrap the RVM system (the exact file it was added to is shown in the instructions output by the installer). Or you can just open a new terminal.</p>
<h6>Load RVM system</h6>
<pre>
$ . ~/.bash_profile
</pre>
<h3>Installing some prereqs</h3>
<p>Depending on what Linux/UNIX flavor you&#8217;re using, you might need some additional packages before moving on (namely OpenSSL and iconv). Let&#8217;s install those now:</p>
<pre>
$ rvm pkg install openssl
$ rvm pkg install iconv
</pre>
<h3>Install Ruby</h3>
<p>There&#8217;s a good chance you already have Ruby installed on your OS, however, it will be the system-wide version in <code>/usr/local</code> or somewhere similar:</p>
<h6>Check Ruby Install</h6>
<pre>
$ which ruby
/usr/local/bin/ruby
$ ruby -v
ruby 1.8.7 (2010-04-19 patchlevel 253) [i686-linux], MBARI 0x8770, Ruby Enterprise Edition 2010.02
</pre>
<p>Let&#8217;s install Ruby 1.9.3 now:</p>
<pre>
$ rvm install 1.9.3 -C --with-openssl-dir=$HOME/.rvm/usr,--with-iconv-dir=$HOME/.rvm/usr
</pre>
<p>This step will take some time as it actually downloads the Ruby source, configures it and compiles it. When it&#8217;s done installing, we can set ruby 1.9.3 as the default version by running:</p>
<pre>
$ rvm use 1.9.3 --default
</pre>
<h6>Install Rails</h6>
<p>Now that Ruby 1.9.3 is fully installed, we can go ahead and install Rails 3.2 using RubyGems. For speed we will disable documentation:</p>
<pre>
$ gem install rails --no-rdoc --no-ri
</pre>
<h3>Create a Rails App</h3>
<p>We should have everything installed that we need to get started with an actual Rails app.</p>
<h6>Create new app</h6>
<pre>
$ rails new helloworld
</pre>
<p>Creating a new Rails app will create the skeleton folder structure for you, start off with a basic gem set (located in Gemfile), and then run <code>bundle install</code> for you. The <code>bundle install</code> command installs all dependent gems from your Gemfile in your gem repository.</p>
<p>Let&#8217;s actually make some changes to your new Rails app and test it out.</p>
<h6>Create index Controller</h6>
<pre>
$ cd helloworld
$ rails generate controller index
$ vi app/controllers/index_controller.rb
</pre>
<h6>Add the index action</h6>
<pre>
class IndexController &lt; ApplicationController
  def index
    @hello = 'World'
  end
end
</pre>
<h6>Add index view template</h6>
<pre>
$ vi app/views/index/index.html.erb
</pre>
<pre>
&lt;h1&gt;Hello &lt;%= @hello %&gt;!&lt;/h1&gt;
</pre>
<h3>Start the Rails server</h3>
<p>For development purposes, most developers just use the built-in Rails server. This can be executed by calling <code>rails server</code> (or <code>rails s</code>). This runs a local server that will listen on port 3000.</p>
<p>If you try running <code>rails s</code> now, you will likely get an error complaining about the lack of a JavaScript runtime. Starting in Rails 3.1, a new asset pipeline is being employed. This pipeline adds features like automatically minifying JS/CSS as well as compiling CoffeeScript or SASS/LESS. In order for your Rails server to run, it needs a JS runtime to execute JavaScript within Ruby. Let&#8217;s install <code>therubyracer</code> to make <code>rails s</code> happy.</p>
<h6>Install <code>therubyracer</code> gem</h6>
<p>Open the <code>Gemfile</code> for your Rails app and <code>gem 'therubyracer'</code> anywhere in this file. After saving the <code>Gemfile</code>, let&#8217;s reinstall our gems with the bundler.</p>
<pre>
$ bundle install
</pre>
<p>This will install <code>therubyracer</code> gem for us. Now try executing <code>rails s</code> again, and you should have a running Rails app. Open the app in your browser: <code>http://localhost:3000/</code></p>
<h3>Clean Up App</h3>
<p>The default Rails app includes a placeholder <code>index.html</code> file. To finalize our setup, let&#8217;s remove that file and create our default route.</p>
<h6>Remove index.html file</h6>
<pre>
$ rm public/index.html
$ vi config/routes.rb
</pre>
<h6>config/routes.rb</h6>
<pre>
  root :to =&gt; 'index#index'
</pre>
<h3>All done</h3>
<p>You should be done now. If you refresh your browser, you should see <code>Hello World!</code></p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2012/01/25/installing-rails-3-2-with-rvm/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ajax simplified in Rails 3.1</title>
		<link>http://codeperspective.ca/2011/11/28/ajax-simplified-in-rails-3-1/</link>
		<comments>http://codeperspective.ca/2011/11/28/ajax-simplified-in-rails-3-1/#comments</comments>
		<pubDate>Tue, 29 Nov 2011 05:52:42 +0000</pubDate>
		<dc:creator>Mark Hazlett</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[3.1]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=274</guid>
		<description><![CDATA[So I'm working on a project and obviously I wanted to take advantage of the built in AJAX in rails using UJS. I for some reason couldn't find a good tutorial on how to do it so I figured I would just write one instead. Here's the basics of how I solved it. I'm going to assume a couple of things to start so that this tutorial stays within scope.]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;m working on a project and obviously I wanted to take advantage of the built in AJAX in rails using UJS. I for some reason couldn&#8217;t find a good tutorial on how to do it so I figured I would just write one instead. Here&#8217;s the basics of how I solved it. I&#8217;m going to assume a couple of things to start so that this tutorial stays within scope.</p>
<ul>
<li>A user model with an email address</li>
<li>jquery-rails gem</li>
</ul>
<p>To start we&#8217;re going to add the user to the view from the session. To do this in your controller add the following.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@user</span> = session<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> !@user
  redirect_to <span style="color:#996600;">'/login_path'</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Next we&#8217;re going to create the form, this is pretty simple but there are a couple of small things to watch out for. Here&#8217;s what it will look like.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= form_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;/signup/email&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:remote</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, :<span style="color:#CC0066; font-weight:bold;">format</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:js</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;%</span>= text_field_tag <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#996600;">''</span>, <span style="color:#ff3333; font-weight:bold;">:size</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">60</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
        <span style="color:#006600; font-weight:bold;">&lt;%</span>= submit_tag <span style="color:#996600;">&quot;Go!&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>So as you can see we&#8217;re adding a form to the view only if the user has not previously entered their email(unless @user.email). From there we create the form tag. There are 2 things to watch out for here. The first is :remote => true. This will tell rails that your form is going to be submitted via ajax and not via the regular redirection. The second is :format => :js. This will tell rails to use the respond to js block that we will discuss a little later on. As for a normal form the first argument is the controller/action path we want to take. For simplicity&#8217;s sake I just left it as a string(you could also use :controller/:action as well). Next we add a text field and a submit button and then close off our unless statement and out form block.</p>
<p>Now we need to add a signup controller so that the form has somewhere to submit the ajax request to. Do so by running the following command.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">rails generate controller signup email</pre></div></div>

<p>This will generate a brand new controller with a single action of email along with all the views/tests as well. Let&#8217;s open up our signup controller and add the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> email
  <span style="color:#0066ff; font-weight:bold;">@user</span> = session<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:user</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span>@user<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">unless</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">email</span>
      <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">email</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:email</span><span style="color:#006600; font-weight:bold;">&#93;</span>
        respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
            <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">save</span>
              <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">js</span>
            <span style="color:#9966CC; font-weight:bold;">else</span>
              <span style="color:#0066ff; font-weight:bold;">@user</span>.<span style="color:#9900CC;">email</span> = <span style="color:#0000FF; font-weight:bold;">nil</span>
              <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">js</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'error'</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
            <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Notice how we have a small piece of code in there(respond_to do |format|)? This code will tell rails which methods that it will respond to. In order for your method to respond to  an ajax call you need to have format.js inside of your respond_to block just like above. Next is the sweet part. We go if @user.save render the default view for rails. Now this is not a traditional erb file. Let&#8217;s go ahead and create email.js.erb inside of our signup views directory. This will tell rails to respond to the ajax call using js instead of a plain erb file.</p>
<p>Inside of the else we need to make sure that we reset the user&#8217;s email to nil so that the following ajax calls work. Then we render a partial called error. This is also going to be a UJS file called _error.js.erb. Inside of these files you can do whatever you want as far as javascript/ruby go. You pass variables to the view the same way and access them using ruby inside the js.erb file and you can also perform JS functions on DOM objects that are back on the page that made the AJAX request.</p>
<p>Hopefully this helps anyone reading on figuring out how to use UJS in rails 3.1. As always, if you have any questions/comments feel free to post them in the comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2011/11/28/ajax-simplified-in-rails-3-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding a link to an image in Rails 3.1</title>
		<link>http://codeperspective.ca/2011/10/29/adding-an-link-to-an-image-in-rails-3-1/</link>
		<comments>http://codeperspective.ca/2011/10/29/adding-an-link-to-an-image-in-rails-3-1/#comments</comments>
		<pubDate>Sun, 30 Oct 2011 01:10:53 +0000</pubDate>
		<dc:creator>Mark Hazlett</dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=269</guid>
		<description><![CDATA[So I had a small issue the other day learning how to add a link to an image so I figure I would share with everyone on how I accomplished it. The key is that the link_to object can take a block and then you can add an image to the block like this:]]></description>
			<content:encoded><![CDATA[<p>So I had a small issue the other day learning how to add a link to an image so I figure I would share with everyone on how I accomplished it. The key is that the link_to object can take a block and then you can add an image to the block like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'link/path/here'</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= image_tag <span style="color:#996600;">'image.png'</span>, :<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'someClass'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>It&#8217;s quick and it saves you a ton of time doing it manually!</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2011/10/29/adding-an-link-to-an-image-in-rails-3-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Zend_Auth to authenticate users in your web application</title>
		<link>http://codeperspective.ca/2011/08/13/using-zend_auth-to-authenticate-users-in-your-web-application/</link>
		<comments>http://codeperspective.ca/2011/08/13/using-zend_auth-to-authenticate-users-in-your-web-application/#comments</comments>
		<pubDate>Sun, 14 Aug 2011 05:55:02 +0000</pubDate>
		<dc:creator>Mark Hazlett</dc:creator>
				<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=263</guid>
		<description><![CDATA[So you're using Zend and you must be thinking, there has to be an easy way to authenticate users! Well, you can use Zend Auth and it makes it pretty damn easy. The basic concept is that it assumes that you have a combination of either a username/password, email/password or a combination of the two to authenticate your users. If you have something other than those combinations you may want to look at extending Zend_Auth instead of directly using it(that however it outside the scope of this tutorial). First off this tutorial assumes a couple things. One, that you know about the standard Zend Directory structure and two, that you have already setup a login form and are now working on authenticating the user. We'll start off in the index action of your LoginController. That way you have the url as /login. First off we'll check if the request is a post request:
]]></description>
			<content:encoded><![CDATA[<p>So you&#8217;re using Zend and you must be thinking, there has to be an easy way to authenticate users! Well, you can use Zend Auth and it makes it pretty damn easy. The basic concept is that it assumes that you have a combination of either a username/password, email/password or a combination of the two to authenticate your users. If you have something other than those combinations you may want to look at extending Zend_Auth instead of directly using it(that however it outside the scope of this tutorial). First off this tutorial assumes a couple things. One, that you know about the standard Zend Directory structure and two, that you have already setup a login form and are now working on authenticating the user. We&#8217;ll start off in the index action of your LoginController. That way you have the url as /login. First off we&#8217;ll check if the request is a post request:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Next we&#8217;ll use the &#8220;isValid&#8221; method call on your form. This uses the parameters you set when you created the form to validate each element. We pass in the post parameters into the isValid to do the verification like follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$loginForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will go inside of your isPost if statement. Next we&#8217;re going to grab the parameters that we want to validate with. In this case we&#8217;ll use the username/password combination. We&#8217;ll then grab an instance of the database adapter and create a Zend_Auth_Adapter_DbTable object that we pass in the database adapter to.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// get the username and password from the form</span>
<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$loginForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$loginForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Create the adapter and zend auth instance</span>
<span style="color: #000088;">$dbAdapter</span> <span style="color: #339933;">=</span> Zend_Db_Table<span style="color: #339933;">::</span><span style="color: #004000;">getDefaultAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$authAdapter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Adapter_DbTable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbAdapter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now is where the heavy lifting happens. We need to pass some items into the $authAdapter so that it knows what to use to authenticate the user. WE do that by passing it a tableName, an Identity Column, a Credential Column and a Credential Treatment like follows:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTableName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIdentityColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredentialColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredentialTreatment</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MD5(?)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// pass to the adapter the submitted username and password</span>
<span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredential</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this tutorial I&#8217;m just using an MD5 for the password however there are a plethora of other options available to use. Next we need to get our Zend_Auth instance and call the &#8220;authenticate&#8221; method while passing in the $authAdapter we created earlier. We then call the &#8220;isValid&#8221; method on the $auth adapter to return a boolean.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$authAdapter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now we know that the user has been authenticated. So there was a row in the database that matched the user&#8217;s credentials. We can now proceed to set the user session values and redirect them to the location you want them to go. We&#8217;re going to grab all the user&#8217;s information from the database(excluding their password) to set to the session.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// get all info about this user from the login table</span>
<span style="color: #666666; font-style: italic;">// ommit only the password, we don't need that</span>
<span style="color: #000088;">$userInfo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResultRowObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// the default storage is a session with namespace Zend_Auth</span>
<span style="color: #000088;">$authStorage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStorage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$authStorage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userInfo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$authNamespace</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Session_Namespace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend_Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$authNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/dashboard'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Finally we need to set an error message if the user enters invalid credentials. We do that in the else statement of the $result->isValid call.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Wrong username or password provided. Please try again.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>We can now use that error message in our view by going $this->view->errorMessage = $errorMessage. All together it looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Index Action to display the login form and
 * uses AJAX call to validate + authenticate the user
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> indexAction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// If we're already logged in, just redirect  </span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hasIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/dashboard'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
    <span style="color: #009900;">&#125;</span>  
&nbsp;
    <span style="color: #000088;">$request</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
    <span style="color: #000088;">$loginForm</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLoginForm</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #000088;">$errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$loginForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPost</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// get the username and password from the form</span>
            <span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$loginForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$loginForm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$dbAdapter</span> <span style="color: #339933;">=</span> Zend_Db_Table<span style="color: #339933;">::</span><span style="color: #004000;">getDefaultAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$authAdapter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Auth_Adapter_DbTable<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dbAdapter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setTableName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'User'</span><span style="color: #009900;">&#41;</span>
                	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIdentityColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'username'</span><span style="color: #009900;">&#41;</span>
                    	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredentialColumn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span>
                    	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredentialTreatment</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'MD5(?)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// pass to the adapter the submitted username and password</span>
            <span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span>
                    	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCredential</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$auth</span> <span style="color: #339933;">=</span> Zend_Auth<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">authenticate</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$authAdapter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	    <span style="color: #666666; font-style: italic;">// is the user a valid one?</span>
       	    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isValid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	        <span style="color: #666666; font-style: italic;">// get all info about this user from the login table</span>
	        <span style="color: #666666; font-style: italic;">// ommit only the password, we don't need that</span>
	        <span style="color: #000088;">$userInfo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$authAdapter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResultRowObject</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'password'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	        <span style="color: #666666; font-style: italic;">// the default storage is a session with namespace Zend_Auth</span>
	        <span style="color: #000088;">$authStorage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$auth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStorage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	        <span style="color: #000088;">$authStorage</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">write</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$userInfo</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	        <span style="color: #000088;">$authNamespace</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Session_Namespace<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend_Auth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	        <span style="color: #000088;">$authNamespace</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">username</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$username</span><span style="color: #339933;">;</span>
&nbsp;
	        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_redirect<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/dashboard'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    		<span style="color: #000088;">$errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Wrong username or password provided. Please try again.&quot;</span><span style="color: #339933;">;</span>
	    <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorMessage</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$errorMessage</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">view</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">loginForm</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$loginForm</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As always feel free to post any questions in the comments section below. </p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2011/08/13/using-zend_auth-to-authenticate-users-in-your-web-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a better singleton in Objective-C</title>
		<link>http://codeperspective.ca/2011/08/07/building-a-better-singleton-in-objective-c/</link>
		<comments>http://codeperspective.ca/2011/08/07/building-a-better-singleton-in-objective-c/#comments</comments>
		<pubDate>Sun, 07 Aug 2011 06:29:00 +0000</pubDate>
		<dc:creator>Mark Hazlett</dc:creator>
				<category><![CDATA[Iphone]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[dev]]></category>
		<category><![CDATA[objective-c]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=254</guid>
		<description><![CDATA[So I've found quite a few tutorials on writing singleton's in Objective-C online, however not many of them seem to be completely correct. The key to building a singleton in objective-c is synchronization. If you don't synchronize your threads then you might as well not build a singleton. Here is how it should be done...]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve found quite a few tutorials on writing singleton&#8217;s in Objective-C online, however not many of them seem to be completely correct. The key to building a singleton in objective-c is synchronization. If you don&#8217;t synchronize your threads then you might as well not build a singleton. Here is how it should be done&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> SharedManager<span style="color: #339933;">*</span> _sharedManager <span style="color: #339933;">=</span> nil<span style="color: #339933;">;</span></pre></div></div>

<p>Start by adding that line directly below your @implementation declaration.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Returns the shared instance
 */</span>
<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>SharedManager<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>sharedManager
<span style="color: #009900;">&#123;</span>
	@synchronized<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>SharedManager class<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>_sharedManager<span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>self alloc<span style="color: #009900;">&#93;</span> init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> _sharedManager<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> nil<span style="color: #339933;">;</span>    
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This will allow you to create a shared instance of your class. In order to instantiate your class, you can now call -</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #009900;">&#91;</span>SharedManager sharedManager<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Next we need to override alloc:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Overide alloc
 */</span>
<span style="color: #339933;">+</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>alloc
<span style="color: #009900;">&#123;</span>
	@synchronized<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#91;</span>SharedManager class<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		NSAssert<span style="color: #009900;">&#40;</span>_sharedManager <span style="color: #339933;">==</span> nil<span style="color: #339933;">,</span> @<span style="color: #ff0000;">&quot;Attempted to allocate a second instance of a singleton.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		_sharedManager <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>super alloc<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> _sharedManager<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> nil<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Overriding alloc will allow us to completely control the state of our singleton, just be careful, you are now responsible for EVERY part of this class including memory deallocation. Finally, we override init to allow for any initialization/instantiating to occur.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/**
 * Overide init
 */</span>
<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span>init <span style="color: #009900;">&#123;</span>
&nbsp;
	self <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>super init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>self <span style="color: #339933;">!=</span> nil<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	    <span style="color: #666666; font-style: italic;">// initialize stuff here</span>
        <span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> self<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can initialize any variables you need where the comment &#8220;initialize stuff here&#8221;. That&#8217;s it! That&#8217;s all you need, remember to put these methods declarations into the .h so that the rest of your application can see them properly. Now you can add methods to your heart&#8217;s content.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>someMethodCall
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Do some stuff here</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then to call your method all you have to do is the following:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>SharedManager sharedManager<span style="color: #009900;">&#93;</span> someMethodCall<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Remember to always add your method declarations to your header file and you&#8217;re finished.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2011/08/07/building-a-better-singleton-in-objective-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Zend Framework on a shared host</title>
		<link>http://codeperspective.ca/2011/04/30/using-zend-framework-on-a-shared-host/</link>
		<comments>http://codeperspective.ca/2011/04/30/using-zend-framework-on-a-shared-host/#comments</comments>
		<pubDate>Sat, 30 Apr 2011 19:57:57 +0000</pubDate>
		<dc:creator>Mark Hazlett</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=250</guid>
		<description><![CDATA[I recently got Zend Framework up and running on Media Temple's grid service. Turns out this isn't as easy as I originally thought. In order to get it to work properly i needed to add the following to the .htaccess file in the root of my domain directory.]]></description>
			<content:encoded><![CDATA[<p>I recently got Zend Framework up and running on Media Temple&#8217;s grid service. Turns out this isn&#8217;t as easy as I originally thought. In order to get it to work properly i needed to add the following to the .htaccess file in the root of my domain directory.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">AddHandler php5-script .php
&nbsp;
RewriteEngine On
&nbsp;
RewriteRule ^\.htaccess$ - <span style="color: #7a0874; font-weight: bold;">&#91;</span>F<span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
RewriteCond <span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>REQUEST_URI<span style="color: #7a0874; font-weight: bold;">&#125;</span> =<span style="color: #ff0000;">&quot;&quot;</span>
RewriteRule ^.<span style="color: #000000; font-weight: bold;">*</span>$ <span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>index.php <span style="color: #7a0874; font-weight: bold;">&#91;</span>NC,L<span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
RewriteCond <span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>REQUEST_URI<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #000000; font-weight: bold;">!</span>^<span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>.<span style="color: #000000; font-weight: bold;">*</span>$
RewriteRule ^<span style="color: #7a0874; font-weight: bold;">&#40;</span>.<span style="color: #000000; font-weight: bold;">*</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>$ <span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$1</span>
&nbsp;
RewriteCond <span style="color: #000000; font-weight: bold;">%</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>REQUEST_FILENAME<span style="color: #7a0874; font-weight: bold;">&#125;</span> <span style="color: #660033;">-f</span>
RewriteRule ^.<span style="color: #000000; font-weight: bold;">*</span>$ - <span style="color: #7a0874; font-weight: bold;">&#91;</span>NC,L<span style="color: #7a0874; font-weight: bold;">&#93;</span>
&nbsp;
RewriteRule ^public<span style="color: #000000; font-weight: bold;">/</span>.<span style="color: #000000; font-weight: bold;">*</span>$ <span style="color: #000000; font-weight: bold;">/</span>public<span style="color: #000000; font-weight: bold;">/</span>index.php <span style="color: #7a0874; font-weight: bold;">&#91;</span>NC,L<span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>This will properly re-route all of the requests to make some prettier url&#8217;s.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2011/04/30/using-zend-framework-on-a-shared-host/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Iphone Programming Part 2 &#8211; Intro to Views</title>
		<link>http://codeperspective.ca/2010/03/27/iphone-programming-part-2-intro-to-views/</link>
		<comments>http://codeperspective.ca/2010/03/27/iphone-programming-part-2-intro-to-views/#comments</comments>
		<pubDate>Sat, 27 Mar 2010 19:32:45 +0000</pubDate>
		<dc:creator>Mark Hazlett</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[XCode]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=235</guid>
		<description><![CDATA[Welcome to iphone programming part 2, introduction to views. In this tutorial I'll go over creating an application from scratch that transitions between 2 different views using the iphone's built in UINavigationController and the navigation stack. If you haven't done so already, I suggest reading through my first iphone article. If you already have a basic understanding of how to create an iphone application and hooking up IBOulets and IBActions in interface builder then you can head straight into this article. So let's jump into it, and as always if you have any questions feel free to leave a comment and I'll get back to you.]]></description>
			<content:encoded><![CDATA[<p>Welcome to iphone programming part 2, introduction to views. In this tutorial I&#8217;ll go over creating an application from scratch that transitions between 2 different views using the iphone&#8217;s built in UINavigationController and the navigation stack. If you haven&#8217;t done so already, I suggest reading through my first iphone article. If you already have a basic understanding of how to create an iphone application and hooking up IBOulets and IBActions in interface builder then you can head straight into this article. So let&#8217;s jump into it, and as always if you have any questions feel free to leave a comment and I&#8217;ll get back to you.</p>
<p>Let&#8217;s start out by opening up XCode and creating a new window based application from the standard iphone OS templates. Save the file to wherever you save your iphone files to and create.</p>
<p style="text-align: center;"><a href="http://www.codeperspective.ca/images/ViewTransitionDemo/createNewWindow.png"><img class="aligncenter" title="CreateNewWindowApplication" src="http://www.codeperspective.ca/images/ViewTransitionDemo/createNewWindow.png" alt="" width="287" height="217" /></a>Once you create and save your new window-based application, open up the classes and resources folders in the XCode folder explorer. Open up the ViewDemoAppDelegate.h and insert the following code&#8230;</p>
<p style="text-align: left;">

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">//  ViewDemoAppDelegate.h</span>
<span style="color: #666666; font-style: italic;">//  ViewDemo</span>
<span style="color: #666666; font-style: italic;">//</span>
<span style="color: #666666; font-style: italic;">//  Created by Mark Hazlett on 10-03-26.</span>
<span style="color: #666666; font-style: italic;">//  Copyright Code Perspective 2010. All rights reserved.</span>
<span style="color: #666666; font-style: italic;">//</span>
&nbsp;
<span style="color: #339933;">#import &lt;UIKit/UIKit.h&gt;</span>
@interface ViewDemoAppDelegate <span style="color: #339933;">:</span> NSObject
<span style="color: #009900;">&#123;</span>
    UIWindow <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
    UINavigationController <span style="color: #339933;">*</span>navController<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #339933;">,</span> retain<span style="color: #009900;">&#41;</span> IBOutlet UIWindow <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
&nbsp;
@end</pre></div></div>

<p>All we&#8217;re doing here is creating a UINavigationController object so that we can access it in our application delegate implementation file. Let&#8217;s go ahead and open up our ViewDemoAppDelegate.m and add the following code to the application did finish launching method.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>applicationDidFinishLaunching<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>UIApplication <span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>application
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Allocate and instatiate a new UINavigationController Object</span>
	navController <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>UINavigationController alloc<span style="color: #009900;">&#93;</span> init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Allocate and initialize a First View Controller Object</span>
	FirstViewController <span style="color: #339933;">*</span>firstViewController <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>FirstViewController alloc<span style="color: #009900;">&#93;</span>init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Push the first view controller onto the view controller stack without animation</span>
	<span style="color: #009900;">&#91;</span>navController pushViewController<span style="color: #339933;">:</span>firstViewController animated<span style="color: #339933;">:</span>NO<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Some memory management</span>
	<span style="color: #009900;">&#91;</span>firstViewController release<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Make it visible</span>
	<span style="color: #009900;">&#91;</span>window	addSubview<span style="color: #339933;">:</span>navController.<span style="color: #202020;">view</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Override point for customization after application launch</span>
    <span style="color: #009900;">&#91;</span>window makeKeyAndVisible<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The first thing we&#8217;re doing in this function is instantiating the UINavigationController that we declared in our header. We pass it an alloc and init to allocate the appropriate amount of memory for it as well as an init to initialize it properly. The next thing we&#8217;re going to do is create a new FirstViewController object. Now you may be asking what a FirstViewController object is? That&#8217;s because we haven&#8217;t created it yet. So let&#8217;s go ahead and go to &#8220;File &gt; New File&#8221; and add a new file that&#8217;s of the type UIViewController subclass. Make sure you check the little check box that says &#8220;With XIB for user interface&#8221;. This will give you an XIB file to work with instead of producing your interface programmatically. Now that we have our FirstViewControllerObject the only thing we have to do is import the header into the ViewDemoAppDelegate. So make sure to declare #import &#8220;FirstViewController.h&#8221; at the top of your code. Now onto the rest of the code in our application did finish launching method. The first thing we do with our allocated view controller is push it onto the navigation stack. Now because this is our first view we&#8217;re pushing onto the navigation stack we want to make sure to pass &#8220;NO&#8221; to the animated parameter because we don&#8217;t want to animate the root view when it&#8217;s pushed onto the stack. Now for some memory management. Once the FirstViewController is pushed onto the navigation stack the retain count on the object moves up to 2, therefore we can perform a release on the FirstViewController object so that the retain count stays at 1. Last thing we do in this method is add the view so our window so that it&#8217;s actually visible when we load the application.</p>
<p>Now we&#8217;re going to get into some interface builder stuff. Let&#8217;s start by opening up our FirstViewController.xib file in Interface Builder(or by just double clicking on it). You should see just a plain white view. Now in order to transition between views we need a button to press to actually transition between views. So let&#8217;s go ahead and add a UIButton to the view. Label the button anything you would like to to transition between views. Go ahead and save that file for now(we&#8217;ll come back to it later to connect the pointers). Next we&#8217;re going to create a new file in our project that&#8217;s a subclass of UIViewController. Again we&#8217;re going to make sure that &#8220;use XIB for user interface&#8221; is selected and press create. We&#8217;re going to name this view controller &#8220;SecondViewController&#8221; as it&#8217;s the one that we transition to from pressing the button on our first view controller. Once you create the new View Controller you should see it appear on the left hand side in your package explorer. Let&#8217;s go into our FirstViewController.h and add the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#import &lt;UIKit/UIKit.h&gt;</span>
@interface FirstViewController <span style="color: #339933;">:</span> UIViewController
<span style="color: #009900;">&#123;</span>
	IBOutlet UIButton <span style="color: #339933;">*</span>transitionButton<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>IBAction<span style="color: #009900;">&#41;</span> pushViewContoller<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> send<span style="color: #339933;">;</span>
&nbsp;
@end</pre></div></div>

<p>All we do here is create an IBOutlet for the UIButton we added to the XIB file and call it transition button. Then we create an IBAction method that will get called whenever the user presses inside the UIButton. Let&#8217;s open up our FirstViewController.m implementation file and implement the IBAction method like so&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#import &quot;FirstViewController.h&quot;</span>
<span style="color: #339933;">#import &quot;SecondViewController.h&quot;</span>
&nbsp;
@implementation FirstViewController
&nbsp;
<span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>IBAction<span style="color: #009900;">&#41;</span> pushViewContoller<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> send
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//Allocate and instantiate the second view controller object</span>
	SecondViewController <span style="color: #339933;">*</span>secondView <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span>SecondViewController alloc<span style="color: #009900;">&#93;</span> init<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Push the secondViewController onto the navigation stack</span>
	<span style="color: #009900;">&#91;</span>self.<span style="color: #202020;">navigationController</span> pushViewController<span style="color: #339933;">:</span>secondView animated<span style="color: #339933;">:</span>YES<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">//Some memory management</span>
	<span style="color: #009900;">&#91;</span>secondView release<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now as you can see, we first provide our implementation with an import of the SecondViewController.h that we created earlier. We then proceed to allocate and instantiate our SecondViewController object. This time we&#8217;re going to push it onto our navigation stack and passing the YES parameter to our animated option because this time we do want it to be be animated when we press the button. After that we just provide some basic memory management to bring the retain count of the SecondViewController object back down to 1. That&#8217;s al the necessary code we need right now, however if you run this program right now you&#8217;ll notice that if you press the button on the first view controller it won&#8217;t really do anything. So let&#8217;s open back up our FirstViewController.xib file and hook up our IBOutlet and IBAction to our button that we created. After you&#8217;re finished hooking up these actions in the File&#8217;s owner connections inspector, the file&#8217;s owner connections inspector should look something like this..</p>
<p><a href="http://www.codeperspective.ca/images/ViewTransitionDemo/completedConnectionInspec.png"><img class="aligncenter" title="Completed Connections Inspec" src="http://www.codeperspective.ca/images/ViewTransitionDemo/completedConnectionInspec.png" alt="" width="301" height="247" /></a>Now you should be able to run the application and press the button and see the transition to the second view controller(which will be blank unless you added something to the nib) and press the back button in the navigation controller and transition back to the first screen.</p>
<p>If you&#8217;re having some troubles, here is the project and all the code. <a title="View Demo Download" href="http://codeperspective.ca/ViewDemo.zip" target="_blank">Download</a></p>
<p>Also again, if you have any questions feel free to leave them in the comments section.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2010/03/27/iphone-programming-part-2-intro-to-views/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Installing Cocos2D for the iphone</title>
		<link>http://codeperspective.ca/2010/03/09/installing-cocos2d-for-the-iphone/</link>
		<comments>http://codeperspective.ca/2010/03/09/installing-cocos2d-for-the-iphone/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 21:34:36 +0000</pubDate>
		<dc:creator>Mark Hazlett</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=229</guid>
		<description><![CDATA[I have recently started working on some Iphone games only to discover a spectacular 2D game engine on the iphone called Cocos2D. After reading their very impressive feature list and some blogs on how Cocos2D works, I decided to try it out for myself. Low and behold however that it wasn't the easiest thing to install manually for iphone development. If you try and install cocos manually, you will run into countless errors and hassles while getting it up and running.]]></description>
			<content:encoded><![CDATA[<p>I have recently started working on some Iphone games only to discover a spectacular 2D game engine on the iphone called <a title="Cocos2D" href="http://cocos2d.org/" target="_blank">Cocos2D</a>. After reading their very impressive feature list and some blogs on how Cocos2D works, I decided to try it out for myself. Low and behold however that it wasn&#8217;t the easiest thing to install manually for iphone development. If you try and install cocos manually, you will run into countless errors and hassles while getting it up and running.</p>
<p>However, Cocos2D now sports a fancy shell script to get some Cocos2D templates up and running directly in Xcode with all the files pre-configured to start. To install these templates all you have to do is navigate to the cocos2D root directory on your filesystem using the terminal and then run this command.</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">./install_template.sh</pre></div></div>

<p>You can then open Xcode and you should see the following screen ready to use.</p>
<p style="text-align: center;">
<p style="text-align: center;"><a style="text-decoration: none;" href="http://codeperspective.ca/images/XCodePart1/cocos2d.png"><img class="aligncenter" title="Cocos2D" src="http://codeperspective.ca/images/XCodePart1/cocos2d.png" alt="" width="595" height="569" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2010/03/09/installing-cocos2d-for-the-iphone/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Iphone Programming Part 1 &#8211; Intro to Xcode and IB</title>
		<link>http://codeperspective.ca/2010/03/01/iphone-programming-part-1-intro-to-xcode-and-ib/</link>
		<comments>http://codeperspective.ca/2010/03/01/iphone-programming-part-1-intro-to-xcode-and-ib/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 23:40:01 +0000</pubDate>
		<dc:creator>Mark Hazlett</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Mac OS X Hints]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Interface Builder]]></category>
		<category><![CDATA[Iphone]]></category>
		<category><![CDATA[XCode]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=217</guid>
		<description><![CDATA[First off before we start, here are a couple of things you will need to get started: 1. Mac running the latest version of xcode &#8211; This can be found on your install disc for leopard or snow leopard. 2. Latest version of the iphone SDK &#8211; This can be found at developer.apple.com Now we [...]]]></description>
			<content:encoded><![CDATA[<p>First off before we start, here are a couple of things you will need to get started:</p>
<p>1. Mac running the latest version of xcode &#8211; This can be found on your install disc for leopard or snow leopard.</p>
<p>2. Latest version of the iphone SDK &#8211; This can be found at <a title="Apple Developer Connection" href="http://developer.apple.com" target="_blank">developer.apple.com</a></p>
<p>Now we can get started. Once you have xcode and the iphone SDK installed, open up xcode(Macintosh HD &gt; developer &gt; Applications). Once you have xcode installed, from the menu bar select file &gt; new project. You should see a windows like the this one.</p>
<p><a href="http://codeperspective.ca/images/XCodePart1/CreateNewProjectXcode.png"><img class="aligncenter" title="Create New XCode Project" src="http://codeperspective.ca/images/XCodePart1/CreateNewProjectXcode.png" alt="" width="716" height="641" /></a></p>
<p>Select window based application from the iphone application option on the left hand side of the window. Once you select the window based application, xcode will prompt you to save the project. I named my project &#8220;LabelTester&#8221; and saved it under my personal iphone projects directory. Once you save the project, an xcode window should open up and look something like this.</p>
<p style="text-align: center;"><a href="http://codeperspective.ca/images/XCodePart1/initialXcodeView.png"><img class="aligncenter" title="Initial XCode View" src="http://codeperspective.ca/images/XCodePart1/initialXcodeView.png" alt="" width="713" height="550" /></a></p>
<p>You should see a combination of files here in the browser in the code browser. The first should be the CoreGraphics.framework. This is just a set of files provided by Apple that have all the code relevant to the core graphics libraries in Cocoa. The second is the foundation.framework. This is the library set for the foundation framework which is basically all the classes like NSArray, NSString and other foundation objects. The next item should be LabelTester.plist. The plist, or preference list, is a set of XML attributes that control how the application is handled. For the sake of this tutorial, you won&#8217;t need to open the plist. The next item should be the LabelTester.app file, which is the executable for your application. Once your application is deployed, all files and resources in your project will be located inside your app&#8217;s .app file. The next file is the LabelTester.pch file. This file is the prefix header for your application. The LabelTesterAppDelegate.h file is the next file in the list. This file is the header file for your application delegate. This is the starting for your application, the app delegate is what&#8217;s run by your main.m file when you app is launched. The .h file is of course the header file, which is basically the interface file that the .m file will implement. The next file is of course the LabelTesterAppDelegate.m. This file is the implementation file for the app delegate header. Next is the main.m file. This file is the application launcher for your app. It basically runs the app delegate. The next file is the Main Window.xib file. Anything with a nib/xib file extension is classified as an interface builder file and you open these files in the Interface builder application. Finally, the UIKit.framework. This library is used to correspond to all the UI elements on the iphone(text fields, buttons, etc).</p>
<p>Let&#8217;s start by opening up our Main Window.xib to setup our interface. Start by dragging a button from the libraries window onto your main View. Rename that button by double clicking on the button and change it to say &#8220;Update Label!&#8221;. Next, grab a text field and drag it just above the button. Expand the text field to fit the majority of the screen. Select the textfield and go into the attributes tab and enter into the placeholder field, &#8220;Enter Text to update Label&#8221;. Once that&#8217;s finished, drag a label onto the main window and change the text from &#8220;label&#8221; to &#8220;Label to Update&#8221;. Once that&#8217;s done you can save the interface builder file by pressing (command + s) and switch your view back to your xcode project.</p>
<p><a href="http://codeperspective.ca/images/XCodePart1/IBIntroPage.png"><img class="aligncenter" title="IB Intro Page" src="http://codeperspective.ca/images/XCodePart1/IBIntroPage.png" alt="" width="707" height="710" /></a><a href="http://codeperspective.ca/images/XCodePart1/IBAddItems.png"><img class="aligncenter" title="IB Add Items" src="http://codeperspective.ca/images/XCodePart1/IBAddItems.png" alt="" width="400" height="582" /></a></p>
<p>Open up the LabelTesterAppDelegate.h file and change the file to reflect the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#import </span>
&nbsp;
@interface LabelTesterAppDelegate <span style="color: #339933;">:</span> NSObject
<span style="color: #009900;">&#123;</span>
    UIWindow <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
	IBOutlet UITextField <span style="color: #339933;">*</span>textField<span style="color: #339933;">;</span>
	IBOutlet UIButton <span style="color: #339933;">*</span>button<span style="color: #339933;">;</span>
	IBOutlet UILabel <span style="color: #339933;">*</span>label<span style="color: #339933;">;</span>
	IBAction <span style="color: #339933;">*</span>updateButtonPressed<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #339933;">,</span> retain<span style="color: #009900;">&#41;</span> IBOutlet UIWindow <span style="color: #339933;">*</span>window<span style="color: #339933;">;</span>
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #339933;">,</span> retain<span style="color: #009900;">&#41;</span> IBOutlet UITextField <span style="color: #339933;">*</span>textField<span style="color: #339933;">;</span>
@property <span style="color: #009900;">&#40;</span>nonatomic<span style="color: #339933;">,</span> retain<span style="color: #009900;">&#41;</span> IBOutlet UILabel <span style="color: #339933;">*</span>label<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>IBAction<span style="color: #009900;">&#41;</span>updateButtonpressed<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> sender<span style="color: #339933;">;</span>
&nbsp;
@end</pre></div></div>

<p>The first lines inside the curly braces will declare variables that we will be hooking up in Interface builder later. After the curly braces are closed, we have these property tags. These will allow us to have access to the objects and their data in the implementation file. After that we have an IBAction method declaration. This will eventually be hooked up to our button to run every time that our button is pressed. You can think of an IBAction method as an event handler in OBjective-C. Now let&#8217;s move onto our LabelTesterAppDelegate.m file and add the following method directly below out import statement.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#import &quot;LabelTesterAppDelegate.h&quot;</span>
&nbsp;
@implementation LabelTesterAppDelegate
&nbsp;
@synthesize window<span style="color: #339933;">;</span>
@synthesize textField<span style="color: #339933;">;</span>
@synthesize label<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #339933;">-</span><span style="color: #009900;">&#40;</span>IBAction<span style="color: #009900;">&#41;</span>updateButtonpressed<span style="color: #339933;">:</span><span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span> sender
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">//allocate a string and initialize it with the data from the text field</span>
	NSString <span style="color: #339933;">*</span>string <span style="color: #339933;">=</span> textField.<span style="color: #202020;">text</span><span style="color: #339933;">;</span>
&nbsp;
	label.<span style="color: #202020;">text</span> <span style="color: #339933;">=</span> string<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Basically what we&#8217;re doing here is just creating that IBAction method that we declared in our interface file. Inside that method, we allocate a string to the value of our textField.text and then set the label equal to the string that we get from the textfield. That&#8217;s all the code we&#8217;ll need for now. Don&#8217;t forget to synthesize your objects below the implementation declaration in order to have access to the data you want. If you try and run this now, the application will run but will not function because the pointers in Interface Builder are not hooked up. So let&#8217;s go ahead and do that next. Save all your xcode files and open back up your Main Window.xib file in Interface Builder.</p>
<p>In order to setup a pointer in Interface builder, start by clicking on the application delegate and then the connections manager should appear. Beside each of the items that you created you should see a tiny circle. For each, click on the circle and drag the pointer to each of the elements in your window. This will create a pointer to that element. Now click on the button and then open it&#8217;s connection manager. Find the &#8220;touch up inside&#8221; connection and drag that pointer to the &#8220;file&#8217;s owner&#8221; section in the library and select &#8220;updateButtonPressed&#8221; when it appears. Once this is completed, you should see the connection screen look something like this.</p>
<p><a href="http://codeperspective.ca/images/XCodePart1/IBConnections.png"><img class="aligncenter" title="IB Add Connections" src="http://codeperspective.ca/images/XCodePart1/IBConnections.png" alt="" width="301" height="259" /></a>That&#8217;s all folks, now if you run the application and enter some text into the text field, it will update the label to whatever text you entered. To download the whole project click <a title="XcodeProject For Label Tester" href="http://codeperspective.ca/LabelTester.zip" target="_self">here</a>.</p>
<p style="text-align: center;"><a style="text-decoration: none;" href="http://codeperspective.ca/images/XCodePart1/iphoneFinish.png"><img class="aligncenter" title="Iphone Finished Product" src="http://codeperspective.ca/images/XCodePart1/iphoneFinish.png" alt="" width="290" height="539" /></a></p>
<p style="text-align: center;">Cheers everyone and stay tuned for part 2!</p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2010/03/01/iphone-programming-part-1-intro-to-xcode-and-ib/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Kal-Arts.com is up and running</title>
		<link>http://codeperspective.ca/2010/01/10/kal-arts-com-is-up-and-running/</link>
		<comments>http://codeperspective.ca/2010/01/10/kal-arts-com-is-up-and-running/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 03:04:22 +0000</pubDate>
		<dc:creator>Mark Hazlett</dc:creator>
				<category><![CDATA[Portfolio]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Design]]></category>

		<guid isPermaLink="false">http://codeperspective.ca/?p=201</guid>
		<description><![CDATA[I have just completed my first freelance site! It's looking pretty great! I learnt a lot on this project and am really excited with the result. The full site can be found here. I'll be posting this site in a portfolio page when I get that up and running but for now here are a few screenshots.]]></description>
			<content:encoded><![CDATA[<p>I have just completed my first freelance site! It&#8217;s looking pretty great! I learnt a lot on this project and am really excited with the result. The full site can be found <a title="Chris Lee Design and Illustration" href="http://www.kal-arts.com/" target="_blank">here</a>. I&#8217;ll be posting this site in a portfolio page when I get that up and running but for now here are a few screenshots.</p>
<p>Home Page&#8230;</p>
<p><a href="http://codeperspective.ca/images/ChrisLee/home.png"><img class=" alignnone" title="Kal-Arts Home" src="http://codeperspective.ca/images/ChrisLee/home.png" alt="Home Page at kal-arts.com" width="684" height="572" /></a></p>
<p>Portfolio Page&#8230;</p>
<p><a href="http://codeperspective.ca/images/ChrisLee/portfolio.png"><img class=" alignnone" title="kal-arts portfolio page" src="http://codeperspective.ca/images/ChrisLee/portfolio.png" alt="Portfolio Page at kal-arts.com" width="656" height="566" /></a></p>
<p>Contact Form&#8230;</p>
<p><a href="http://codeperspective.ca/images/ChrisLee/contact.png"><img class=" alignnone" title="Contact Form kal-arts" src="http://codeperspective.ca/images/ChrisLee/contact.png" alt="Contact form at kal-arts.com" width="661" height="567" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://codeperspective.ca/2010/01/10/kal-arts-com-is-up-and-running/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

