<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Junda Ong]]></title>
  <link href="http://samwize.com/atom.xml" rel="self"/>
  <link href="http://samwize.com/"/>
  <updated>2015-09-07T18:41:27+08:00</updated>
  <id>http://samwize.com/</id>
  <author>
    <name><![CDATA[Junda]]></name>
    <email><![CDATA[junda@just2us.com]]></email>
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[Fix npm/bower install with su]]></title>
    <link href="http://samwize.com/2015/09/07/fix-npm-slash-bower-install-with-su/"/>
    <updated>2015-09-07T18:32:23+08:00</updated>
    <id>http://samwize.com/2015/09/07/fix-npm-slash-bower-install-with-su</id>
    <content type="html"><![CDATA[<p>You should not be using <code>su</code> with <code>npm install</code> or <code>bower install</code>.</p>

<p>However, we understand when you see errors such as <strong>npm ERR! Error: EACCES, mkdir ..</strong>, you will relent to using your superadmin power..</p>

<!-- more -->


<p>In bower case, if you do, you will see warning message:</p>

<blockquote><p>Since bower is a user command, there is no need to execute it with superuser permissions.
If you&rsquo;re having permission errors when using bower without sudo, please spend a few minutes learning more about how your system should work and make any necessary repairs.</p></blockquote>

<p>The <a href="https://github.com/npm/npm/issues/5649">fix</a> is simple:</p>

<pre><code>sudo chown -R $USER:$GROUP ~/.npm
sudo chown -R $USER:$GROUP ~/.config
</code></pre>

<p>This makes the directory where npm and bower is using when installing packages to be given the correct permissions.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to host a website on Github Pages]]></title>
    <link href="http://samwize.com/2015/09/02/how-to-host-a-website-on-github-pages/"/>
    <updated>2015-09-02T22:45:32+08:00</updated>
    <id>http://samwize.com/2015/09/02/how-to-host-a-website-on-github-pages</id>
    <content type="html"><![CDATA[<p><a href="https://pages.github.com">GitHub Pages</a> provides FREE hosting of your (static) website.</p>

<p>This post is a guide on hosting your &ldquo;project&rdquo; repository with a custom domain.</p>

<!-- more -->


<h2>Your Local Repository</h2>

<p>Instead of a <code>master</code> branch, rename it to <code>gh-pages</code>.</p>

<p>In your root, have a <code>CNAME</code> file with the line <code>example.com</code>.</p>

<p>Commit and push.</p>

<h2>GitHub Pages</h2>

<p>Create a new repository on GitHub for the project.</p>

<pre><code>git remote add origin https://github.com/yourusername/yourproject.git
git push -u origin gh-pages
</code></pre>

<p>If you have an existing respository, create a new <code>gh-pages</code> branch, and make sure it is your default branch in your repos settings.</p>

<h2>Configure Domain Name</h2>

<p>GitHub does provide an <a href="https://help.github.com/articles/tips-for-configuring-an-a-record-with-your-dns-provider/">article</a> on how you can do it, but it is very confusing.</p>

<p>As an illustration, this is my configuration using <a href="http://www.namecheap.com/?aff=68466">namecheap</a>:</p>

<pre><code>@       192.30.252.153              A (Address)
@       192.30.252.154              A (Address)
www     yourusername.github.io.     CNAME (Alias)
</code></pre>

<p>Note: The <strong>CNAME (Alias)</strong> record is for the subdomain <strong>www</strong>. It is pointing to your github username, not the project name. GitHub will determine which project it is for via the CNAME file.</p>

<p>Wait for the DNS changes to propagate.</p>

<p>Or check with <code>dig example.com +nostats +nocomments +nocmd</code> if changes are propagated.</p>

<p>That&rsquo;s it. With GitHub Pages, you can host as many projects/websites as you want, for free!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Squashing Multiple Commits into One]]></title>
    <link href="http://samwize.com/2015/08/22/squashing-multiple-commits-into-one/"/>
    <updated>2015-08-22T13:30:02+08:00</updated>
    <id>http://samwize.com/2015/08/22/squashing-multiple-commits-into-one</id>
    <content type="html"><![CDATA[<p>A good tip for developers using Git is to squash multiple commmits into one.</p>

<p>Why do that?</p>

<p>If you ever work on a task and had multiple commits because you want to be <em>safe</em> and track the changes while work-in-progress, your commit history will be difficult to follow.</p>

<p>It will be hard (and waste of time) for code reviewer, or other developers, to go through multiple commits. There will be bound to be codes that you add, then remove or edit.</p>

<!-- more -->


<p>To <a href="http://ponyfoo.com/articles/git-github-hacks">squash the last 5 commits</a>, this is what you do:</p>

<pre><code>git reset HEAD~5
git add .
git commit -am "Here's the bug fix that closes #28"
git push --force
</code></pre>

<p>If you are using SourceTree, you can right click on the last commit before yours, select &ldquo;Reset master to this commit&rdquo;, using the mode &ldquo;Mixed&rdquo;. Then re-commit those changes again.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Reinstall Octopress on a new system]]></title>
    <link href="http://samwize.com/2015/08/13/reinstall-octopress-on-a-new-system/"/>
    <updated>2015-08-13T14:57:05+08:00</updated>
    <id>http://samwize.com/2015/08/13/reinstall-octopress-on-a-new-system</id>
    <content type="html"><![CDATA[<p>Instructions for reinstalling an existing Octopress on a new system, using this website (samwize.com) as an example:</p>

<!-- more -->


<pre><code>git clone https://github.com/samwize/samwize.github.com samwize.com
cd samwize.com
git checkout source
gem install bundler
# Restore the themes
git submodule update --init
cd .themes/greyshade
# Ensure custom remote https://github.com/samwize/greyshade is added
git checkout -b master2 samwize/master2
</code></pre>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to handle remote notification with background mode enabled]]></title>
    <link href="http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled/"/>
    <updated>2015-08-07T16:28:43+08:00</updated>
    <id>http://samwize.com/2015/08/07/how-to-handle-remote-notification-with-background-mode-enabled</id>
    <content type="html"><![CDATA[<p>iOS push notification has evolved greatly over the years, from a simple push with a alert string, to the now sophisticated system involving custom actions, localized string and background mode.</p>

<p>Reading the documentations <a href="https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/IPhoneOSClientImp.html">here</a>, <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/doc/uid/TP40006786-CH3-SW17">here</a> and <a href="https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW1">here</a>&hellip; provided clues everywhere on how it works.</p>

<p>This post is to explain what happens when a push reaches a device.</p>

<!-- more -->


<h2>Which delegate method is called?</h2>

<p>That is the question to answer when a push reaches a device.</p>

<p>Three possible callbacks:</p>

<ol>
<li><code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code></li>
<li><code>application:didFinishLaunchingWithOptions:</code></li>
<li>No callback</li>
</ol>


<p>There are <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html#//apple_ref/doc/uid/TP40006786-CH3-SW17">other callback methods in UIApplicationDelegate</a>, but we leave omit them in this discussion unless it is needed. Specifically they are:</p>

<ul>
<li><code>application:didReceiveRemoteNotification:</code> &ndash; This is the original API when push notification service is launched (iOS 3.0), but now it is superceded by <code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code> (iOS 7.0).</li>
<li><code>application:handleActionWithIdentifier:forRemoteNotification:completionHandler:</code> &ndash; This is for providing custom action that goes with a push notification (iOS 8.0). An advanced topic we shelf off for now.</li>
</ul>


<h2>Configuring The Payload &amp; Silent Push</h2>

<p>The <a href="https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html">push notification payload</a> consists of:</p>

<ul>
<li><code>alert</code> &ndash; the alert string and actions</li>
<li><code>badge</code></li>
<li><code>sound</code></li>
<li><code>content-available</code></li>
</ul>


<p>The key <code>content-available</code> is a new feature, and it is this key that makes <em>silent push</em> possible.</p>

<p>To enable, you also have to add <code>remote-notifcation</code> as your app <code>UIBackgroundModes</code> as described <a href="https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html#//apple_ref/doc/uid/TP40007072-CH4-SW1">here</a>.</p>

<p>This is what happens when <code>content-available</code> is in the payload:</p>

<ul>
<li>If app is <strong>Suspended</strong>, the system will bring it into <strong>Background</strong></li>
<li>If app was killed by user, nothing happens and app remains in <strong>Not Running</strong></li>
</ul>


<p>Read about <a href="https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html">app state changes</a>.</p>

<p>A potential is pitfall:</p>

<blockquote><p>You enable with <code>content-available=1</code>. But, it is WRONG to disable with <code>content-available=0</code>. To disable, you have to REMOVE the key in the payload.</p></blockquote>

<h2>Playing out the Scenarios</h2>

<p>With <code>content-available</code> enabled:</p>

<ol>
<li>App is in <strong>Foreground</strong>

<ul>
<li>No system alert shown</li>
<li><code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code> is called</li>
</ul>
</li>
<li>App is in <strong>Background</strong>

<ul>
<li>System alert is shown</li>
<li><code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code> is called</li>
</ul>
</li>
<li>App is in <strong>Suspended</strong>

<ul>
<li>App state changes to <strong>Background</strong></li>
<li>System alert is shown</li>
<li><code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code> is called</li>
</ul>
</li>
<li>App is <strong>Not Running</strong> because killed by user

<ul>
<li>System alert is shown</li>
<li>No callback is called</li>
</ul>
</li>
</ol>


<p>With <code>content-available</code> disabled (key removed):</p>

<ol>
<li>App is in <strong>Foreground</strong>

<ul>
<li>No system alert shown</li>
<li><code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code> is called</li>
</ul>
</li>
<li>App is in <strong>Background</strong> or <strong>Suspended</strong>

<ul>
<li>System alert is shown</li>
<li>No method is called, but when user tap on the push and the app is opened

<ul>
<li><code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code> is called</li>
</ul>
</li>
</ul>
</li>
<li>App is in <strong>Not Running</strong>

<ul>
<li>System alert is shown</li>
<li>No method is called, but when user tap on the push and the app is opened

<ul>
<li><code>application:didFinishLaunchingWithOptions:</code> then <code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code> are both called</li>
</ul>
</li>
</ul>
</li>
</ol>


<h2>Conclusion</h2>

<p>If app is in <strong>Foreground</strong>, the system will not show the alert and it is up to the app to display some UI after <code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code> is called.</p>

<p>Enabling <code>content-available</code> will put the app in <strong>Background</strong> (unless app was killed by user) and then call <code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code> immediately.</p>

<p>Whereas without <code>content-available</code>, app will remain in <strong>Suspended</strong>, and <code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code> is delayed until the app is opened.</p>

<p>Lastly, <code>application:didFinishLaunchingWithOptions:</code> is called only when the app is <strong>Not Running</strong>, and the user tap on the push alert. It will subsequently call <code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code>. Therefore, it is better to handle the push in <code>application:didReceiveRemoteNotification:fetchCompletionHandler:</code>, as that covers all scenarios.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to really clean your Xcode build]]></title>
    <link href="http://samwize.com/2015/07/10/how-to-really-clean-your-xcode-build/"/>
    <updated>2015-07-10T14:43:38+08:00</updated>
    <id>http://samwize.com/2015/07/10/how-to-really-clean-your-xcode-build</id>
    <content type="html"><![CDATA[<p>There are times when you have weird Xcode build errors eg. you are absolutely sure <em>that the referenced object</em> is in the project, yet Xcode could not find it.</p>

<p>Or compile errors such as <code>Include of non-modular header inside framework module</code>, when you know the framework works very fine.</p>

<p>You can try clean (Cmd + Shift + K) and build.</p>

<p>But the <strong>real way to clean Xcode</strong> is to delete folders in <code>~/Library/Developer/Xcode/DerivedData</code>. Delete folders with your app name, and also <code>ModuleCache</code>. That could well save your day.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Pitfall: Parse Circular Dependency Error and Bidirectional Relationship]]></title>
    <link href="http://samwize.com/2015/06/24/pitfall-parse-circular-dependency-error-and-bidirectional-relationship/"/>
    <updated>2015-06-24T12:26:11+08:00</updated>
    <id>http://samwize.com/2015/06/24/pitfall-parse-circular-dependency-error-and-bidirectional-relationship</id>
    <content type="html"><![CDATA[<p>When you save a <code>PFObject</code> and encounter the error <code>Found a circular dependency when saving</code>, it could be due to you setting a bidirectional/inverse relationship.</p>

<!-- more -->


<p>Take this example:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="c1">// You have 2 Parse Class Teacher and Student</span>
</span><span class='line'><span class="n">PFObject</span> <span class="o">*</span><span class="n">teacher</span><span class="p">,</span> <span class="n">student</span><span class="p">;</span>
</span><span class='line'><span class="c1">// You try to set up the bidirectional relationship</span>
</span><span class='line'><span class="n">teacher</span><span class="p">.</span><span class="n">student</span> <span class="o">=</span> <span class="n">student</span>
</span><span class='line'><span class="n">student</span><span class="p">.</span><span class="n">teacher</span> <span class="o">=</span> <span class="n">teacher</span>
</span></code></pre></td></tr></table></div></figure>


<p>Upon save, the code will throw the exception <code>Found a circular dependency when saving</code>. This is because <a href="https://parse.com/questions/bidirectional-relationship-one-to-many">bidirectional relationship is not supported</a> on Parse.</p>

<p>The solution requires 3 steps:</p>

<ul>
<li>Don&rsquo;t set up any of the relationship, and save <code>teacher</code> first. Saving will provide <code>teacher.objectId</code>.</li>
<li>Then save <code>student</code> with 1 directional relationship to <code>teacher</code>. Saving will provide <code>student.objectId</code>.</li>
<li>Then update <code>teacher</code> with the relation to <code>student</code>.</li>
</ul>


<p>This takes 3 API requests to set up.</p>

<p>Not good, but workaround the limitation of Parse.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Guide on using OCMock]]></title>
    <link href="http://samwize.com/2015/06/16/guide-on-using-ocmock/"/>
    <updated>2015-06-16T22:31:33+08:00</updated>
    <id>http://samwize.com/2015/06/16/guide-on-using-ocmock</id>
    <content type="html"><![CDATA[<p><a href="http://ocmock.org">OCMock</a> is a great framework for Objective-C (iOS or Mac OS) for test driven development.</p>

<p>It changes the behaviours of classes during testing.</p>

<p>This is a guide on how to use OCMock, and also raise some of the pitfalls.</p>

<!-- more -->


<h2>1.  Mock an instance method</h2>

<p>If you have a class <code>MyClass</code>, an instance <code>anObject</code>, and an instance method <code>foo</code>, you can change the return value of <code>foo</code> during testing:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="c1">// Create a mock for the class</span>
</span><span class='line'><span class="kt">id</span> <span class="n">myClassMock</span> <span class="o">=</span> <span class="n">OCMClassMock</span><span class="p">([</span><span class="n">MyClass</span> <span class="n">class</span><span class="p">]);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Change the method&#39;s behaviour for MyClass with a stub</span>
</span><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">myClassMock</span> <span class="n">foo</span><span class="p">]).</span><span class="n">andReturn</span><span class="p">(</span><span class="s">@&quot;bar&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// If you only want to stub for a specific value eg Force to return Apple for Green fruit</span>
</span><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">myClassMock</span> <span class="nl">fruitWithColor:</span><span class="s">@&quot;Green&quot;</span><span class="p">]).</span><span class="n">andReturn</span><span class="p">(</span><span class="s">@&quot;Apple&quot;</span><span class="p">);</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// If you want to return Apple for all fruits</span>
</span><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">myClassMock</span> <span class="nl">fruitWithColor:</span><span class="p">[</span><span class="n">OCMArg</span> <span class="n">any</span><span class="p">]]).</span><span class="n">andReturn</span><span class="p">(</span><span class="s">@&quot;Apple&quot;</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<h2>2. Mock a class method</h2>

<p>Mocking a class method is exactly the same as mocking for instance method.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="kt">id</span> <span class="n">myClassMock</span> <span class="o">=</span> <span class="n">OCMClassMock</span><span class="p">([</span><span class="n">MyClass</span> <span class="n">class</span><span class="p">]);</span>
</span><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">myClassMock</span> <span class="n">classMethod</span><span class="p">]).</span><span class="n">andReturn</span><span class="p">(</span><span class="s">@&quot;bar&quot;</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<h2>3. Mock an object (Partial mock)</h2>

<p>If you have the instance <code>anObject</code>, you can use <code>OCMPartialMock</code> to create the mock.</p>

<p>This mock in effect is the same as mocking the class.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="kt">id</span> <span class="n">partialMock</span> <span class="o">=</span> <span class="n">OCMPartialMock</span><span class="p">(</span><span class="n">anObject</span><span class="p">);</span>
</span><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">partialMock</span> <span class="n">foo</span><span class="p">]).</span><span class="n">andReturn</span><span class="p">(</span><span class="s">@&quot;bar&quot;</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>Pitfall: You might assume partial mock applies to only that instance <code>anObject</code>. That&rsquo;s wrong. If you have <code>anotherObject2</code>, it will be affected by whatever was mocked because partial mock actually mock the whole class.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="p">[</span><span class="n">anObject</span> <span class="n">foo</span><span class="p">];</span>     <span class="c1">// Will return @&quot;bar&quot;</span>
</span><span class='line'><span class="p">[</span><span class="n">anObject2</span> <span class="n">foo</span><span class="p">];</span>    <span class="c1">// Will return @&quot;bar&quot; too!</span>
</span></code></pre></td></tr></table></div></figure>


<h2>4. Stop mocking</h2>

<p>You can stop mocking to return to the real object and reset it&rsquo;s behaviours.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="p">[</span><span class="n">anObject</span> <span class="n">foo</span><span class="p">];</span>     <span class="c1">// Will return @&quot;bar&quot;</span>
</span><span class='line'><span class="p">[</span><span class="n">anObject</span> <span class="n">stopMocking</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="n">anObject2</span> <span class="n">foo</span><span class="p">];</span>    <span class="c1">// Will return as per normal</span>
</span></code></pre></td></tr></table></div></figure>


<p>We use the example of partial mock, but you could call <code>[MyClass stopMocking]</code> and it will have the same effects.</p>

<h2>5. Stub methods and chaining</h2>

<p>You can stub the methods with different actions: return object, return native values, throw, post notification, forward to real object or do nothing.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">mock</span> <span class="n">someMethod</span><span class="p">]).</span><span class="n">andReturn</span><span class="p">(</span><span class="n">anObject</span><span class="p">);</span>
</span><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">mock</span> <span class="n">aMethodReturningABoolean</span><span class="p">]).</span><span class="n">andReturn</span><span class="p">(</span><span class="n">YES</span><span class="p">);</span>
</span><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">mock</span> <span class="n">someMethod</span><span class="p">]).</span><span class="n">andThrow</span><span class="p">(</span><span class="n">anException</span><span class="p">);</span>
</span><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">mock</span> <span class="n">someMethod</span><span class="p">]).</span><span class="n">andPost</span><span class="p">(</span><span class="n">aNotification</span><span class="p">);</span>
</span><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">mock</span> <span class="n">someMethod</span><span class="p">]).</span><span class="n">andForwardToRealObject</span><span class="p">();</span>
</span><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">mock</span> <span class="n">someMethod</span><span class="p">]).</span><span class="n">andDo</span><span class="p">(</span><span class="n">anInvocation</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<p>You can also <a href="http://ocmock.org/reference/#stubing-methods">chain</a> <code>andPost</code> and <code>andReturn</code> with other actions.</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">OCMStub</span><span class="p">([</span><span class="n">mock</span> <span class="n">someMethod</span><span class="p">]).</span><span class="n">andPost</span><span class="p">(</span><span class="n">aNotification</span><span class="p">).</span><span class="n">andReturn</span><span class="p">(</span><span class="n">aValue</span><span class="p">);</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Limitations &ndash; Especially Core Data</h2>

<p>Not only those <a href="http://ocmock.org/reference/#limitations">listed</a> on OCMock,
but there are other limitations.</p>

<p>The most severe is that you cannot mock a <code>NSManagedObject</code>.</p>

<p>There is a way to <a href="http://hackazach.net/code/2014/04/13/ios-testing-and-core-data/">get around</a> it, but it is better that you never, never mock Core Data.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to download all WWDC 2015 PDF Slides Only]]></title>
    <link href="http://samwize.com/2015/06/14/how-to-download-all-wwdc-2015-pdf-slides-only/"/>
    <updated>2015-06-14T11:48:11+08:00</updated>
    <id>http://samwize.com/2015/06/14/how-to-download-all-wwdc-2015-pdf-slides-only</id>
    <content type="html"><![CDATA[<p>Last year, I provided the script to <a href="http://samwize.com/2014/06/06/download-all-wwdc-2014-pdf-slides-only/">download all PDFs for WWDC 2014</a>.</p>

<p>This year, for WWDC 2015, I have <a href="https://github.com/samwize/wwdc-downloader">forked ohoachuck&rsquo;s wwdc-downloader</a> to provide an option to download only the PDFs and sample codes.</p>

<p>To use, <a href="https://github.com/samwize/wwdc-downloader">download the script</a> and run:</p>

<pre><code>./wwdcVideoPDFGet-curlVersion.sh -p
</code></pre>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Pitfall: Using UIActionSheet (or UIAlertController) and first tap does not work]]></title>
    <link href="http://samwize.com/2015/06/04/pitfall-using-uiactionsheet-or-uialertcontroller-and-first-tap-does-not-work/"/>
    <updated>2015-06-04T11:41:53+08:00</updated>
    <id>http://samwize.com/2015/06/04/pitfall-using-uiactionsheet-or-uialertcontroller-and-first-tap-does-not-work</id>
    <content type="html"><![CDATA[<p>There is constantly bug with <code>UIActionSheet</code>.</p>

<p>Symptoms: Tapping on the action sheet does not work on the first tap. The subsequent tap works as expected.</p>

<!-- more -->


<h2>Since long ago..</h2>

<p>Back in 2009, <a href="http://stackoverflow.com/q/1197746/242682">UIActionSheet has a strange behaviour</a>, whereby the tap area seems to have moved.</p>

<p>Solutions such as using <code>showFromTabBar</code> or show in window helped to solve Apple&rsquo;s bug.</p>

<p>It wasn&rsquo;t a good experience to have such glaring UI bug in Apple&rsquo;s SDK.</p>

<h2>iOS 8</h2>

<p>In iOS 8, Apple introduced <code>UIAlertController</code>.</p>

<p>I have once again encountered strange behaviour, whereby I need to tap twice before I can tap a button on the action sheet.</p>

<p>Turned out the issue lies with my <code>UITextField</code>, which was the first responder before I present the action sheet.</p>

<p>When I present the action sheet, UIKit will dismiss the keyboard, then present the action sheet. But it didn&rsquo;t do so properly. The first tap will somehow be ignored.</p>

<p>Solution:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="p">[</span><span class="n">textfield</span> <span class="n">resignFirstResponder</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="n">self</span> <span class="nl">presentViewController:</span><span class="n">alert</span> <span class="nl">animated:</span><span class="n">YES</span> <span class="nl">completion:</span><span class="nb">nil</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>Before presenting the action sheet, manually <code>resignFirstResponder</code> first.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Pitfall: Creating Parent/Abstract Entitiy in Core Data]]></title>
    <link href="http://samwize.com/2015/06/02/pitfall-creating-parent-slash-abstract-entitiy-in-core-data/"/>
    <updated>2015-06-02T11:05:38+08:00</updated>
    <id>http://samwize.com/2015/06/02/pitfall-creating-parent-slash-abstract-entitiy-in-core-data</id>
    <content type="html"><![CDATA[<p>If you design your data model with parent entity, then you are most likely doing it wrong, unknowingly.</p>

<p>Because when you do so, Core Data will create 1 common table for ALL the children entities!</p>

<p>This is bad design (on Core Data part) and it will slow down the performance.</p>

<p>Read more about this under the section <a href="http://www.objc.io/issue-4/core-data-models-and-model-objects.html">Entity Hierarchy vs. Class Hierarchy</a>.</p>

<!-- more -->


<h2>Designing a flat Entity Hierarchy</h2>

<p>When you have common attributes (such as <code>createdAt</code>, <code>updatedAt</code>) between entities, most likely you only want to enjoy the convenient of using them in your code.</p>

<p>Steps:</p>

<ul>
<li>Copy and paste the common attributes across entities</li>
<li>Create a parent <code>NSManagedObject</code> that declare the common attributes</li>
<li>Subclass with the parent <code>NSManagedObject</code></li>
</ul>


<h2>Using mogenerator</h2>

<p>When using mogenerator, the class files are auto generated, and it is generated according to the schema design.</p>

<p>This complicate things..</p>

<p>To fix:</p>

<ul>
<li>Create the parent entity with the common attributes</li>
<li>Generate</li>
<li>In all the <code>_Entity.h</code>, subclass with the parent class instead of <code>NSManagedObject</code>, and remove codes on the common attributes</li>
<li>Remove <code>objectID</code> in the parenty entity class (because each child has the same attribute)</li>
</ul>


<p>Tedious workaround, but I don&rsquo;t have a better way to work with mogenerator.</p>

<p>If you know, let me know :)</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to create retina banner ads for MoPub line item]]></title>
    <link href="http://samwize.com/2015/05/25/how-to-create-retina-banner-ads-for-mopub-line-item/"/>
    <updated>2015-05-25T16:04:49+08:00</updated>
    <id>http://samwize.com/2015/05/25/how-to-create-retina-banner-ads-for-mopub-line-item</id>
    <content type="html"><![CDATA[<p>If you have been using MoPub to serve your banner ads eg. for backfills, it is important that you know it is possible to serve retina sizes @2x.</p>

<p>This means you can upload 640x100 instead of 320x50.</p>

<p>MoPub documentation has been poor to highlight this capability. It is found only in their <a href="https://twittercommunity.com/t/is-it-possible-to-have-banner-ads-optimized-for-retina-devices/6914">forum</a>.</p>

<!-- more -->


<p>Anyhow, the steps as follows:</p>

<ol>
<li>Create a new creative with the 320x50 banner format</li>
<li>Set the creative type to <strong>Image</strong>, and upload your retina image file</li>
<li>Save and pause this creative</li>
<li>Preview on the paused creative, right click and copy the image URL</li>
<li>Create another NEW creative, but set the creative type to <strong>HTML</strong></li>
<li><p>Copy the following into the HTML section:</p>

<p> <code>
 &lt;a href="CLICK URL HERE" target="_blank"&gt; &lt;img src="IMAGE URL HERE" width=100%&gt; &lt;/a&gt;
</code></p></li>
</ol>


<p>It is a straight forward <code>&lt;a&gt;</code> tag, which makes the image fills the width of the banner.</p>

<p>Note: The paused creative is simply to get the image URL, which is used by the actual creative using HTML mode. Do not delete the paused creative.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to make an expandable header image in a UIScrollView]]></title>
    <link href="http://samwize.com/2015/05/06/how-to-make-an-expandable-header-image-in-a-uiscrollview/"/>
    <updated>2015-05-06T11:07:27+08:00</updated>
    <id>http://samwize.com/2015/05/06/how-to-make-an-expandable-header-image-in-a-uiscrollview</id>
    <content type="html"><![CDATA[<p>A common UI design pattern is to make an image fill the header as you scroll beyond the top, like this:</p>

<p><img class="center" src="http://samwize.com/images/expandable-header-uiscrollview.gif"></p>

<p>This post will show you how to do that, easily.</p>

<!-- more -->


<p>Implement the <code>UIScrollViewDelegate</code> method as follows:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">scrollViewDidScroll:</span><span class="p">(</span><span class="n">UIScrollView</span> <span class="o">*</span><span class="p">)</span><span class="nv">scrollView</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">scrollView</span><span class="p">.</span><span class="n">contentOffset</span><span class="p">.</span><span class="n">y</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">CGAffineTransform</span> <span class="n">translate</span> <span class="o">=</span> <span class="n">CGAffineTransformMakeTranslation</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">scrollView</span><span class="p">.</span><span class="n">contentOffset</span><span class="p">.</span><span class="n">y</span><span class="o">/</span><span class="mf">2.0</span><span class="p">);</span>
</span><span class='line'>        <span class="n">CGFloat</span> <span class="n">orgHeight</span> <span class="o">=</span> <span class="n">_photoHeightConstraint</span><span class="p">.</span><span class="n">constant</span><span class="p">;</span>
</span><span class='line'>        <span class="n">CGFloat</span> <span class="n">scaleFactor</span> <span class="o">=</span> <span class="p">(</span><span class="n">orgHeight</span> <span class="o">-</span> <span class="n">scrollView</span><span class="p">.</span><span class="n">contentOffset</span><span class="p">.</span><span class="n">y</span><span class="p">)</span> <span class="o">/</span> <span class="n">orgHeight</span><span class="p">;</span>
</span><span class='line'>        <span class="n">CGAffineTransform</span> <span class="n">translateAndZoom</span> <span class="o">=</span> <span class="n">CGAffineTransformScale</span><span class="p">(</span><span class="n">translate</span><span class="p">,</span> <span class="n">scaleFactor</span><span class="p">,</span> <span class="n">scaleFactor</span><span class="p">);</span>
</span><span class='line'>        <span class="n">_photoImageView</span><span class="p">.</span><span class="n">transform</span> <span class="o">=</span> <span class="n">translateAndZoom</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In the code above <code>_photoImageView</code> is the image view that we want to make expandable, and <code>_photoHeightConstraint</code> is a NSLayoutConstraint for the height of <code>_photoImageView</code>.</p>

<p>That&rsquo;s all for the code!</p>

<p>When the scroll view <code>contentOffset</code> changes, perform a zoom and translate transformation.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[How to handle keyboard hide/show events in your view]]></title>
    <link href="http://samwize.com/2015/04/29/how-to-handle-keyboard-hide-slash-show-in-your-view/"/>
    <updated>2015-04-29T09:44:36+08:00</updated>
    <id>http://samwize.com/2015/04/29/how-to-handle-keyboard-hide-slash-show-in-your-view</id>
    <content type="html"><![CDATA[<p>It is a very common task in iOS when you have eg <code>UITextField</code>, which on tap brings up the keyboard, and in the process obscure part of your view.</p>

<p>Unfortuantely, iOS leaves this situation to the developer to handle.</p>

<p>If you have a simple form for users to enter some text, then I suggest using <a href="https://github.com/hackiftekhar/IQKeyboardManager">IQKeyboardManager</a> to this common problem.</p>

<!-- more -->


<h2>IQKeyboardManager</h2>

<p>This <a href="https://github.com/hackiftekhar/IQKeyboardManager">open soure library by hackiftekhar</a> is a terrific component for iOS.</p>

<p>Just install (eg via Cocoapods), and that&rsquo;s it.</p>

<p>Zero code needed.</p>

<p>How the library achieves this is by having it&rsquo;s manager listening to the standard <code>UIKeyboardWillShowNotification</code> during app initialization, and then adjusting the whole window view when necessary.</p>

<p>It&rsquo;s a shame iOS didn&rsquo;t ship with a capability like that.</p>

<h2>How to manually handle the keyboard</h2>

<p>While IQKeyboardManager is great, sometimes you want to manage these keyboard events manually.</p>

<p>For example, if you have a <code>UITextView</code> for entering paragraph of text, and you want only the insets to change when keyboard is showing/hiding, then IQKeyboardManager isn&rsquo;t configured for that.</p>

<p>Or for example, if you have a toolbar that you want to stick just above the keyboard, then IQKeyboardManager isn&rsquo;t suited for that.</p>

<p>This is the code to manually handle, and with proper animations that synchronize with keyboard animation:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
<span class='line-number'>56</span>
<span class='line-number'>57</span>
<span class='line-number'>58</span>
<span class='line-number'>59</span>
<span class='line-number'>60</span>
<span class='line-number'>61</span>
<span class='line-number'>62</span>
<span class='line-number'>63</span>
<span class='line-number'>64</span>
<span class='line-number'>65</span>
<span class='line-number'>66</span>
<span class='line-number'>67</span>
<span class='line-number'>68</span>
<span class='line-number'>69</span>
<span class='line-number'>70</span>
<span class='line-number'>71</span>
<span class='line-number'>72</span>
<span class='line-number'>73</span>
<span class='line-number'>74</span>
<span class='line-number'>75</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">viewDidLoad</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="n">super</span> <span class="n">viewDidLoad</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Listen to the standard keyboard notifications</span>
</span><span class='line'>    <span class="p">[[</span><span class="n">NSNotificationCenter</span> <span class="n">defaultCenter</span><span class="p">]</span> <span class="nl">addObserver:</span><span class="n">self</span>
</span><span class='line'>                                             <span class="nl">selector:</span><span class="k">@selector</span><span class="p">(</span><span class="nl">keyboardWillShow:</span><span class="p">)</span>
</span><span class='line'>                                                 <span class="nl">name:</span><span class="n">UIKeyboardWillShowNotification</span> <span class="nl">object:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="p">[[</span><span class="n">NSNotificationCenter</span> <span class="n">defaultCenter</span><span class="p">]</span> <span class="nl">addObserver:</span><span class="n">self</span>
</span><span class='line'>                                             <span class="nl">selector:</span><span class="k">@selector</span><span class="p">(</span><span class="nl">keyboardWillHide:</span><span class="p">)</span>
</span><span class='line'>                                                 <span class="nl">name:</span><span class="n">UIKeyboardWillHideNotification</span> <span class="nl">object:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">dealloc</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">[[</span><span class="n">NSNotificationCenter</span> <span class="n">defaultCenter</span><span class="p">]</span> <span class="nl">removeObserver:</span><span class="n">self</span>
</span><span class='line'>                                                    <span class="nl">name:</span><span class="n">UIKeyboardDidShowNotification</span>
</span><span class='line'>                                                  <span class="nl">object:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[[</span><span class="n">NSNotificationCenter</span> <span class="n">defaultCenter</span><span class="p">]</span> <span class="nl">removeObserver:</span><span class="n">self</span>
</span><span class='line'>                                                    <span class="nl">name:</span><span class="n">UIKeyboardWillHideNotification</span>
</span><span class='line'>                                                  <span class="nl">object:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">keyboardWillShow:</span><span class="p">(</span><span class="n">NSNotification</span><span class="o">*</span><span class="p">)</span><span class="nv">aNotification</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="n">self</span> <span class="nl">adjustForKeyboard:</span><span class="n">aNotification</span> <span class="nl">showing:</span><span class="n">YES</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">keyboardWillHide:</span><span class="p">(</span><span class="n">NSNotification</span><span class="o">*</span><span class="p">)</span><span class="nv">aNotification</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="n">self</span> <span class="nl">adjustForKeyboard:</span><span class="n">aNotification</span> <span class="nl">showing:</span><span class="n">NO</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">adjustForKeyboard:</span><span class="p">(</span><span class="n">NSNotification</span><span class="o">*</span><span class="p">)</span><span class="nv">aNotification</span> <span class="nf">showing:</span><span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nv">showing</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">NSDictionary</span><span class="o">*</span> <span class="n">info</span> <span class="o">=</span> <span class="p">[</span><span class="n">aNotification</span> <span class="n">userInfo</span><span class="p">];</span>
</span><span class='line'>    <span class="n">CGRect</span> <span class="n">keyboardFrame</span> <span class="o">=</span> <span class="p">[[</span><span class="n">info</span> <span class="nl">objectForKey:</span><span class="n">UIKeyboardFrameEndUserInfoKey</span><span class="p">]</span> <span class="n">CGRectValue</span><span class="p">];</span>
</span><span class='line'>    <span class="n">CGRect</span> <span class="n">convertedFrame</span> <span class="o">=</span> <span class="p">[</span><span class="n">self</span><span class="p">.</span><span class="n">view</span> <span class="nl">convertRect:</span><span class="n">keyboardFrame</span> <span class="nl">fromView:</span><span class="n">self</span><span class="p">.</span><span class="n">view</span><span class="p">.</span><span class="n">window</span><span class="p">];</span>
</span><span class='line'>    <span class="n">CGSize</span> <span class="n">kbSize</span> <span class="o">=</span> <span class="n">convertedFrame</span><span class="p">.</span><span class="n">size</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// 1. Adjust for the UITextView</span>
</span><span class='line'>    <span class="n">UIEdgeInsets</span> <span class="n">newInsets</span><span class="p">;</span>
</span><span class='line'>    <span class="c1">// If keyboard showing, add an inset</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">showing</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="n">newInsets</span> <span class="o">=</span> <span class="n">UIEdgeInsetsMake</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">kbSize</span><span class="p">.</span><span class="n">height</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// Else keyboard hiding, rest the inset</span>
</span><span class='line'>        <span class="n">newInsets</span> <span class="o">=</span> <span class="n">UIEdgeInsetsMake</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">);</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>    <span class="n">_textView</span><span class="p">.</span><span class="n">contentInset</span> <span class="o">=</span> <span class="n">newInsets</span><span class="p">;</span>
</span><span class='line'>    <span class="n">_textView</span><span class="p">.</span><span class="n">scrollIndicatorInsets</span> <span class="o">=</span> <span class="n">newInsets</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// 2. Adjust toolbar with animation</span>
</span><span class='line'>    <span class="c1">// Get keyboard animation info</span>
</span><span class='line'>    <span class="n">NSTimeInterval</span> <span class="n">animationDuration</span><span class="p">;</span>
</span><span class='line'>    <span class="n">UIViewAnimationCurve</span> <span class="n">animationCurve</span><span class="p">;</span>
</span><span class='line'>    <span class="p">[[</span><span class="n">info</span> <span class="nl">objectForKey:</span><span class="n">UIKeyboardAnimationCurveUserInfoKey</span><span class="p">]</span> <span class="nl">getValue:</span><span class="o">&amp;</span><span class="n">animationCurve</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[[</span><span class="n">info</span> <span class="nl">objectForKey:</span><span class="n">UIKeyboardAnimationDurationUserInfoKey</span><span class="p">]</span> <span class="nl">getValue:</span><span class="o">&amp;</span><span class="n">animationDuration</span><span class="p">];</span>
</span><span class='line'>    <span class="c1">// A simple convertion of UIViewAnimationCurve to UIViewAnimationOptions</span>
</span><span class='line'>    <span class="n">UIViewAnimationOptions</span> <span class="n">animationOption</span> <span class="o">=</span> <span class="n">animationCurve</span> <span class="o">&lt;&lt;</span> <span class="mi">16</span><span class="p">;</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">animationDuration</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
</span><span class='line'>        <span class="n">animationDuration</span> <span class="o">=</span> <span class="mf">0.1</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Start animation</span>
</span><span class='line'>    <span class="p">[</span><span class="n">self</span><span class="p">.</span><span class="n">view</span> <span class="n">layoutIfNeeded</span><span class="p">];</span>
</span><span class='line'>    <span class="p">[</span><span class="n">UIView</span> <span class="nl">animateWithDuration:</span><span class="n">animationDuration</span>
</span><span class='line'>                          <span class="nl">delay:</span><span class="mi">0</span>
</span><span class='line'>                        <span class="nl">options:</span><span class="n">animationOption</span>
</span><span class='line'>                     <span class="nl">animations:</span><span class="o">^</span><span class="p">{</span>
</span><span class='line'>
</span><span class='line'>                         <span class="c1">// Adjust for toolbar</span>
</span><span class='line'>                         <span class="k">if</span> <span class="p">(</span><span class="n">showing</span><span class="p">)</span>
</span><span class='line'>                             <span class="n">_toolbarBottomSpace</span><span class="p">.</span><span class="n">constant</span> <span class="o">=</span> <span class="n">kbSize</span><span class="p">.</span><span class="n">height</span><span class="p">;</span>
</span><span class='line'>                         <span class="k">else</span>
</span><span class='line'>                             <span class="n">_toolbarBottomSpace</span><span class="p">.</span><span class="n">constant</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span><span class='line'>                         <span class="p">[</span><span class="n">self</span><span class="p">.</span><span class="n">view</span> <span class="n">layoutIfNeeded</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>                     <span class="p">}</span> <span class="nl">completion:</span><span class="nb">nil</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In <code>viewDidLoad</code>, it is where we add the view controller as the observer to the keyboard events. Subsequently in <code>dealloc</code>, remove the observer.</p>

<p>The gist is in <code>adjustForKeyboard:showing:</code>.</p>

<p><code>aNotification</code> contains a dictionary about the keyboard size and animation details.</p>

<p>A little trick is needed to convert <code>UIViewAnimationCurve</code> to <code>UIViewAnimationOptions</code>, which is needed for the new animation code.</p>

<p>For the toolbar, we change <code>_toolbarBottomSpace</code>, which is a vertical spacing constraint for the toolbar view to the bottom layout guide. You need to create this constraint on your storyboard (with autolayout).</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Using appledoc to generate documentation for project]]></title>
    <link href="http://samwize.com/2015/04/23/using-appledoc-to-generate-documentation-for-project/"/>
    <updated>2015-04-23T11:00:34+08:00</updated>
    <id>http://samwize.com/2015/04/23/using-appledoc-to-generate-documentation-for-project</id>
    <content type="html"><![CDATA[<p>It is good practise to generate documentation for your project.</p>

<p><a href="https://github.com/tomaz/appledoc">appledoc</a> is a tool to help generating the HTML docs from your code.</p>

<p>This is a guide on how to do it.</p>

<!-- more -->


<h2>Install VVDocumenter-Xcode</h2>

<p>Firstly, install <a href="https://github.com/onevcat/VVDocumenter-Xcode">this Xcode plugin</a> to help you with generating Javadoc style by simply typing <code>///</code> (3 forward slashes).</p>

<pre><code>git clone https://github.com/onevcat/VVDocumenter-Xcode.git
</code></pre>

<p>Open the project and build.</p>

<p>Restart Xcode.</p>

<p>Typing <code>///</code> above a method should now generate the template for you to document on a method.</p>

<h2>Install appledoc</h2>

<p>Install <a href="https://github.com/tomaz/appledoc">appledoc</a> and build it.</p>

<pre><code>git clone https://github.com/tomaz/appledoc.git
sudo sh install-appledoc.sh
</code></pre>

<p>Note: <code>brew install appledoc</code> doesn&rsquo;t work with Yosemite as of April 2015, so git clone and build is the way to go.</p>

<h2>Create a AppledocSettings.plist</h2>

<p>You can refer to a <a href="https://github.com/tomaz/appledoc/wiki/appledoc-docs-examples-basic">basic use of appledoc</a> at this point, but a basic use would have a number of parameters to the command.</p>

<p>It is better to create a plist to contain the parameters.</p>

<p>The default plist for a project is <code>AppledocSettings.plist</code>.</p>

<p>The plist for my project looks like this:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;--project-name&lt;/key&gt;
    &lt;string&gt;Jade&lt;/string&gt;
    &lt;key&gt;--project-company&lt;/key&gt;
    &lt;string&gt;Just2us&lt;/string&gt;
    &lt;key&gt;--company-id&lt;/key&gt;
    &lt;string&gt;com.justus&lt;/string&gt;

    &lt;key&gt;--output&lt;/key&gt;
    &lt;string&gt;Docs&lt;/string&gt;

    &lt;key&gt;--create-docset&lt;/key&gt;
    &lt;false/&gt;

    &lt;key&gt;--ignore&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;Pods&lt;/string&gt;
        &lt;string&gt;Libs&lt;/string&gt;
        &lt;string&gt;JadeTests&lt;/string&gt;
        &lt;string&gt;*.framework&lt;/string&gt;
    &lt;/array&gt;

&lt;/dict&gt;
&lt;/plist&gt;
</code></pre>

<p>I have some basic information about the project and the company. Then I am generating the HTML in <code>Docs</code>, without docset, and ignoring some folders such as Pods, tests and frameworks.</p>

<p>With that, in your project directory, run</p>

<pre><code>appledoc .
</code></pre>

<p>That&rsquo;s it!</p>

<p>The HTML will be in <code>Docs</code>.</p>

<h2>Advanced</h2>

<p>There are some <a href="https://github.com/tomaz/appledoc/wiki/appledoc-docs-examples-advanced">advanced commands</a> and good guide about publishing from <a href="http://www.cocoanetics.com/2011/11/amazing-apple-like-documentation/">Cocoanetics</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Guide to using Unwind Segues]]></title>
    <link href="http://samwize.com/2015/04/23/guide-to-using-unwind-segues/"/>
    <updated>2015-04-23T09:46:55+08:00</updated>
    <id>http://samwize.com/2015/04/23/guide-to-using-unwind-segues</id>
    <content type="html"><![CDATA[<p>Unwind segue is a very nice concept in iOS to &ldquo;go back&rdquo; to a previous view controller.</p>

<p>There is an <a href="https://developer.apple.com/library/ios/technotes/tn2298/_index.html">Apple Guide</a> to using it, and a good <a href="http://stackoverflow.com/a/15839298/242682">in a nutshell</a> about it.</p>

<p>Here, I will share how I use it in real world applications.</p>

<!-- more -->


<h2>Basics and Pitfalls</h2>

<ul>
<li><p>You create this method in the view controller which let others unwind to it:</p>

<p>  <code>- (IBAction)unwindToHere:(UIStoryboardSegue*)sender {...}</code></p></li>
<li><p>The view controller MUST be present in the navigation hierarchy. Obvious perhaps, but good to note unwind is to &ldquo;go back&rdquo;, not jump to anywhere.</p></li>
<li>The view controller should have appeared before performing other segues, otherwise you will encounter <a href="http://stackoverflow.com/q/20250481/242682">Warning: Attempt to present on whose view is not in the window hierarchy!</a></li>
</ul>


<h2>How I use unwind</h2>

<p>I always have a <code>FirstViewController</code> which is the initial view controller on my storyboard.</p>

<p>This <code>FirstViewController</code> is like a coordinator to other view controllers such as Login, Main, etc..</p>

<p>The advantage of having that is since it is the initial view controller, it will always be in the navigation hierarchy. And thus, it is a good candidate to unwind to (will always succeed!).</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">@interface</span> <span class="nc">FirstViewController</span> <span class="p">()</span>
</span><span class='line'><span class="c1">// Use this to coordinate unwind</span>
</span><span class='line'><span class="k">@property</span> <span class="p">(</span><span class="n">nonatomic</span><span class="p">,</span> <span class="n">strong</span><span class="p">)</span> <span class="n">NSString</span> <span class="o">*</span><span class="n">segueIdentifierToUnwindTo</span><span class="p">;</span>
</span><span class='line'><span class="k">@end</span>
</span><span class='line'>
</span><span class='line'><span class="k">@implementation</span> <span class="nc">FirstViewController</span>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">viewDidAppear:</span><span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nv">animated</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="n">super</span> <span class="nl">viewDidAppear:</span><span class="n">animated</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Handle unwind</span>
</span><span class='line'>    <span class="k">if</span> <span class="p">(</span><span class="n">_segueIdentifierToUnwindTo</span><span class="p">)</span> <span class="p">{</span>
</span><span class='line'>        <span class="p">[</span><span class="n">self</span> <span class="nl">performSegueWithIdentifier:</span><span class="n">_segueIdentifierToUnwindTo</span> <span class="nl">sender:</span><span class="n">self</span><span class="p">];</span>
</span><span class='line'>        <span class="n">self</span><span class="p">.</span><span class="n">segueIdentifierToUnwindTo</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">;</span>   <span class="c1">// reset</span>
</span><span class='line'>        <span class="k">return</span><span class="p">;</span>
</span><span class='line'>    <span class="p">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="c1">// Logics to segue </span>
</span><span class='line'>    <span class="c1">// ...</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Example of an unwind segue &quot;gotoLogin&quot;</span>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">IBAction</span><span class="p">)</span><span class="nf">gotoLogin:</span><span class="p">(</span><span class="n">UIStoryboardSegue</span><span class="o">*</span><span class="p">)</span><span class="nv">sender</span> <span class="p">{</span>
</span><span class='line'>    <span class="c1">// Don&#39;t perform segue directly here, because in unwind, this view is not in window hierarchy yet!</span>
</span><span class='line'>    <span class="c1">// [self performSegueWithIdentifier:@&quot;Login&quot; sender:self];</span>
</span><span class='line'>    <span class="n">self</span><span class="p">.</span><span class="n">segueIdentifierToUnwindTo</span> <span class="o">=</span> <span class="s">@&quot;Login&quot;</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="c1">// Example of another unwind segue &quot;gotoMain&quot;</span>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">IBAction</span><span class="p">)</span><span class="nf">gotoMain:</span><span class="p">(</span><span class="n">UIStoryboardSegue</span><span class="o">*</span><span class="p">)</span><span class="nv">sender</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">self</span><span class="p">.</span><span class="n">segueIdentifierToUnwindTo</span> <span class="o">=</span> <span class="s">@&quot;Main&quot;</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<p>In the example above, I have 2 unwind segues <code>gotoLogin</code> and <code>gotoMain</code>. You could have as many unwind segues.</p>

<p>When you add an unwind segue to a scene (read <a href="https://developer.apple.com/library/ios/technotes/tn2298/_index.html">here</a> on how), you could choose <code>gotoLogin</code>. That is bascially asking to unwind to <code>FirstViewController</code>, and then asking <code>FirstViewController</code> to go to Login next.</p>

<p>If it is an unwind operation, the segue is performed right after <code>viewDidAppear</code>.</p>

<p>If it is not an unwind operation, other logics could be added in <code>viewDidAppear</code> eg. go to Login if no logged in user.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Guide to using PromiseKit]]></title>
    <link href="http://samwize.com/2015/04/17/guide-to-using-promisekit/"/>
    <updated>2015-04-17T08:49:54+08:00</updated>
    <id>http://samwize.com/2015/04/17/guide-to-using-promisekit</id>
    <content type="html"><![CDATA[<p><a href="http://promisekit.org">PromiseKit</a> is a wonderful library for dealing for async callbacks.</p>

<p>Other similar libraries are <a href="http://promisekit.org">Bolts Framework</a> and <a href="https://github.com/ReactiveCocoa/ReactiveCocoa">ReactiveCocoa</a>, but personally I felt the syntax and design of PromiseKit to be much better.</p>

<p>Initially, I wanted to write a guide on some common usage of PromiseKit, but I find that the <a href="http://philmitchell.github.io/PromiseKit/">common recipes by Phil Mitchell himself</a> is great.</p>

<p>Therefore, I will be emphasizing only on some usage/pitfall.</p>

<!-- more -->


<h2>Throwing NSError</h2>

<p>You can return <code>NSError</code> (or <code>@throw</code> a message), which will then have the nearest <code>catch</code> blcok to handle</p>

<h2>Return in a promise</h2>

<p>In a promise, you can return almost anything.</p>

<ul>
<li>If return a promise, next <code>then</code> will receive the <em>fulfilled value</em></li>
<li>If return <code>NSError</code>, next <code>catch</code> will handle it</li>
<li>If return any other type, next <code>then</code> will recive it</li>
<li>If return <code>nil</code>, that&rsquo;s fine too</li>
<li>If no return, next <code>then</code> will run immediately</li>
</ul>


<p>It&rsquo;s really flexible with promises.</p>

<p>Pitfall: If returning <strong>different types</strong>, the return type must be explicitly <code>(^id)</code> (any type), not just <code>(^)</code> (no type).</p>

<pre><code>myURLPromise.then(^id(NSURL *url){ 
     if (url) {
         return url; // Got my url, I'm done, return the value
     }
     else {
         return myOtherURLPromise; // Not done, do some more work
     }
}).then(^(NSURL *url){
    // Do something with URL ...
});
</code></pre>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[UIScrollView Content View Contraints]]></title>
    <link href="http://samwize.com/2015/04/13/uiscrollview-content-view-constraints-autolayout/"/>
    <updated>2015-04-13T21:15:35+08:00</updated>
    <id>http://samwize.com/2015/04/13/uiscrollview-content-view-constraints-autolayout</id>
    <content type="html"><![CDATA[<p>The <code>Content</code> view in a <code>UIScrollView</code> is very unique when it comes to configuring a scroll view with auto layout.</p>

<p>In short, the constraints needed:</p>

<ol>
<li><p>You must have only 1 child view in <code>UIScrollView</code>, which we refered to as <code>Content</code> view</p></li>
<li><p><code>Content</code> is usually pinned up, down, left and right to the <code>UIScrollView</code></p></li>
<li><p>In addition, <code>Content</code> must define it&rsquo;s width and height, which is the scrollable area. In most case you will have the width equal to the <code>UIScrollView</code>. The height is set to 888 in example below, but usually you will have the views within in to determine the overall height.</p></li>
</ol>


<p><img class="center" src="http://samwize.com/images/UIScrollView-content-setup.png"></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Always Needed Guide to Customize UINavigationBar]]></title>
    <link href="http://samwize.com/2015/04/11/always-needed-guide-to-customize-uinavigationbar/"/>
    <updated>2015-04-11T00:12:41+08:00</updated>
    <id>http://samwize.com/2015/04/11/always-needed-guide-to-customize-uinavigationbar</id>
    <content type="html"><![CDATA[<p>Customizing <code>UINavigationBar</code> remains a <em>not-easy-to-remember</em> topic.</p>

<p>When I need to change the looks of the nav bar &ndash; such as changing the Back arrow, or title, or making it transparent &ndash; I always need to google for the code.</p>

<!-- more -->


<h2>Understanding Navigation Bar</h2>

<p>The reason that nav bar is not easy, is because it is a pretty complicated component.</p>

<p>There are 3 classes associated with nav bar:</p>

<ol>
<li><a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationBar_Class/index.html">UINavigationBar</a></li>
<li><a href="https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UINavigationItem_Class/index.html">UINavigationItem</a></li>
<li><a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIBarButtonItem_Class/index.html">UIBarButtonItem</a></li>
</ol>


<p><code>UIBarButtonItem</code> are the buttons on the <code>UINavigationBar</code>.</p>

<p>But it is actually the <code>UINavigationItem</code> that you will use to manage the buttons and views.</p>

<p>Specifically, eg you are on a view controller, then <code>self.navigationItem</code> will provide you the left button, right button, title, etc.</p>

<p>One of the most confusing thing is between <code>self.navigationItem.backBarButtonItem</code> and <code>self.navigationItem.leftBarButtonItem</code>.</p>

<p>Assuming you are now on your view controller, then <code>leftBarButtonItem</code> refers to the button on the left of the nav bar. <code>backBarButtonItem</code> is NOT shown (yet).</p>

<p><code>backBarButtonItem</code> will be shown when you push another view controller (on top of it), as the left button of this topmost view controller (the &ldquo;Back&rdquo;).</p>

<h2>Change the Back Arrow Image</h2>

<p>You can change the default back arrow image by setting the appearance proxy.</p>

<pre><code>[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"back-button-image"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"back-button-image"]];
</code></pre>

<h2>Change the Back Title</h2>

<p>The default &ldquo;Back Title&rdquo; is the title of the view controller.</p>

<p>Sometimes, you want it to display literally &ldquo;Back&rdquo; (a shorter word if your title is too long), or you might want to not display a string at all (just the Back Arrow).</p>

<pre><code>self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
</code></pre>

<p>Remember: The above has to be set whereby <code>self</code> is the parent view controller which the button will go back to.</p>

<h2>Make the Background Transparent</h2>

<p>For this, I have created <a href="https://github.com/samwize/UINavigationBar-Addition">UINavigationBar+Addition</a>.</p>

<p>There are quite a number of things to set, some are not obvious.</p>

<pre><code>[navBar setTranslucent:YES];
[navBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
navBar.backgroundColor = [UIColor clearColor];
navBar.shadowImage = [UIImage new];
</code></pre>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Long Standing Bug With Using UIScreenEdgePanGestureRecognizer Created with Xcode Interface Builder]]></title>
    <link href="http://samwize.com/2015/04/08/long-standing-bug-with-using-uiscreenedgepangesturerecognizer-created-with-xcode-interface-builder/"/>
    <updated>2015-04-08T16:05:55+08:00</updated>
    <id>http://samwize.com/2015/04/08/long-standing-bug-with-using-uiscreenedgepangesturerecognizer-created-with-xcode-interface-builder</id>
    <content type="html"><![CDATA[<p>Encountered the <a href="http://stackoverflow.com/q/26256985/242682">same bug</a> when creating a <code>UIScreenEdgePanGestureRecognizer</code> via Interface Builder.</p>

<p>It just didn&rsquo;t work as expected: the selector is never called.</p>

<p>Other gesture recognizers work. Except panning from edge.</p>

<!-- more -->


<p>We can only fallback to manually creating it</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="nf">viewDidLoad</span> <span class="p">{</span>
</span><span class='line'>    <span class="p">[</span><span class="n">super</span> <span class="n">viewDidLoad</span><span class="p">];</span>
</span><span class='line'>    <span class="n">UIScreenEdgePanGestureRecognizer</span> <span class="o">*</span><span class="n">leftEdgeGesture</span> <span class="o">=</span> <span class="p">[[</span><span class="n">UIScreenEdgePanGestureRecognizer</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithTarget:</span><span class="n">self</span> <span class="nl">action:</span><span class="k">@selector</span><span class="p">(</span><span class="nl">handleLeftEdgeGesture:</span><span class="p">)];</span>
</span><span class='line'>    <span class="n">leftEdgeGesture</span><span class="p">.</span><span class="n">edges</span> <span class="o">=</span> <span class="n">UIRectEdgeLeft</span><span class="p">;</span>
</span><span class='line'>    <span class="n">leftEdgeGesture</span><span class="p">.</span><span class="n">delegate</span> <span class="o">=</span> <span class="n">self</span><span class="p">;</span>
</span><span class='line'>    <span class="p">[</span><span class="n">self</span><span class="p">.</span><span class="n">view</span> <span class="nl">addGestureRecognizer:</span><span class="n">leftEdgeGesture</span><span class="p">];</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">IBAction</span><span class="p">)</span><span class="nf">handleLeftEdgeGesture:</span><span class="p">(</span><span class="n">UIScreenEdgePanGestureRecognizer</span> <span class="o">*</span><span class="p">)</span><span class="nv">gesture</span> <span class="p">{</span>
</span><span class='line'>    <span class="n">NSLog</span><span class="p">(</span><span class="s">@&quot;panned&quot;</span><span class="p">);</span>
</span><span class='line'><span class="p">}</span>
</span><span class='line'>
</span><span class='line'><span class="k">-</span> <span class="p">(</span><span class="kt">BOOL</span><span class="p">)</span><span class="nf">gestureRecognizer:</span><span class="p">(</span><span class="n">UIGestureRecognizer</span> <span class="o">*</span><span class="p">)</span><span class="nv">gestureRecognizer</span> <span class="nf">shouldRecognizeSimultaneouslyWithGestureRecognizer:</span><span class="p">(</span><span class="n">UIGestureRecognizer</span> <span class="o">*</span><span class="p">)</span><span class="nv">otherGestureRecognizer</span> <span class="p">{</span>
</span><span class='line'>    <span class="k">return</span> <span class="n">YES</span><span class="p">;</span>
</span><span class='line'><span class="p">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Not impressed with a bug that is not fixed for over 6 months.</p>
]]></content>
  </entry>
  
</feed>
