<?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>Kelvin Kao Dev</title>
	<atom:link href="http://www.kelvinkaodev.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kelvinkaodev.com/blog</link>
	<description>About iOS development, until I go off on tangents</description>
	<lastBuildDate>Fri, 08 Apr 2011 09:07:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Writing a PHP Script that Takes One Protobuf and Returns Another Protobuf</title>
		<link>http://www.kelvinkaodev.com/blog/2011/04/writing-a-php-script-that-takes-one-protobuf-and-returns-another-protobuf/</link>
		<comments>http://www.kelvinkaodev.com/blog/2011/04/writing-a-php-script-that-takes-one-protobuf-and-returns-another-protobuf/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 09:07:08 +0000</pubDate>
		<dc:creator>Kelvin Kao</dc:creator>
				<category><![CDATA[Protobuf]]></category>

		<guid isPermaLink="false">http://www.kelvinkaodev.com/blog/?p=48</guid>
		<description><![CDATA[After we&#8217;ve generated the PHP classes from the Protocol Buffers, we can now use those classes in a PHP script. The PHP script will take a StoryRequest Protobuf from the iOS client, and respond with a StoryResponse Protobuf. We&#8217;ll call &#8230; <a href="http://www.kelvinkaodev.com/blog/2011/04/writing-a-php-script-that-takes-one-protobuf-and-returns-another-protobuf/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After we&#8217;ve <a href="http://www.kelvinkaodev.com/blog/2011/04/generating-php-classes-from-protocol-buffer/">generated the PHP classes from the Protocol Buffers</a>, we can now use those classes in a PHP script. <strong>The PHP script will take a StoryRequest Protobuf from the iOS client, and respond with a StoryResponse Protobuf</strong>.</p>
<p>We&#8217;ll call this file story_generator.php and upload it into the story folder on the server (that we&#8217;ve created in the last tutorial). Here&#8217;s the entire code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p48code2'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p482"><td class="code" id="p48code2"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?</span>
<span style="color: #666666; font-style: italic;">// first include pb_message</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'../message/pb_message.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now include the generated files</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./pb_proto_StoryRequest.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./pb_proto_StoryResponse.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//parse the StoryRequest</span>
<span style="color: #000088;">$received_story_request</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StoryRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$received_story_request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parseFromString</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$HTTP_RAW_POST_DATA</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//the info we want to gather</span>
<span style="color: #000088;">$name1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$received_story_request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">character1</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$name2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$received_story_request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">character2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ammo</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$received_story_request</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">secret_weapon</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ammo</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$ammo</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;baseball&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//build the StoryResponse</span>
<span style="color: #000088;">$story_response</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StoryResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$story_response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_status</span><span style="color: #009900;">&#40;</span>FAILED<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$story_response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_story</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Oops. Both characters need to be named!&quot;</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;">$story_response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_status</span><span style="color: #009900;">&#40;</span>SUCCESS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$story_choice</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$the_story</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$story_choice</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'Once upon a time, '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' and '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' went to slay a dragon that started fires everywhere. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'When they arrived, the dragon spat fire at them. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'They were backed into a corner. They were both scared. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'And then suddenly, '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' had an idea.'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\n\n'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\&quot;I know, \&quot; said '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'. \&quot;Why don\'t we throw a '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ammo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' at it?\&quot;\n'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\&quot;Good idea!\&quot; agreed '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">', so they did.'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\n\n'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'And it worked! The dragon was defeated and agreed to stop being such a fire hazard. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'They agreed that they all should get along and be friends. They lived happily ever after.'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$story_choice</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'One day, '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' and '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' were playing basketball. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'Unfortunately, the ball got caught in the tree. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'And then suddenly, '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' had an idea.'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\n\n'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\&quot;I know, \&quot; said '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'. \&quot;Why don\'t we throw a '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ammo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' at it?\&quot;\n'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\&quot;Good idea!\&quot; agreed '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">', so they did.'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\n\n'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'And it worked! They got the ball back. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'They agreed that this was too much fun and decided to play basketball happily ever after.'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$story_choice</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'Long ago, in a galaxy far far away, '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' and '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' were looking for a ship. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'A salesman told them that he got a piece of junk they could sell, but they would need to fix it first. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'They were told that there was a piece missing from the engine and the ship wouldn\'t fly until they found a replacement. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'And then suddenly, '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' had an idea.'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\n\n'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\&quot;I know, \&quot; said '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name2</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'. \&quot;Why don\'t we just jam this '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ammo</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' in it?\&quot;\n'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\&quot;Good idea!\&quot; agreed '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$name1</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">', so they did.'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'\n\n'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'And it worked! The ship was good as new. '</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$the_story</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'They agreed that the ship was awesome and decided to cruise around in it happily ever after.'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000088;">$story_response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set_story</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$the_story</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//respond to client</span>
<span style="color: #000088;">$serialized_string</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$story_response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">SerializeToString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">print</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$serialized_string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>A few notes:</p>
<ol>
<li>You first use the require_once calls to make sure the PHP classes you generated are imported for use.</li>
<li>You create a new (and empty) StoryRequest: <tt>$received_story_request = new StoryRequest();</tt></li>
<li>Populate it with the content that we sent from the iOS client (remember <a href="http://www.kelvinkaodev.com/blog/2010/12/how-to-send-a-protobuf-from-iphone-client-to-server/">this tutorial</a>?). We sent the serialized StoryRequest Protobuf from the client side as the HTTP body, so we are now using the special PHP variable <tt>$HTTP_RAW_POST_DATA</tt> to get those raw bytes on the server side. There might be a better way. Leave me a comment if you are a more experienced PHP programmer.</li>
<li>Now you have a <tt>$receive_story_request</tt> filled with actual content, you can use the getters defined in the StoryRequest PHP class (that you compiled from the .proto definitions) to get the character names and the weapon.</li>
<li>Now we make a new StoryResponse: <tt>$story_response = new StoryResponse();</tt>. This is a Protobuf that&#8217;s going to be serialized and sent back to the iOS client as the response.</li>
<li>Use the character names and weapon from the StoryRequest, and make a string representing a silly story using those items. Now use the setters for the StoryResponse to fill in the actual content.</li>
<li>And now you can call <tt>SerializeToString()</tt> on the StoryResponse to, well&#8230; serialize it to string.</li>
<li>And finally, print it so the HTTP request from the client side has something to grab as the result.</li>
</ol>
<p>Next, we&#8217;ll finally parse the response on the client side.</p>
<p>PS. When I was trying this, for some reason, the enum stuff never serialize correctly so it always returned the same number. I wasn&#8217;t sure if there&#8217;s a bug in the library, or it was just me being stupid (probably the latter). But since I wasn&#8217;t the one working on the server portion (and they might not even be using PHP), I did not investigate any further. The idea is that no matter what language that the server and client are using, it should still work as long as you pack the Protocol Buffers correctly.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kelvinkaodev.com/blog/2011/04/writing-a-php-script-that-takes-one-protobuf-and-returns-another-protobuf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating PHP Classes from Protocol Buffer</title>
		<link>http://www.kelvinkaodev.com/blog/2011/04/generating-php-classes-from-protocol-buffer/</link>
		<comments>http://www.kelvinkaodev.com/blog/2011/04/generating-php-classes-from-protocol-buffer/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 03:55:56 +0000</pubDate>
		<dc:creator>Kelvin Kao</dc:creator>
				<category><![CDATA[Protobuf]]></category>

		<guid isPermaLink="false">http://www.kelvinkaodev.com/blog/?p=44</guid>
		<description><![CDATA[Just like we need to compile our Protocol Buffers using the Protocol Buffer Compiler (protoc) into Objective-C classes before we can use it in an iOS project, we also need to compile the Protocol Buffer into PHP classes before we &#8230; <a href="http://www.kelvinkaodev.com/blog/2011/04/generating-php-classes-from-protocol-buffer/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Just like we need to <a href="http://www.kelvinkaodev.com/blog/2010/09/generating-objective-c-classes-from-protocol-buffer/">compile our Protocol Buffers using the Protocol Buffer Compiler (protoc) into Objective-C classes</a> before we can use it in an iOS project, we also need to compile the Protocol Buffer into PHP classes before we can use it in a server side script.</p>
<p>To get the PHP source code, you can download it from <a href="http://code.google.com/p/pb4php/">http://code.google.com/p/pb4php/</a>.<br />
Disclaimer: Just like the Objective-C one, this is a third-party open-source library that&#8217;s not officially supported by Google. Also, I am a lot more experienced in iOS than in PHP, so what I am showing you is something that will do, but might not be the best approach.</p>
<p>You will need to have some kind of PHP server set up, so later you can send a Protocol Buffer from your iOS client to the server. In the zip file you just downloaded from that Google Code page, you will see three folders: example, message, and parser. You want to create another directory. In this case, we will call it story. In the story folder, you want to have three files: <tt>StoryRequest.proto</tt>, <tt>StoryResponse.proto</tt>, and <tt>protoc.php</tt> </p>
<p>This is the content of StoryRequest.proto; you&#8217;ve seen this before.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p44code6'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p446"><td class="code" id="p44code6"><pre class="c" style="font-family:monospace;">message StoryRequest <span style="color: #009900;">&#123;</span>
	required <span style="color: #993333;">string</span> character1 <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	required <span style="color: #993333;">string</span> character2 <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
	optional <span style="color: #993333;">string</span> secret_weapon <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And this is StoryResponse.proto</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p44code7'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p447"><td class="code" id="p44code7"><pre class="c" style="font-family:monospace;">message StoryResponse <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">enum</span> ResponseType <span style="color: #009900;">&#123;</span>
		SUCCESS <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
		FAILED <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	required ResponseType status <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	optional <span style="color: #993333;">string</span> story <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And this is the new protoc.php</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p44code8'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p448"><td class="code" id="p44code8"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// just create from the proto file a pb_prot[NAME].php file</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'../parser/pb_parser.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$test</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PBParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$test</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./StoryRequest.proto'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$test</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> PBParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$test</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">parse</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'./StoryResponse.proto'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'File parsing done!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>After this, upload the three folders (parser, message, and story) into the same directory on the server using some kind of FTP program. Now, type the full path to protoc.php script (should be in the form of http://someWebSite.com/&#8230;/story/protoc.php) into a browser. This triggers the protoc php script to run. It will look at the two .proto files, and compile them into PHP classes. If you refresh the directory in the FTP program, you should see two new files generated, <tt>pb_proto_StoryRequest.php</tt> and <tt>pb_proto_StoryResponse.php</tt>. The generated classes should have class definitions that extend PBMessage and have a bunch of getters and setters in them. (If you don&#8217;t see the files generated, you might need to refresh again. If you still don&#8217;t see them, check to see if you have all the paths set up correctly. If you still don&#8217;t see them, check to see if you get write access on your server.)</p>
<p>So now we finally have the PHP classes generated from the Protocol Buffer definitions (.proto files). Next we&#8217;ll write a php script to actually take requests from the iOS client.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kelvinkaodev.com/blog/2011/04/generating-php-classes-from-protocol-buffer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to Send a Protobuf from iPhone Client to Server</title>
		<link>http://www.kelvinkaodev.com/blog/2010/12/how-to-send-a-protobuf-from-iphone-client-to-server/</link>
		<comments>http://www.kelvinkaodev.com/blog/2010/12/how-to-send-a-protobuf-from-iphone-client-to-server/#comments</comments>
		<pubDate>Fri, 31 Dec 2010 05:48:59 +0000</pubDate>
		<dc:creator>Kelvin Kao</dc:creator>
				<category><![CDATA[Protobuf]]></category>

		<guid isPermaLink="false">http://www.kelvinkaodev.com/blog/?p=41</guid>
		<description><![CDATA[After linking the protocol buffer static library into the XCode project, we can finally use it! If you recall from the generating Objective-C classes from protocol buffer post, we had a StoryRequest that takes two character names and an object &#8230; <a href="http://www.kelvinkaodev.com/blog/2010/12/how-to-send-a-protobuf-from-iphone-client-to-server/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After <a href="http://www.kelvinkaodev.com/blog/2010/09/linking-the-protocol-buffer-static-library-into-the-xcode-project/">linking the protocol buffer static library into the XCode project</a>, we can finally use it! If you recall from the <a href="http://www.kelvinkaodev.com/blog/2010/09/generating-objective-c-classes-from-protocol-buffer/">generating Objective-C classes from protocol buffer</a> post, we had a StoryRequest that takes two character names and an object to be sent to the server, and the server will send back a StoryResponse using the information supplied.</p>
<p>To refresh your memory,<br />
This is a StoryRequest:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p41code14'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4114"><td class="code" id="p41code14"><pre class="c" style="font-family:monospace;">message StoryRequest <span style="color: #009900;">&#123;</span>
	required <span style="color: #993333;">string</span> character1 <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	required <span style="color: #993333;">string</span> character2 <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
	optional <span style="color: #993333;">string</span> secret_weapon <span style="color: #339933;">=</span> <span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And this is StoryResponse:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p41code15'); return false;">View Code</a> C</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4115"><td class="code" id="p41code15"><pre class="c" style="font-family:monospace;">message StoryResponse <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">enum</span> ResponseType <span style="color: #009900;">&#123;</span>
		SUCCESS <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
		FAILED <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	required ResponseType status <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	optional <span style="color: #993333;">string</span> story <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So you will have four files to include into your XCode project:<br />
StoryRequest.pb.h, StoryRequest.pb.m, StoryResponse.pb.h, and StoryResponse.pb.m</p>
<p>I created three UITextFields to get user input for the character names and the object. The variables are named character1Field, character2Field and secretWeaponField. And finally, here is how to use the protobuf.</p>
<p>To build a protobuf, you use a _builder. In this case, you want to build a StoryRequest, so you will be using a StoryRequest_builder. Both of these classes were already generated. You can find them in StoryRequest.pb.h and StoryRequest.pb.m. You first create a StoryRequest_Builder, fill it with all the necessary info, and then use it to actually build a StoryRequest.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p41code16'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4116"><td class="code" id="p41code16"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">//StoryRequest builder</span>
StoryRequest_Builder<span style="color: #002200;">*</span> newStoryRequestBuilder <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>StoryRequest_Builder alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>newStoryRequestBuilder setCharacter1<span style="color: #002200;">:</span>self.character1Field.text<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>newStoryRequestBuilder setCharacter2<span style="color: #002200;">:</span>self.character2Field.text<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>newStoryRequestBuilder setSecretWeapon<span style="color: #002200;">:</span>self.secretWeaponField.text<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">//build StoryRequest to be sent to server</span>
StoryRequest<span style="color: #002200;">*</span> newStoryRequest <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>newStoryRequestBuilder build<span style="color: #002200;">&#93;</span>;		<span style="color: #11740a; font-style: italic;">//newStoryRequestBuilder is invalid from this point onward, do not use again</span></pre></td></tr></table></div>

<p>Now that you have a StoryRequest, it&#8217;s time to send it over to the server. You will need to have a URL for the server side script (STORY_URL that I defined elsewhere in code) and the serialized StoryRequest data. And then you use those to build an NSURLRequest.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p41code17'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4117"><td class="code" id="p41code17"><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: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span>STORY_URL<span style="color: #002200;">&#93;</span>;
<span style="color: #400080;">NSMutableURLRequest</span><span style="color: #002200;">*</span> theRequest <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableURLRequest</span> requestWithURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>theRequest setHTTPMethod<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;POST&quot;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>theRequest setHTTPBody<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>self.storyRequest data<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>theRequest setValue<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;application/x-protobuf&quot;</span> forHTTPHeaderField<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Content-Type&quot;</span><span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>Notice a few things:<br />
1) The HTTP method should be POST, so that we can set raw bytes to the HTTP body.<br />
2) You use the &#8220;data&#8221; method to turn the StoryRequest into an NSData, and then just attach that as the HTTP body. Of course there are other ways to do it if you want to send other information along with the protobuf, but in this case that&#8217;s all we want to send.<br />
3) Set your content type to application/x-protobuf. You can probably set it to other types that send byte streams (as long as it&#8217;s not text), but hey, if we have one specifically defined for protobuf, there&#8217;s no reason not to use it.</p>
<p>But we are not done. All we have now is an NSURLRequest. It&#8217;s simply a request. It represents what you intend to do, but you haven&#8217;t actually establish a connection yet. To do that, we create an NSURLConnection like so:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p41code18'); return false;">View Code</a> OBJC</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4118"><td class="code" id="p41code18"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSURLConnection</span><span style="color: #002200;">*</span> theConnection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> alloc<span style="color: #002200;">&#93;</span> initWithRequest<span style="color: #002200;">:</span>theRequest delegate<span style="color: #002200;">:</span>self startImmediately<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
self.mHTTPConnection <span style="color: #002200;">=</span> theConnection;
<span style="color: #002200;">&#91;</span>theConnection release<span style="color: #002200;">&#93;</span>;</pre></td></tr></table></div>

<p>At this point, a connection will be established. And everything goes as planned, your protobuf is now sent to the server side!</p>
<p>Next time&#8230; hm, I haven&#8217;t decided yet. We&#8217;ll either talk about how the server side generates a response or how the client side will use it. For now, enjoy the sense of satisfaction from knowing that you&#8217;ve sent the request out without thinking about whatever happened to it!</p>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kelvinkaodev.com/blog/2010/12/how-to-send-a-protobuf-from-iphone-client-to-server/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>iPad 繁體中文輸入程式新上市！</title>
		<link>http://www.kelvinkaodev.com/blog/2010/11/ipad-traditional-chinese-input/</link>
		<comments>http://www.kelvinkaodev.com/blog/2010/11/ipad-traditional-chinese-input/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 23:19:19 +0000</pubDate>
		<dc:creator>Kelvin Kao</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Chinese Input]]></category>

		<guid isPermaLink="false">http://www.kelvinkaodev.com/blog/?p=39</guid>
		<description><![CDATA[iPad 上的繁體中文輸入法新上市！這支程式，是我原本為了自己的便利而寫的，目前的iPad 3.2作業系統只支援簡體中文的輸入，但是我常常上的卻是繁體網站，在這樣的情況下，我決定自己做一個繁體中文輸入法出來！ 這個輸入法程式，讓您可以用熟悉的注音與拼音方式，來輸入繁體中文字。有了這支軟體，您再也不用先用簡體中文打字，然後再用另一支程式把簡體字轉為繁體字。 想知道更多有關的訊息嗎？請到 iPad 繁體中文輸入法 網站. 如果想要直接從 app store 下載，請按 itms://itunes.apple.com/us/app/chinese-input/id398857305?mt=8 在有的電腦上，因為設定的不同，有時這個連結並沒有辦法正確顯示，但是您可以直接到 app store 搜尋 &#8220;Chinese Input&#8221; ，即可找到本程式。 希望這支程式能為您帶來便利！]]></description>
			<content:encoded><![CDATA[<p>iPad 上的繁體中文輸入法新上市！這支程式，是我原本為了自己的便利而寫的，目前的iPad 3.2作業系統只支援簡體中文的輸入，但是我常常上的卻是繁體網站，在這樣的情況下，我決定自己做一個繁體中文輸入法出來！</p>
<p><a href="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/icon_256.png"><img src="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/icon_256.png" alt="" title="icon_256" width="256" height="256" class="alignnone size-full wp-image-35" /></a></p>
<p>這個輸入法程式，讓您可以用熟悉的注音與拼音方式，來輸入繁體中文字。有了這支軟體，您再也不用先用簡體中文打字，然後再用另一支程式把簡體字轉為繁體字。</p>
<p><a href="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/zhuyin_lan_thumb.png"><img src="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/zhuyin_lan_thumb.png" alt="" title="zhuyin_lan_thumb" width="471" height="353" class="alignnone size-full wp-image-36" /></a></p>
<p>想知道更多有關的訊息嗎？請到 <a href="http://ChineseInputApp.com/index_chinese.php">iPad 繁體中文輸入法</a> 網站.<br />
如果想要直接從 app store 下載，請按<br />
<a href="itms://itunes.apple.com/us/app/chinese-input/id398857305?mt=8">itms://itunes.apple.com/us/app/chinese-input/id398857305?mt=8</a><br />
在有的電腦上，因為設定的不同，有時這個連結並沒有辦法正確顯示，但是您可以直接到 app store 搜尋 &#8220;Chinese Input&#8221; ，即可找到本程式。</p>
<p>希望這支程式能為您帶來便利！</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kelvinkaodev.com/blog/2010/11/ipad-traditional-chinese-input/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chinese Input iPad App Released!</title>
		<link>http://www.kelvinkaodev.com/blog/2010/10/chinese-input-ipad-app-released/</link>
		<comments>http://www.kelvinkaodev.com/blog/2010/10/chinese-input-ipad-app-released/#comments</comments>
		<pubDate>Fri, 29 Oct 2010 09:19:46 +0000</pubDate>
		<dc:creator>Kelvin Kao</dc:creator>
				<category><![CDATA[Announcement]]></category>
		<category><![CDATA[Chinese Input]]></category>

		<guid isPermaLink="false">http://www.kelvinkaodev.com/blog/?p=37</guid>
		<description><![CDATA[I am happy to announce that the Chinese Input app has been approved by Apple, and is now available in the app store! With this app, you can enter Traditional Chinese characters using Zhuyin. No more using the Simplified Chinese &#8230; <a href="http://www.kelvinkaodev.com/blog/2010/10/chinese-input-ipad-app-released/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I am happy to announce that the Chinese Input app has been approved by Apple, and is now available in the app store!</p>
<p><a href="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/icon_256.png"><img src="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/icon_256.png" alt="" title="icon_256" width="256" height="256" class="alignnone size-full wp-image-35" /></a></p>
<p>With this app, you can enter Traditional Chinese characters using Zhuyin. No more using the Simplified Chinese keyboard and then converting it to Traditional Chinese again! And then after you&#8217;ve entered the text, you can copy it into another app to use.</p>
<p><a href="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/zhuyin_lan_thumb.png"><img src="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/zhuyin_lan_thumb.png" alt="" title="zhuyin_lan_thumb" width="471" height="353" class="alignnone size-full wp-image-36" /></a></p>
<p>To know more about the app, go to my <a href="http://ChineseInputApp.com">Traditional Chinese Input app on iPad</a> web page.<br />
To download a copy of the app, use the link<br />
<a href="itms://itunes.apple.com/us/app/chinese-input/id398857305?mt=8">itms://itunes.apple.com/us/app/chinese-input/id398857305?mt=8</a><br />
Now, I realized that the link doesn&#8217;t work for everyone if they don&#8217;t have their iTunes set up a certain way, but you can always go to the app store and search &#8220;Chinese Input&#8221;.</p>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kelvinkaodev.com/blog/2010/10/chinese-input-ipad-app-released/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Chinese Input iPad App Submitted!</title>
		<link>http://www.kelvinkaodev.com/blog/2010/10/chinese-input-app-submitted/</link>
		<comments>http://www.kelvinkaodev.com/blog/2010/10/chinese-input-app-submitted/#comments</comments>
		<pubDate>Sat, 23 Oct 2010 10:44:33 +0000</pubDate>
		<dc:creator>Kelvin Kao</dc:creator>
				<category><![CDATA[Chinese Input]]></category>

		<guid isPermaLink="false">http://www.kelvinkaodev.com/blog/?p=34</guid>
		<description><![CDATA[After about two years of building apps for clients, last week I submitted the first app to app store to be released under my name! And now I am waiting for approval, since I am an approval-seeking person like that. &#8230; <a href="http://www.kelvinkaodev.com/blog/2010/10/chinese-input-app-submitted/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After about two years of building apps for clients, last week I submitted the first app to app store to be released under my name! And now I am waiting for approval, since I am an approval-seeking person like that. Hm. </p>
<p><a href="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/icon_256.png"><img src="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/icon_256.png" alt="" title="icon_256" width="256" height="256" class="alignnone size-full wp-image-35" /></a></p>
<p>I built this app because I wanted this app. As of right now, the iPad only supports the Simplified Chinese keyboard. Although this covers a big portion of the Chinese-reading population, the Traditional Chinese used in Taiwan, Hong Kong, etc. wasn&#8217;t supported. Although I am not expecting to be typing huge passages on a touch screen, sometimes I just want to enter a few characters here and there. And it was an annoyance that I couldn&#8217;t do that.</p>
<p>What I ended up doing was using an app that converts between Traditional and Simplified Chinese. I would type the text using the Simplified Chinese keyboard, convert it, and then paste it into whichever app (usually Safari) that I am using. Although I got much better at Pinyin and hand-writing Simplified Chinese characters, it was still quite convoluted. As an iOS developer, how can I not build an app for that? So I went ahead and built the app. I&#8217;ve been using the app for a few months now but recently I&#8217;ve finally made it look presentable. So here we are, to the app store we come.</p>
<p><a href="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/zhuyin_lan_thumb.png"><img src="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/10/zhuyin_lan_thumb.png" alt="" title="zhuyin_lan_thumb" width="471" height="353" class="alignnone size-full wp-image-36" /></a></p>
<p>Hopefully the app will be approved soon, so it can be available in the app store sometime in November. To see more details about the app (and possibly sign up for a reminder so I can tell you when it&#8217;s actually released), check out my <a href="http://ChineseInputApp.com">Traditional Chinese Input app on iPad</a> web page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kelvinkaodev.com/blog/2010/10/chinese-input-app-submitted/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linking the Protocol Buffer Static Library into the Xcode Project</title>
		<link>http://www.kelvinkaodev.com/blog/2010/09/linking-the-protocol-buffer-static-library-into-the-xcode-project/</link>
		<comments>http://www.kelvinkaodev.com/blog/2010/09/linking-the-protocol-buffer-static-library-into-the-xcode-project/#comments</comments>
		<pubDate>Fri, 10 Sep 2010 02:57:51 +0000</pubDate>
		<dc:creator>Kelvin Kao</dc:creator>
				<category><![CDATA[Protobuf]]></category>

		<guid isPermaLink="false">http://www.kelvinkaodev.com/blog/?p=8</guid>
		<description><![CDATA[Last time we talked about how to compile proto files into Objective-C files, now let&#8217;s actually use them in a project! So create a new Xcode project, or use an existing one that will use these new files. Now add &#8230; <a href="http://www.kelvinkaodev.com/blog/2010/09/linking-the-protocol-buffer-static-library-into-the-xcode-project/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Last time we talked about how to <a href="http://www.kelvinkaodev.com/blog/2010/09/generating-objective-c-classes-from-protocol-buffer/">compile proto files into Objective-C files</a>, now let&#8217;s actually use them in a project! So create a new Xcode project, or use an existing one that will use these new files. Now add the files (in my case, StoryRequest.pb.h, StoryRequest.pb.m, StoryResponse.pb.h, StoryResponse.pb.m) to the project, and compile.</p>
<p>And Kaboom! A gazillion errors!</p>
<p>This is because we haven&#8217;t included the static library yet. So here is how you add it to the project. The same steps apply for any such static libraries, not just protocol buffers.</p>
<h1>1) Add ProtobufLib&#8217;s ProtocolBuffers target as direct dependency</h1>
<p>Let&#8217;s go back to the file that you downloaded from the <a href="http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers">Protocol Buffer for Objective-C</a> page. After you opened it, you will see a folder called <code>objectivec</code>. If you look into it, you will see familiar stuff, such as the info.plist, the xcodeproj file, and a <code>Classes</code> folder. This is the Xcode project for the static library. I like to keep these projects in sibling folders with my own projects so they can all share it. So I copied it into the same parent folder as my project so they can be siblings. I renamed the folder ProtobufLib. You can place this folder wherever you want. Just set up the path accordingly.</p>
<p>Now you want to add ProtobufLib&#8217;s ProtocolBuffers target as direct dependency. This means that when you build your project, it will first build the ProtocolBuffers target (which generates libProtocolBuffers.a), so your project will have a static library to link to. You do this by first dragging ProtobufLib&#8217;s ProtocolBuffers.xcodeproj into your project (I like to put it under Frameworks to keep things organized). You will then select ProtocolBuffers.xcodeproj and check libProtocolBuffers.a to tell Xcode to build that target, not the other two (the command line test and unit test). </p>
<p><a href="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/09/static_library.png"><img src="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/09/static_library.png" alt="" title="static_library" width="553" height="464" class="alignnone size-full wp-image-29" /></a></p>
<p>And finally, right click on your own target, select Get Info. Then under the General tab, click on the + to add ProtocolBuffers as direct dependency.</p>
<p><a href="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/09/direct_dependency.png"><img src="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/09/direct_dependency.png" alt="" title="direct_dependency" width="468" height="419" class="alignnone size-full wp-image-27" /></a></p>
<h1>2) Tell Xcode where the headers are</h1>
<p>Now, also under target info, but in the Build tab, you want to specify where the headers are. Make sure that your configuration is set to All Configurations. Well, you don&#8217;t have to, but then you would have to do this same thing once for debug and another time for distribution. Under Header Search Paths, add ../ProtobufLib since they are in a sibling folder, and check the recursive option. You can set the path to ../ProtobufLib/classes so it just goes straight to that folder. I decided to just let it search recursively in this case, since I don&#8217;t feel like specifying sub directories like classes or headers for every single library I link to.</p>
<p><a href="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/09/header_path.png"><img src="http://www.kelvinkaodev.com/blog/wp-content/uploads/2010/09/header_path.png" alt="" title="header_path" width="563" height="601" class="alignnone size-full wp-image-28" /></a></p>
<p>And compile again. It shall build without error.</p>
<p>In the next post, we&#8217;ll talk about how to use protocol buffers for the iPhone client to communicate with the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kelvinkaodev.com/blog/2010/09/linking-the-protocol-buffer-static-library-into-the-xcode-project/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Generating Objective-C Classes from Protocol Buffer</title>
		<link>http://www.kelvinkaodev.com/blog/2010/09/generating-objective-c-classes-from-protocol-buffer/</link>
		<comments>http://www.kelvinkaodev.com/blog/2010/09/generating-objective-c-classes-from-protocol-buffer/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 00:43:26 +0000</pubDate>
		<dc:creator>Kelvin Kao</dc:creator>
				<category><![CDATA[Protobuf]]></category>

		<guid isPermaLink="false">http://www.kelvinkaodev.com/blog/?p=7</guid>
		<description><![CDATA[Once you have the Protocol Buffer Compiler (protoc) built, you can use it to compile proto files into headers and implementations in the language of your choice. For example, I have this pair of proto files defined. This is a &#8230; <a href="http://www.kelvinkaodev.com/blog/2010/09/generating-objective-c-classes-from-protocol-buffer/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Once you have the <a href="http://www.kelvinkaodev.com/blog/2010/08/compiling-the-protocol-buffer-compiler-for-objective-c/">Protocol Buffer Compiler</a> (protoc) built, you can use it to compile proto files into headers and implementations in the language of your choice.</p>
<p>For example, I have this pair of proto files defined. This is a StoryRequest:<br />
<code><br />
message StoryRequest {<br />
	required string character1 = 1;<br />
	required string character2 = 2;<br />
	optional string secret_weapon = 3;<br />
}<br />
</code></p>
<p>And this is StoryResponse:<br />
<code><br />
message StoryResponse {<br />
	enum ResponseType {<br />
		SUCCESS = 0;<br />
		FAILED = 1;<br />
	}<br />
	required ResponseType status  = 1;<br />
	optional string story = 2;<br />
}<br />
</code></p>
<p>The client will send a StoryRequest to a server with names of two characters and an object, and the server will send back a StoryResponse with a status code and the story that has those two characters using that object, <a href="http://en.wikipedia.org/wiki/Mad_Libs">Mad Libs</a> style.</p>
<p>To compile the proto files using protoc, you want to indicate the path of the source files, the directory to output the files to, and indicate the language.</p>
<p>For example, for C++, you will do something like this:<br />
<code>protoc --proto_path=src --cpp_out=build/cpp src/StoryRequest.proto</code><br />
And for Objective-C, you will do something like this:<br />
<code>protoc --proto_path=src --objc_out=build/objc src/StoryRequest.proto</code></p>
<p>Note that you want to create build/cpp and build/obj in advance because protoc will not create them. If you are generating only the Objective-C files, fine, keep them in one place. But in this case, we are doing both for demonstration. Since C++ and Objective-C use the same naming conventions for header files, they need to be in separate folders. You will now see StoryRequest.pb.h and StoryRequest.pb.cc in build/cpp and StoryRequest.pb.h and StoryRequest.pb.m in build/objc.</p>
<p>Let&#8217;s ignore the implementation files (.cc and .m) since they are just code for what&#8217;s declared in the headers. We will now inspect the C++ header. You will see a lot of functions generated for you. They are mostly getters, setters and parsing functions like this:<br />
<code><br />
  inline bool has_character1() const;<br />
  inline void clear_character1();<br />
  static const int kCharacter1FieldNumber = 1;<br />
  inline const ::std::string&#038; character1() const;<br />
  inline void set_character1(const ::std::string&#038; value);<br />
  inline void set_character1(const char* value);<br />
  inline void set_character1(const char* value, size_t size);<br />
  inline ::std::string* mutable_character1();<br />
</code></p>
<p>And if you open up the Objective-C version of the header, you will see something like this:<br />
<code><br />
- (BOOL) hasCharacter1;<br />
- (NSString*) character1;<br />
- (StoryRequest_Builder*) setCharacter1:(NSString*) value;<br />
- (StoryRequest_Builder*) clearCharacter1;<br />
</code></p>
<p>They are just Objective-C methods equivalent to the C++ ones. As you can see, the proto files are defined regardless of which language you are going to use, and you will compile it into source files in the languages of your choosing. </p>
<p>In the <a href="http://www.kelvinkaodev.com/blog/2010/09/linking-the-protocol-buffer-static-library-into-the-xcode-project/">next post</a>, we&#8217;ll talk about how to get these files to compile in an Xcode project.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kelvinkaodev.com/blog/2010/09/generating-objective-c-classes-from-protocol-buffer/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Compiling the Protocol Buffer Compiler for Objective-C</title>
		<link>http://www.kelvinkaodev.com/blog/2010/08/compiling-the-protocol-buffer-compiler-for-objective-c/</link>
		<comments>http://www.kelvinkaodev.com/blog/2010/08/compiling-the-protocol-buffer-compiler-for-objective-c/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 21:32:27 +0000</pubDate>
		<dc:creator>Kelvin Kao</dc:creator>
				<category><![CDATA[Protobuf]]></category>

		<guid isPermaLink="false">http://www.kelvinkaodev.com/blog/?p=6</guid>
		<description><![CDATA[Recently, I looked into Protocol Buffers as a format of communicating between the iPhone client and the server. I didn&#8217;t find a lot of writing on this topic when it comes to Objective-C, so I figured I will write down &#8230; <a href="http://www.kelvinkaodev.com/blog/2010/08/compiling-the-protocol-buffer-compiler-for-objective-c/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently, I looked into <a href="http://code.google.com/apis/protocolbuffers/docs/overview.html">Protocol Buffers</a> as a format of communicating between the iPhone client and the server. I didn&#8217;t find a lot of writing on this topic when it comes to Objective-C, so I figured I will write down what I found out. So here you go.</p>
<p>Protocol Buffers (or Protobuf) is a structured way to encode data that was first created by Google for internal use, but has been released into the public later. It was certainly more concise than XML, but I personally still prefer something that&#8217;s more human readable like JSON. The nice thing about Protocol Buffers, though, is that many of the parsing is taken care of for you. You just need to define a proto file like this (example from Google&#8217;s official site):</p>
<p><code><br />
message Person {<br />
  required string name = 1;<br />
  required int32 id = 2;<br />
  optional string email = 3;</p>
<p>  enum PhoneType {<br />
    MOBILE = 0;<br />
    HOME = 1;<br />
    WORK = 2;<br />
  }</p>
<p>  message PhoneNumber {<br />
    required string number = 1;<br />
    optional PhoneType type = 2 [default = HOME];<br />
  }</p>
<p>  repeated PhoneNumber phone = 4;<br />
}<br />
</code></p>
<p>And then use the protocol buffer compiler (protoc) to compile a header and implementations for the accessors (getters) and mutators (setters). You can download the Windows binary to test out what protoc does by going to their <a href="http://code.google.com/p/protobuf/downloads/list">download page</a>. However, this protocol buffer compiler will only spit out C++, Java, and Python headers, but not Objective-C. This is only for you to get an overview of what the proto file looks like and what&#8217;s generated by protoc, so you get a better idea of whether you are doing it right when you do it in Objective-C.</p>
<p>So what do we do when it comes to Objective-C? Luckily, there&#8217;s a <a href="http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers">open-source port</a> by <a href="http://code.google.com/u/cyrus.najmabadi/">Cyrus Najmabadi</a> that you can find <a href="http://code.google.com/p/metasyntactic/wiki/ProtocolBuffers">here</a>. There&#8217;s no compiled binary though, so our first step will be compiling it. Basically all you have to do is follow the steps on that page. I am listing the steps here as well, with some additional comments from me.</p>
<p><strong>1. Download and unzip the latest ProtocolBuffers-*.*-Source.tar.gz file in the download section to &lt;install_directory&gt; on your computer.</strong></p>
<p>That should be relatively straight-forward.</p>
<p><strong>2. Navigate to &lt;install_directory&gt; and type the following commands</strong></p>
<p>For those who are only familiar with GUIs, we are about to run a shell script. How you do it is with the Terminal app which should be already installed on your Mac. To navigate to the directory, you use the command <code>cd</code>.</p>
<p><strong>3. ./autogen.sh</strong></p>
<p><strong>4. ./configure</strong></p>
<p>At this point, I was getting a <code>./configure: Permission denied</code>. I had to change the permission for execution by typing in<br />
<code>chmod +x configure</code><br />
Depending on what your set-up is like, you might or might not set the permissions differently according to your set-up. After running the configure script, a bunch of config files, many of which under gtest\config should be generated.</p>
<p><strong>5. make</strong></p>
<p>Now you run the make file. A bunch of files will be generated. What you want to look for is a file called protoc that should end up in your &lt;install_directory&gt;/src directory.</p>
<p>And we are done and have a brand-spanking-new protocol buffer compiler. I shall talk about the actual usage of this compiler in the <a href="http://www.kelvinkaodev.com/blog/2010/09/generating-objective-c-classes-from-protocol-buffer/">next post</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kelvinkaodev.com/blog/2010/08/compiling-the-protocol-buffer-compiler-for-objective-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>iPhone Developers LA Meet-up</title>
		<link>http://www.kelvinkaodev.com/blog/2010/08/iphone-developers-la-meet-up/</link>
		<comments>http://www.kelvinkaodev.com/blog/2010/08/iphone-developers-la-meet-up/#comments</comments>
		<pubDate>Thu, 19 Aug 2010 09:25:22 +0000</pubDate>
		<dc:creator>Kelvin Kao</dc:creator>
				<category><![CDATA[Events]]></category>

		<guid isPermaLink="false">http://www.kelvinkaodev.com/blog/?p=23</guid>
		<description><![CDATA[Last night I went to iPhone Developers LA&#8217;s first meet-up. We met at Coloft and PJ Cabrera, the author of Beginning iPhone Games Development, gave a little presentation on using UIKit and Cocos2D. I was going to come to the &#8230; <a href="http://www.kelvinkaodev.com/blog/2010/08/iphone-developers-la-meet-up/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Last night I went to iPhone Developers LA&#8217;s first meet-up. We met at <a href="http://www.coloft.com/">Coloft</a> and PJ Cabrera, the author of <a href="http://www.amazon.com/gp/product/1430225998?ie=UTF8&#038;tag=pupkao-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=1430225998">Beginning iPhone Games Development</a><img src="http://www.assoc-amazon.com/e/ir?t=pupkao-20&#038;l=as2&#038;o=1&#038;a=1430225998" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, gave a little presentation on using UIKit and Cocos2D. I was going to come to the meet-up whether there was a workshop or not, but if there was one, great. And it was given by the author of a book that I was considering buying, even better. Of course, later I realized that the publisher <a href="http://apress.com">Apress</a> was sponsoring the event so the first 15 people got the book for free. Hey, even better. Now I got a new book to read. I found the Apress book that I bought before, <a href="http://www.amazon.com/gp/product/1430224592?ie=UTF8&#038;tag=pupkao-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=1430224592">Beginning iPhone 3 Development: Exploring the iPhone SDK</a><img src="http://www.assoc-amazon.com/e/ir?t=pupkao-20&#038;l=as2&#038;o=1&#038;a=1430224592" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />, to be quite helpful so hopefully I&#8217;ll learn a lot from this book too.</p>
<p>As for the workshop, PJ gave an introduction of the UIKit and showed a simple code example that&#8217;s basically a skeleton version of Arkanoid. I wrote down a few notes, but for the most part I was already familiar with what was covered. Then we moved on to Cocos2D. We got a brief overview of Cocos2D and looked a little bit of code. This was something I was less familiar with and would like to learn more about. But of course, I knew it was unrealistic to expect to learn a lot of details when there were two big topics and only one hour of time.</p>
<p>I talked to the people that attended the event. Some of them were iOS developers, some were other kinds of programmers interested in platforms, and some were more on the business side than the dev side. So overall, a good mix of people. But this also meant that when the code was shown, some people were going &#8220;Ah, of course&#8221; and some had basically no understanding of what was on the screen. So that was the reality of presenting to a mixed crowd like that. I thought it was a nice overview by the speaker, but ultimately, to really learn anything, you just got to read more and have more trial-and-error on your own time. There&#8217;s no way around that.</p>
<p>But of course, the best thing about the event was to be able to mingle with the people there, and share the experiences. And it was just good to have contacts that you could reach out for potential collaboration or just to talk about what you are up to, since you do speak the same language. I am glad that an event like this is being put together.</p>
<p>And since I didn&#8217;t have business cards printed, I gave out these hand-made cards with doodles on them. I had fun making them. Hopefully it&#8217;s not too unprofessional. You can see pictures of all the cards <a href="http://www.puppetkaos.com/2010/08/new-business-cards/">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kelvinkaodev.com/blog/2010/08/iphone-developers-la-meet-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

