<?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>LeeJon.es</title>
	<atom:link href="http://leejon.es/feed/" rel="self" type="application/rss+xml" />
	<link>http://leejon.es</link>
	<description>iCode</description>
	<lastBuildDate>Thu, 20 Oct 2011 19:53:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Notifying a ViewController with UIApplicationDidBecomeActiveNotification</title>
		<link>http://leejon.es/notifying-a-viewcontroller-with-uiapplicationdidbecomeactivenotification/</link>
		<comments>http://leejon.es/notifying-a-viewcontroller-with-uiapplicationdidbecomeactivenotification/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 19:49:24 +0000</pubDate>
		<dc:creator>lee</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Cocoa]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://leejon.es/?p=55</guid>
		<description><![CDATA[If a running iOS application is switched out for another, its application delegate is notified by calls to the applicationWillResignActive and applicationDidEnterBackground methods. When reactivated, the delegate receives calls to the applicationWillEnterForeground and applicationDidBecomeActive methods. All fairly self explanatory,  except &#8230; <a href="http://leejon.es/notifying-a-viewcontroller-with-uiapplicationdidbecomeactivenotification/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If a running iOS application is switched out for another, its application delegate is notified by calls to the <strong>applicationWillResignActive</strong> and <strong>applicationDidEnterBackground</strong> methods. When reactivated, the delegate receives calls to the <strong>applicationWillEnterForeground</strong> and <strong>applicationDidBecomeActive</strong> methods. All fairly self explanatory,  except when you need to refresh some data in a UIViewController. As it turns out, there is a fairly straight forward way to handle this using the very handy <strong>NSNotificationCenter</strong> and <strong>UIApplicationDidBecomeActiveNotification</strong></p>
<p>First, attach to the notification in the <strong>viewWillAppear</strong> method of the target view controller:</p>
<pre class="brush: objc; title: ; notranslate">

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

	[[NSNotificationCenter defaultCenter] addObserver: self
											 selector: @selector( appActivated: )
												 name: UIApplicationDidBecomeActiveNotification
											   object: nil];

}
</pre>
<p>Next, be a good citizen and remove the observer in the <strong>viewWillDisappear</strong> method:</p>
<pre class="brush: objc; title: ; notranslate">
- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	[[NSNotificationCenter defaultCenter] removeObserver:self ];
}
</pre>
<p>Finally, implement the appActivated observer method:</p>
<pre class="brush: objc; title: ; notranslate">
- (void)appActivated:(NSNotification *)note
{
	[self update];
}
</pre>
<p>Set a breakpoint on the appActivated method then fire up the app in the debugger. Double click the home button and switch to another app then double click and switch back to yours. At that point, if everything is configured correctly, you should see the debugger trap on your breakpoint.</p>
<p>Our mobile applications typically do a lot of communicating with webservices. One useful application of this technique we use is to check for network connectivity and make sure that the most current data is currently reflected in the view.</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Notifying+a+ViewController+with+UIApplicationDidBecomeActiveNotification+http%3A%2F%2Fis.gd%2FNysk6k" title="Post to Twitter"><img class="nothumb" src="http://leejon.es/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter.png" alt="Post to Twitter" /></a> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Notifying+a+ViewController+with+UIApplicationDidBecomeActiveNotification+http%3A%2F%2Fis.gd%2FNysk6k" title="Post to Twitter">Tweet This Post</a></p></div>]]></content:encoded>
			<wfw:commentRss>http://leejon.es/notifying-a-viewcontroller-with-uiapplicationdidbecomeactivenotification/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

