<?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>11 Pixel Studios &#187; Code</title>
	<atom:link href="http://www.11pixel.com/blog/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.11pixel.com/blog</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 31 Aug 2010 22:03:55 +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>Setting up SVN on MediaTemple Hosting</title>
		<link>http://www.11pixel.com/blog/37/setting-up-svn-on-mediatemple-hosting/</link>
		<comments>http://www.11pixel.com/blog/37/setting-up-svn-on-mediatemple-hosting/#comments</comments>
		<pubDate>Sat, 26 Dec 2009 10:47:00 +0000</pubDate>
		<dc:creator>11px</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.11pixel.com/blog/?p=37</guid>
		<description><![CDATA[We finally set up our SVN on a MediaTemple grid server and there are a few tricks along the way that might help you set up yours. For those that are unfamiliar with it, Subversion is a version control system that keeps track of current and previous versions of files. We use it mostly for [...]]]></description>
			<content:encoded><![CDATA[<p>We finally set up our SVN on a MediaTemple grid server and there are a few tricks along the way that might help you set up yours.</p>
<p>For those that are unfamiliar with it, <a href="http://subversion.tigris.org/" target="_new">Subversion</a> is a version control system that keeps track of current and previous versions of files. We use it mostly for developing web applications across multiple systems as it makes it very easy to work from different machines anywhere.</p>
<p>We wanted our setup to accomplish a few simple tasks:</p>
<ul>
<li>Maintain a copy of our source code in a single, off-site repository.</li>
<li>Be able to easily download the current-state of any files to our laptops and desktops.</li>
<li>Be able to update the entire live website with a single file check-in command.</li>
</ul>
<p>So without further ado, here's how to get started:</p>
<p>First, you have to enable SSH access on your administrator account. Log in to your MediaTemple account center and go to Server Administrator in your Controls. Make sure the SSH Option is set to "Enabled."</p>
<p><a href="http://www.11pixel.com/blog/wp-content/uploads/2009/12/ssh-mt1.gif" rel="lightbox[37]"><img src="http://www.11pixel.com/blog/wp-content/uploads/2009/12/ssh-mt1.gif" alt="Enable SSH" title="Enable SSH" width="539" height="154" class="aligncenter size-full wp-image-41" style="border: 3px solid #E2E2E2;" /></a></p>
<p>Just a quick note before we get to the commands, the next steps make a lot of use of 3 key variables: your primary domain, your administrator username, and your grid identification number. </p>
<div style="background-color:#BDB; border-top: 1px solid #999; border-bottom: 1px solid #999; padding:10px;"><b>Primary Domain (herein, my-example.com)</b><br />
The primary domain is usually the domain you first signed up with. In this example, it will be called "my-example.com" which means do not use the leading "www."</p>
<p><b>Administrator Username (herein, username)</b><br />
As of the writing of this article, the default administrator username appears to match the primary domain (i.e. "my-example.com") but for some legacy users it may be something like "serveradmin@my-example.com". Whatever yours is, you'll want to make sure to use the entire name. To figure out what yours is, go to your controls and click "Server Guide" you'll find it under FTP -> Admin Username.</p>
<p><b>Grid ID Number (herein, #####)</b><br />
If you don't know your grid id number, you can find it under "Server Guide" in your account center controls. It is the 4 or 5 digit number in your Primary Access Domain: http://s#####.gridserver.com/ (ignore the initial 's').</div>
<p>Next, you're going to log in to your grid server using SSH. To do that, you need to open up a Terminal window (/Applications/Utilities/Terminal.app), or using a client like <a href="http://www.chiark.greenend.org.uk/~sgtatham/putty/" target="_new">PuTTY</a> if you're on Windows, and type the following command:</p>
<div class="terminal">
ssh username@my-example.com</div>
<p>Type in your administrator password when prompted (not the same thing as your Account Center password). You'll get a welcome message if you did it right, and now you're logged in to your server. </p>
<p>Next, we're going to make a directory to collect your repositories in (in case you, like us, want to track multiple projects separately). Feel free to change out the last bit, "svn_repos," to whatever folder name you wish to use to collect them in. (For simplicity sake, don't use any space characters!)</p>
<div class="terminal">
mkdir /home/####/data/svn_repos<br />
cd /home/####/data/svn_repos</div>
<p>Ok, now you're in your svn_repos directory. Time to make your SVN repository (it's easier than it sounds). Luckily for you, SVN comes preinstalled on your server. The repository we're making is going to manage our entire website so we're actually going to name it "my-example.com" you can name yours anything you like but if you want to update your site with a single checkin, we advise you do the same.</p>
<div class="terminal">
svnadmin create --fs-type fsfs my-example.com</div>
<p>The bit about "--fs-type fsfs" just tells SVN to use a filesystem type that (mt) supports. You're done with your server for now, be sure to logout.</p>
<div class="terminal">
logout</div>
<p>Now that your repository is created, it's time to do a checkout on your local machine. </p>
<p>This part of our tutorial is done using the command line version of SVN for Mac that is available from <a href="http://www.open.collab.net/downloads/community/" target="_new">CollabNet (link)</a>. You'll want to get the "Universal Subversion Binaries for MAC OS X" download and install it.</p>
<p>If you're using Windows you'll probably be using a graphical client like <a href="http://tortoisesvn.tigris.org/" target="_new">Tortoise SVN</a>. Unfortunately we don't have step-by-step for you right here, but if you follow along you can probably figure out how to set up your SVN in a similar manner. If nothing else, stay tuned for the juicy server-side help at the end!</p>
<p>Now that you have an SVN client installed, you need to choose where you're going to keep your repository on your local machine. </p>
<div style="background-color:#BDB; border-top: 1px solid #999; border-bottom: 1px solid #999; padding:10px;">
<b>Helpful Hint:</b><br />
<i>This tip is how to store your repositories with a short system path to make check-ins more painless. It is not critical to setting up your repository and you can skip it!</i></p>
<p>We sometimes use a tool called <a href="http://www.mamp.info/en/index.html">MAMP</a> to create a local web development environment with PHP and MySQL. In order to use it, our local web files have to be stored in MAMP's htdocs folder in /Applications/MAMP/htdocs/. So we want to checkout our repository into our website folder -but- that's way too long of a folder path to type out every time we want to check in our files! Enter: the symbolic link. A symbolic link is a file that links to a folder and acts transparently as that folder. </p>
<p>So we're going to create a "/web" link in the root of our hard drive. Open Terminal and enter the following command (where "/Applications/MAMP/htdocs" is the real folder you want to put your repositories in and "/web" is the shortcut folder name):</p>
<div class="terminal">
ln -s /Applications/MAMP/htdocs /web</div>
<p>Now whenever we want to get to the folder containing our repositories, we can just use /web/repository_name instead of that long folder path!</p></div>
<p>Ok, now go into the folder where you want to checkout your repository. (Note: When you checkout your repository, it will create a folder that has the same name as the repository. Navigate to the place that you want to <b>hold</b> that repository folder.) We're going to use /web - replace it with your desired container.</p>
<div class="terminal">
cd /web</div>
<p>Now, let's do the checkout. Type the following command in your Terminal window:</p>
<div class="terminal">
svn co svn+ssh://username@my-example.com/home/####/data/svn_repos/my-example.com</div>
<p>That's it! Your local SVN is now configured. Let's head back to the server for a moment:</p>
<div class="terminal">
ssh username@my-example.com</div>
<p>Now we're going to navigate to the location we want our web files to appear when we check them in. This is NOT the same as your SVN repository in the "data" folder. Instead, we're going to go into the "domains" folder where our websites files are hosted and can be seen from a web browser. Because we're setting up our entire domain to be under version control, we're going to navigate directly into the domains folder. <i>If you only want a sub-directory of your website to be under version control, you would navigate to its parent directory in this step.</i></p>
<div class="terminal">
cd /home/#####/domains</div>
<p></p>
<div style="background-color:#BDB; border-top: 1px solid #999; border-bottom: 1px solid #999; padding:10px;">
<b>Helpful Hint:</b><br />
If you're setting up version control for your entire site, you probably have some files that are currently in your website's directory. If you're not setting up version control for your entire site, you can skip this step. Otherwise, move your website's folder to a backup location:</p>
<div class="terminal">
mv my-example.com bak.my-example.com</div>
</div>
<p>Now, checkout a copy of the repository to this folder on your server. The syntax is a little different than before.</p>
<div class="terminal">
svn co file:///home/#####/data/svn_repos/my-example.com</div>
<p></p>
<div style="background-color:#BDB; border-top: 1px solid #999; border-bottom: 1px solid #999; padding:10px;">
<b>Helpful Hint:</b><br />
If you're not setting up version control for your entire site, skip these steps. Otherwise, copy your website's files back into place:</p>
<div class="terminal">
cp -r bak.my-example.com/html my-example.com/html<br />
cd my-example.com</div>
<p>After copying, sometimes the permissions on the html folder can go askew. Best to run a quick chmod to make sure your file permissions stay correct here.</p>
<div class="terminal">
chmod 755 html</div>
<p>Now you're going to add all your website files to version control.</p>
<div class="terminal">
svn add html</div>
<p>You'll see a big list go by of all the files that SVN is adding to version control. When the list finally finishes and you have a prompt again, it's time to do your first check-in.</p>
<div class="terminal">
svn ci -m "Initial Check In"</div>
<p>This may take a while depending on how many files you have and what size they are. When it finishes, update your local machine's SVN with all these newly added files. From a <b>separate</b> Terminal window, navigate on your <b>local machine</b> and perform an SVN update.</p>
<div class="terminal">
cd /web/my-example.com<br />
svn up</div>
</div>
<p><b>Hooks!</b><br />
<i>The Bread and Butter of SVN on a Web Server</i></p>
<p>That's right, folks. For anyone reading this article that is new to SVN administration - this is the section that you've been waiting for. "How do I get my web server to update to the newest version when I check-in my files?" you've been wondering... Enter: hooks. Hooks are scripts that execute when a repository action occurs. For the purposes of updating a live site on check-in, we're going to use what's called a post-commit hook.</p>
<p>So, while SSH'd inside your server navigate to your repository's hooks:</p>
<div class="terminal">
cd /home/#####/data/svn_repos/my-example.com/hooks</div>
<p>Make a post-commit hook with the following command:</p>
<div class="terminal">
vi post-commit</div>
<p>Press "i" to get vi in the "insert" mode and enter the following into the new file (you may replace "/home/#####/domains/my-example.com" with the location of the checkout we did in the server-side checkout earlier. If you've been following the directions for making your whole site source controlled, this is the correct directory.)</p>
<div class="terminal">
#!/bin/sh<br />
svn update /home/#####/domains/my-example.com</div>
<p>Press Escape to get vi out of the "insert" mode then type :wq and press enter to save and quit. Now you need to make the file executable.</p>
<div class="terminal">
chmod 755 post-commit</div>
<p>Finally, logout of MediaTemple.</p>
<div class="terminal">
logout</div>
<p>And... You're done! Your website is ready to update live every time you commit a change to your repository. Remember, you can have your repositories checked out on multiple machines, and anytime you check-in a change from anywhere, your website will update.</p>
<p>I hope the above has been clear. Please forgive this verbose and convoluted tutorial, the ones I used in trying to get this thing set up seem to have left steps out which caused me a few headaches.</p>
<div style="background-color:#BDB; border-top: 1px solid #999; border-bottom: 1px solid #999; padding:10px;">
<b>Basic SVN Usage on your Local Machine</b><br />
Here are a few commands you may find useful when you're using SVN on your local machine. </p>
<p><i>All of these commands should be used inside the directory you want them to influence.</i></p>
<p><b>Checkout</b><br />
This will perform the initial checkout of the current version of the SVN repository and set up that repository on your local machine. You only ever need to do this once per repository on each machine.</p>
<div class="terminal">
svn co svn+ssh://username@my-example.com/path/to/repo/repo_name</div>
<p><b>Check-In</b><br />
This will check-in any updated files on your local machine to the server. You can populate the "Message" field with a helpful note about what changed or leave it blank with ""</p>
<div class="terminal">
svn ci -m "Message"</div>
<p><b>Update</b><br />
This will update any old files on your local machine with the most current version available from the server.</p>
<div class="terminal">
svn up</div>
<p><b>Add</b><br />
When you create new files or folders, you must let SVN know to add them to version control. Do so with the add command (if you use a folder name in place of filename, svn will add the folder and recursively add any folders and files within it)</p>
<div class="terminal">
svn add filename</div>
<p><b>Remove</b><br />
Similarly, if you want to delete a file, you must remove it from source control. This command will delete the file or folder on the local machine and on the server.</p>
<div class="terminal">
svn rm filename</div>
</div>
<p><b>Additional Resources:</b></p>
<ul>
<li><a href="http://kb.mediatemple.net/questions/143/Using+Subversion" target="_new">MediaTemple: Using Subversion</a></li>
<li><a href="http://subversion.tigris.org/" target="_new">Subversion Website</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.11pixel.com/blog/37/setting-up-svn-on-mediatemple-hosting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Resize Multi-line Text to Fit UILabel on iPhone</title>
		<link>http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/</link>
		<comments>http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/#comments</comments>
		<pubDate>Thu, 30 Jul 2009 22:57:53 +0000</pubDate>
		<dc:creator>11px</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.11pixel.com/blog/?p=28</guid>
		<description><![CDATA[The UILabel is a great tool for displaying lines of text in any view and is easy to set up using the provided Interface Builder. Unfortunately, there is a deceptive option in the Label Attributes that says "Font Size: Adjust To Fit." This is a great feature because it means that the iPhone will automatically [...]]]></description>
			<content:encoded><![CDATA[<p>The UILabel is a great tool for displaying lines of text in any view and is easy to set up using the provided Interface Builder. Unfortunately, there is a deceptive option in the Label Attributes that says "Font Size: Adjust To Fit." This is a great feature because it means that the iPhone will automatically resize your text down in order to fit all of it into the label. Unfortunately, this feature only works when your label's layout is set to single-line. If your text is multiline like ours was, it won't adjust the font size at all!</p>
<p>Well we did some brief searching around to locate a solution to this problem but ended up simply writing our own. We have the following situation:</p>
<ul>
<li>We have dynamic text, the exact quantity of which varies.</li>
<li>We want to place that text in a fixed-width and fixed-height UILabel</li>
<li>We don't want the text to get cutoff if it doesn't fit perfectly</li>
</ul>
<p>In order to meet this challenge, we wrote some simple code to automatically resize the text.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//Create a string with the text we want to display.</span>
self.ourText <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;This is your variable-length string. Assign it any way you want!&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">/* This is where we define the ideal font that the Label wants to use.
   Use the font you want to use and the largest font size you want to use. */</span>
UIFont <span style="color: #002200;">*</span>font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIFont fontWithName<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Marker Felt&quot;</span> size<span style="color: #002200;">:</span><span style="color: #2400d9;">28</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #a61390;">int</span> i;
<span style="color: #11740a; font-style: italic;">/* Time to calculate the needed font size.
   This for loop starts at the largest font size, and decreases by two point sizes (i=i-2)
   Until it either hits a size that will fit or hits the minimum size we want to allow (i &gt; 10) */</span>
<span style="color: #a61390;">for</span><span style="color: #002200;">&#40;</span>i <span style="color: #002200;">=</span> <span style="color: #2400d9;">28</span>; i &gt; <span style="color: #2400d9;">10</span>; i<span style="color: #002200;">=</span>i<span style="color: #002200;">-</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Set the new font size.</span>
	font <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>font fontWithSize<span style="color: #002200;">:</span>i<span style="color: #002200;">&#93;</span>;
	<span style="color: #11740a; font-style: italic;">// You can log the size you're trying: NSLog(@&quot;Trying size: %u&quot;, i);</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* This step is important: We make a constraint box 
	   using only the fixed WIDTH of the UILabel. The height will
	   be checked later. */</span> 
	CGSize constraintSize <span style="color: #002200;">=</span> CGSizeMake<span style="color: #002200;">&#40;</span>260.0f, MAXFLOAT<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// This step checks how tall the label would be with the desired font.</span>
	CGSize labelSize <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.ourText sizeWithFont<span style="color: #002200;">:</span>font constrainedToSize<span style="color: #002200;">:</span>constraintSize lineBreakMode<span style="color: #002200;">:</span>UILineBreakModeWordWrap<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* Here is where you use the height requirement!
	   Set the value in the if statement to the height of your UILabel
	   If the label fits into your required height, it will break the loop
	   and use that font size. */</span>
	<span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span>labelSize.height &lt;<span style="color: #002200;">=</span> 180.0f<span style="color: #002200;">&#41;</span>
		<span style="color: #a61390;">break</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #11740a; font-style: italic;">// You can see what size the function is using by outputting: NSLog(@&quot;Best size is: %u&quot;, i);</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Set the UILabel's font to the newly adjusted font.</span>
msg.font <span style="color: #002200;">=</span> font;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Put the text into the UILabel outlet variable.</span>
msg.text <span style="color: #002200;">=</span> self.ourText;</pre></td></tr></table></div>

<p>To use the above text, you need to make a "IBOutlet UILabel *msg;" which you assign as the UILabel's Reference Outlet from the interface builder.</p>
<p>And there you have it: easy automatic resizing of multi-line text to fit in your fixed-size UILabel for the iPhone using Objective-C!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.11pixel.com/blog/28/resize-multi-line-text-to-fit-uilabel-on-iphone/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Send Email from inside an iPhone App</title>
		<link>http://www.11pixel.com/blog/7/send-email-from-inside-an-iphone-app/</link>
		<comments>http://www.11pixel.com/blog/7/send-email-from-inside-an-iphone-app/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 01:34:27 +0000</pubDate>
		<dc:creator>11px</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[iPhone]]></category>

		<guid isPermaLink="false">http://www.11pixel.com/blog/?p=7</guid>
		<description><![CDATA[One of our first challenges with the Ninja Missions iPhone app was to make a simple and quick way for users to share missions with their friends. This is a valuable tool for viral marketing. Through past projects, we know how many people use the 'share this with a friend' links on websites. It's a [...]]]></description>
			<content:encoded><![CDATA[<p>One of our first challenges with the Ninja Missions iPhone app was to make a simple and quick way for users to share missions with their friends. This is a valuable tool for viral marketing. Through past projects, we know how many people use the 'share this with a friend' links on websites. It's a surprisingly effective free marketing tool, so we wanted to add one to our iPhone.</p>
<p>Our first thought was: How cool would it be to allow people to text it to a friend?!</p>
<p>Unfortunately, Apple does not currently have an API that allows you to open a pre-filled SMS like they do for email. The only way to achieve this would be to use a SMS Gateway provider (like <a href="http://www.tm4b.com" target="_blank">TM4B</a> or <a href="http://www.clickatell.com/" target="_blank">Clickatell</a>) and send the SMS through a web-call in the application. </p>
<p>We can open the SMS application through a link with the following code, but there is no way to fill in the message automatically. (See: <a href="http://developer.apple.com/iphone/library/featuredarticles/iPhoneURLScheme_Reference/Articles/sms.html" target="_blank">Apple URL Scheme Reference</a>)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sms:&quot;</span>&gt;</span>Opens SMS Application<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sms:1-800-555-1234&quot;</span>&gt;</span>Opens New SMS Message to 1-800-555-1234<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span></pre></td></tr></table></div>

<p>Or, open it from within an app with the following code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><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><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;sms:1-800-555-1234&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Clearly, just opening a blank text message wasn't going to work in this case, and the price of SMS Gateway Providers couldn't be justified until our app started generating a bit of revenue. So we decided to use the tried and true email link to open a premade email inside of the iPhone Mail application. </p>
<p><b>To Send Email from inside an iPhone App in Objective-C:</b></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><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><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mailto:test@test.com?subject=Subject%20Line&amp;body=Body%20Text&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Simply put, the above code is the core function needed to open a new email message from within an iPhone App. However, this was not entirely what we needed. We needed to put a NSString generated from our data into the middle of a fixed chunk of text. To do that, we did this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>IBAction<span style="color: #002200;">&#41;</span>sendEmail <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">/* We want to include this variable text within the premade body. 
	   You could replace this variable text with a personalized message from the user or remove it. */</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* First, encode the variable text (in this case, the mission) 
	   NOTE: self.theMission is our NSString object which contain's the unescaped mission text
	   Replace this variable with your own variable (if needed). */</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>encodedMission <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self.theMission stringByAddingPercentEscapesUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* Make a string that contains the first part of the static text.
	   We start off with the mailto:email?subject=subject&amp;body=text format.
	   Stop right where you want to put in the variable's text.
	   ---------
	   To make this part easy, use a free web-based URL encoder like the one at: 
	   http://meyerweb.com/eric/tools/dencoder/
	   ---------
	   Our first half will give us:
	   To:Test@Test.com
	   Subject: Subject Text Here
	   Body: Fellow Ninja -
	   (line break)
	   */</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>firstHalf <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mailto:Test@Test.com?subject=Subject%20Text%20Here&amp;body=Fellow%20Ninja%20-%0D%0A%0D%0A&quot;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* And the second half of the email (the post-variable segment)
	   Encoded the same way we did the first half. This string will decode to:
	   (line break)
	   Yours,
	   (line break)
	   Ninja Master 
	   */</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>lastHalf <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%0D%0A%0D%0AYours%2C%0D%0A%0D%0ANinja%20Master&quot;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* Now we concatenate the strings. */</span>
	<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>mailCode <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;%@%@%@&quot;</span>, firstHalf, encodedMission, lastHalf<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">/* Now it's time to send it! The following code makes the above string into a 
	   URL and opens the URL causing the Mail program to launch with a premade email. */</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><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span>mailCode<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>We then bind this code to a button action in the interface builder. </p>
<p><i>It is important to note that this button will not do anything inside the iPhone simulator from the iPhone SDK because the simulator does not have a Mail App. We have to test the app on a developer's iPhone to test the function.</i></p>
<p>And it works!</p>
<p><a href="http://www.11pixel.com/blog/wp-content/uploads/2009/08/screen2.jpg" rel="lightbox[7]"><img src="http://www.11pixel.com/blog/wp-content/uploads/2009/08/screen2-150x150.jpg" alt="iPhone Email Screenshot" title="iPhone Email Screenshot" width="150" height="150" class="size-thumbnail wp-image-12" align="left" /></a> <a href="http://www.11pixel.com/blog/wp-content/uploads/2009/08/screen1.jpg" rel="lightbox[7]"><img src="http://www.11pixel.com/blog/wp-content/uploads/2009/08/screen1-150x150.jpg" alt="iPhone Email Screenshot" title="iPhone Email Screenshot" width="150" height="150" class="size-thumbnail wp-image-17" align="left" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.11pixel.com/blog/7/send-email-from-inside-an-iphone-app/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
