<?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>Nutsmuggling &#187; iPhone SDK</title>
	<atom:link href="http://davidebenini.it/category/iphone-sdk/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidebenini.it</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 29 Feb 2012 16:35:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>viewWillAppear: not being called inside a UINavigationController</title>
		<link>http://davidebenini.it/2009/01/03/viewwillappear-not-being-called-inside-a-uinavigationcontroller/</link>
		<comments>http://davidebenini.it/2009/01/03/viewwillappear-not-being-called-inside-a-uinavigationcontroller/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 18:40:51 +0000</pubDate>
		<dc:creator>Davide</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://davidebenini.it/?p=225</guid>
		<description><![CDATA[I just stumbled on a small problem that gave me a great headache. I changed the view hierarchy to implement flipping preferences on the back of my App&#8217;s main view. Accordingly, I had to move some stuff out of the nibs and into the code. Iin other words, some heavy refactoring, and without testing (I [...]]]></description>
			<content:encoded><![CDATA[<p>I just stumbled on a small problem that gave me a great headache. I changed the view hierarchy to implement flipping preferences on the back of my App&#8217;s main view. Accordingly, I had to move some stuff out of the nibs and into the code. Iin other words, some heavy refactoring, and without testing (I know, bad bad me).</p>
<p>So basically I now have a root <code>UIViewController</code> which has a <code>UINavigationController</code> as a subview; this, in turn, loads a number of <code>UIViewController</code>s to navigate my items hierarchy.</p>
<p>Today, while working on cosmestic/usability improvements, I realised that something was definitely not working. When moving back and forth in my items, data were not refreshed. This pointed to one culprit, viewWillAppear not being called in the UIViewControllers.</p>
<p>After some googling <a href="http://discussions.apple.com/thread.jspa?threadID=1529769&amp;tstart=0">I found out</a> that if you add a <code>UINavigationController</code> as a subview of a UIViewController subclass, you <strong>must</strong> explicitly call its viewWillAppear method from its <em>container</em>; otherwise, they won&#8217;t be called, and when moving back and forth in the navigation tree, your <code>UIViewController</code>s&#8217; <code>viewWillAppear:</code> methods won&#8217;t be called.</p>
<p>It&#8217;s quite simple; assuming that <code>projectNavigationController</code> is a navigation controller added as a a subview of this <code>UIViewController</code> subclass, just make sure you add this simple call:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewWillAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated <span style="color: #002200;">&#123;</span> 
	<span style="color: #002200;">&#91;</span>super viewWillAppear<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>projectNavigationController viewWillAppear<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Of course the same applies to your other viewWill/Did methods</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewWillDisappear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated <span style="color: #002200;">&#123;</span> 
	<span style="color: #002200;">&#91;</span>super viewWillDisappear<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>projectNavigationController viewWillDisappear<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidAppear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated <span style="color: #002200;">&#123;</span> 
	<span style="color: #002200;">&#91;</span>super viewDidAppear<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>projectNavigationController viewDidAppear<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #002200;">-</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>viewDidDisappear<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>animated <span style="color: #002200;">&#123;</span> 
	<span style="color: #002200;">&#91;</span>super viewDidDisappear<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>projectNavigationController viewDidDisappear<span style="color: #002200;">:</span>animated<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>It&#8217;s quite simple, but wasn&#8217;t obvious for me at first.</p>
]]></content:encoded>
			<wfw:commentRss>http://davidebenini.it/2009/01/03/viewwillappear-not-being-called-inside-a-uinavigationcontroller/feed/</wfw:commentRss>
		<slash:comments>36</slash:comments>
		</item>
		<item>
		<title>iPhone SDK: sending formatted email</title>
		<link>http://davidebenini.it/2008/12/19/iphone-sdk-sending-formatted-email/</link>
		<comments>http://davidebenini.it/2008/12/19/iphone-sdk-sending-formatted-email/#comments</comments>
		<pubDate>Fri, 19 Dec 2008 18:40:34 +0000</pubDate>
		<dc:creator>Davide</dc:creator>
				<category><![CDATA[Apple]]></category>
		<category><![CDATA[Technolalia]]></category>
		<category><![CDATA[iPhone SDK]]></category>

		<guid isPermaLink="false">http://davidebenini.it/?p=205</guid>
		<description><![CDATA[As a part of an iPhone app I&#8217;m presently working on1 I was confronted with an apparently simple problem, which turned out to be more difficult than I thought.
Except it was simple, once you knew how to do it.
Question
How do I send a tabular report via e-mail in the iPhone SDK?  
Short answer
Use a [...]]]></description>
			<content:encoded><![CDATA[<p>As a part of an iPhone app I&#8217;m presently working on<sup>1</sup> I was confronted with an apparently simple problem, which turned out to be more difficult than I thought.<br />
Except it was simple, once you knew how to do it.</p>
<h3>Question</h3>
<p>How do I send a tabular report via e-mail in the iPhone SDK?  </p>
<h3>Short answer</h3>
<p>Use a <code>mailto</code> url + <code>stringByAddingPercentEscapesUsingEncoding</code> + HTML.</p>
<h3>Longer answer</h3>
<p>So far (iPhone OS 2.2) the de facto way to send e-mail on your iPhone application is using a <code>mailto</code> url.<sup>2</sup></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span>
<span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mailto:me@me.com?subject=subject&amp;body=Hi&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> openURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Of course you can use a format to include your own dynamic text, but the url string must be url escaped. Cocoa(Touch) provides a nice way to escape URLs, <code>stringByAddingPercentEscapesUsingEncoding:</code></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>encodedBody <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>eMailBody stringByAddingPercentEscapesUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The only problem left was representing tabular data. At first I though I&#8217;d use <code>\t</code> to create tabs, but I found out they&#8217;re not properly supported in e-mail bodies. Then a <a href="http://groups.google.com/group/iphonewebdev/browse_thread/thread/31496da7ed877667">discussion</a> on the Google Groups gave me the hint, the <code>body</code> parameter of <code>mailto</code> is actually parsed as HTML.<br />
So, amazing as it seems, this code works:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>eMailBody <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;&lt;table&gt;
&lt;tr&gt;&lt;td style='text-align:right'&gt;&lt;b&gt;Name&lt;/b&gt;:&lt;/td&gt;
&lt;td&gt;John&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;
&lt;td style='text-align:right'&gt;&lt;b&gt;Surname&lt;/b&gt;:&lt;/td&gt;&lt;td&gt;Doe&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td style='text-align:right'&gt;
 &lt;b&gt;Occupation:&lt;b/&gt;&lt;/td&gt;&lt;td&gt;Placeholder&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quot;</span>;
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>encodedBody <span style="color: #002200;">=</span> 
<span style="color: #002200;">&#91;</span>eMailBody stringByAddingPercentEscapesUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>; 
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>urlString <span style="color: #002200;">=</span> 
<span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mailto:me@me.com?subject=HiPhone&amp;body=%@&quot;</span>, encodedBody<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> alloc<span style="color: #002200;">&#93;</span> initWithString<span style="color: #002200;">:</span>urlString<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> openURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>And here&#8217;s the result:</p>
<div style="text-align:center;"><img src="http://davidebenini.it/wp-content/uploads/2008/12/img-0001.png" alt="IMG_0001.PNG" border="0" width="320" height="480" /></div>
<p>Bingo!</p>
<p>A weird downside of this method is that the user will be able to edit the email, but not is formatting: the bold characters will stay bold and so on; this seems to be a feature of the mail app itself. </p>
<h3>Device vs Simulator</h3>
<p>Since the iPhone simulator does not have a Mail.app, you can use e-mail URLs only in the actual device. As a matter of fact, it’s advisable to use compiler statements to create specific simulator/device code:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#if TARGETIPHONESIMULATOR</span>
    <span style="color: #11740a; font-style: italic;">//compiler specific code</span>
<span style="color: #6e371a;">#else</span>
    <span style="color: #11740a; font-style: italic;">// device specific code</span>
<span style="color: #6e371a;">#endif</span></pre></div></div>

<p>In my app, I use a <code>UIAlertView</code> in the simulator code, so I can have the email text displayed onscreen. Currently I have a <code>UITextView</code> as its subview, but I plan to use a <code>UIWebView</code>, so I can preview the HTML in the simulator.</p>
<p><em>(Thanks to <a href="http://www.hakbox.com/">Slashzero</a>  for his comment)</em></p>
<h4>Notes</h4>
<ol class="footnotes">
<li id="footnote_0_205" class="footnote">shush shush, too early to talk about it!</li>
<li id="footnote_1_205" class="footnote">Please note that, although the mailto protocol might support attachments, you specify attachment with the sdk. I know, it&#8217;s a shame, but so far we&#8217;re not allowed to send attachments through mail. The alternative is writing your own smtp server, but refrain from doing it, unless the composed mail is sent without any user intervention, otherwise you&#8217;ll also have to write your own mail client. </li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://davidebenini.it/2008/12/19/iphone-sdk-sending-formatted-email/feed/</wfw:commentRss>
		<slash:comments>184</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
