<?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>James Wilding&#039;s Weblog &#187; iphone</title>
	<atom:link href="http://jameswilding.net/tag/iphone/feed/" rel="self" type="application/rss+xml" />
	<link>http://jameswilding.net</link>
	<description>Buddhist businessman, freelance web developer</description>
	<lastBuildDate>Sun, 05 Sep 2010 09:38:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>iPhone on Rails</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2009%2F08%2F10%2Fiphone-on-rails%2F&amp;seed_title=iPhone+on+Rails</link>
		<comments>http://jameswilding.net/2009/08/10/iphone-on-rails/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 09:27:41 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=153</guid>
		<description><![CDATA[It&#8217;s really simple to get a Rails application to serve custom views to an iPhone or iPod touch: so simple, in fact, that in a spare half-an-hour I wrapped the necessary code up into a tiny plugin, which will do all the work for you! Update: if you&#8217;re using the plugin, have questions, or have [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s really simple to get a Rails application to serve custom views to an iPhone or iPod touch: so simple, in fact, that in a spare half-an-hour I wrapped the necessary code up into <a href="http://github.com/jameswilding/iphoneification">a tiny plugin</a>, which will do all the work for you!</p>
<p><strong>Update:</strong> if you&#8217;re using the plugin, have questions, or have found this post useful, take a moment to <a title="My Github profile" href="http://github.com/jameswilding">send me a message on Github</a>, or email me (james at this domain). It&#8217;s always great to know how people are using my work :)</p>
<p><span id="more-153"></span></p>
<p>If you just want to get up and running as fast as possible, skip to the <strong>Plugin</strong> section at the end of the article. Feedback is always appreciated; if you use the plugin, or have questions, feel free to email me (james at this domain).</p>
<p>If you want to understand how the plugin works, read the full article.</p>
<h3>Three Simple Steps</h3>
<p>There are basically three steps to serving custom iPhone content from Rails:</p>
<ol>
<li>Register a custom <code>iphone</code> format</li>
<li>Tell Rails how to recognize an iPhone request</li>
<li>Tell Rails to use the custom format for iPhone requests</li>
</ol>
<h3>Registering a custom iPhone format</h3>
<p>Rails already knows about formats like HTML and XML, and all iPhones actually need from a Rails app is plain old HTML &#8212; but we need a way to distinguish our iPhone HTML from the HTML we send to other browsers.</p>
<p>To do this, we register a mime type alias &#8212; just a way of telling Rails that when we say &#8216;iphone&#8217;, we mean &#8216;HTML&#8217;. This is done in config/initializers/mime_types.rb:</p>
<p>[ruby]<br />
Mime::Type.register_alias &#8220;text/html&#8221;, :iphone<br />
[/ruby]</p>
<p>Now we can do something like <code>format.iphone</code> and Rails will know we want it to serve our views as HTML.</p>
<h3>Tell Rails how to recognize an iPhone request</h3>
<p>In order to serve custom views to iPhones, we need a way of recgonizing when someone is viewing our website on an iPhone. This is easy:</p>
<p>[ruby]<br />
class ApplicationController &lt; ActionController::Base</p>
<p># other code here</p>
<p>private<br />
# detect iphone requests<br />
def iphone_request?<br />
(agent = request.env["HTTP_USER_AGENT"]) &amp;&amp;<br />
agent[/(Mobile\/.+Safari)/]<br />
end<br />
end<br />
[/ruby]</p>
<p>The <code> iphone_request?</code> method looks for MobileSafari&#8217;s <a href="http://en.wikipedia.org/wiki/User_agent">user agent</a> string in the incoming HTTP request. Because <a href="http://www.apple.com/iphone/iphone-3gs/safari.html">MobileSafari</a> runs on the iPod Touch as well as on the iPhone, this method will ensure that we serve our custom views to both devices.</p>
<p>Now we can identify iPhone requests, we just need to put everything together and&#8230;</p>
<h3>Tell Rails to use our custom format for iPhone requests</h3>
<p>Normally, when Rails receives a request from a browser, it determines what sort of response the browser is looking for (XML, HTTP, JSON, etc). Then it looks for corresponding templates &#8212; index.html.erb, index.xml.erb, etc. This behaviour is customizable using ActionController&#8217;s <code>respond_to</code> method.</p>
<p>In order to serve custom views to the iPhone, we need to tell Rails to switch to our custom :iphone format whenever an iPhone visits our website. This sounds complicated, but is easy:</p>
<p>[ruby]<br />
class ApplicationController &lt; ActionController::Base</p>
<p>before_filter :adjust_format_for_iphone_requests</p>
<p>private<br />
def adjust_format_for_iphone_requests<br />
request.format = :iphone if iphone_request?<br />
end<br />
end<br />
[/ruby]</p>
<p>This little snippet calls our <code>iphone_request?</code> method and tells Rails to use our iphone format where appropriate.</p>
<p>That&#8217;s really all there is to it. You can now create index.iphone.erb, show.iphone.erb, etc, alongside your standard HTML views and layouts, and Rails will serve them up to iPhone users. Bon appetit!</p>
<h3>The Plugin</h3>
<p>Serving custom iPhone content is something I want to do from many Rails apps, so I wrapped the functionality outlined above into <a href="http://github.com/jameswilding/iphoneification">a plugin for quick and easy iphoneification</a>. Install thus:</p>
<p>[bash]<br />
script/plugin install git://github.com/jameswilding/iphoneification.git<br />
[/bash]</p>
<p>To use the plugin, just add one line to your application controller:</p>
<p>[ruby]<br />
class ApplicationController<br />
responds_to_iphone<br />
end<br />
[/ruby]</p>
<p>If there&#8217;s a particular controller or action that you don&#8217;t want to serve iPhone content from, use</p>
<p>[ruby]<br />
class NotAnIPhoneController<br />
skip_iphone_response<br />
end<br />
[/ruby]</p>
<p>The plugin does most of the work: all you need to do is name your custom iPhone template <code>{template_name}.iphone.erb</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2009/08/10/iphone-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>O2 iPhone tethering</title>
		<link>http://jameswilding.net/feeder/?FeederAction=clicked&amp;feed=Posts+%28RSS2%29&amp;seed=http%3A%2F%2Fjameswilding.net%2F2009%2F06%2F19%2Fo2-iphone-tethering%2F&amp;seed_title=O2+iPhone+tethering</link>
		<comments>http://jameswilding.net/2009/06/19/o2-iphone-tethering/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 09:07:56 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[o2]]></category>

		<guid isPermaLink="false">http://jameswilding.net/?p=122</guid>
		<description><![CDATA[O2 has published its iPhone tethering rates for the UK (tethering means connecting your laptop to the web using your iPhone&#8217;s internet connection). A 3GB connection costs £14.68/month &#8212; 10GB is £29.36/month. The 3GB package is almost five times more expensive than what I pay for home broadband: O2 charges £5/GB, I pay just £1/GB [...]]]></description>
			<content:encoded><![CDATA[<p>O2 has published its <a href="http://shop.o2.co.uk/update/internet.html">iPhone tethering rates for the UK</a> (tethering means connecting your laptop to the web using your iPhone&#8217;s internet connection). A 3GB connection costs £14.68/month &#8212; 10GB is £29.36/month.</p>
<p>The 3GB package is almost five times more expensive than what I pay for home broadband: O2 charges £5/GB, I pay just £1/GB at home.</p>
<p>I&#8217;m not saying this out of cynicism (I don&#8217;t know the costs involved in mobile phone networking), more out of curiosity. Are these reasonable rates? I&#8217;m not sure. I do image, though, that O2 will make a good profit here &#8212; and maybe the company is using higher rates to discourage casual users from overloading their network?</p>
]]></content:encoded>
			<wfw:commentRss>http://jameswilding.net/2009/06/19/o2-iphone-tethering/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
