<?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>iOS Developer Tips Blog &#187; Core Services</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/core-services/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:40:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Remind Users to Restart Your App</title>
		<link>http://iPhoneDeveloperTips.com/core-services/encourage-users-to-restart-your-app.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/encourage-users-to-restart-your-app.html#comments</comments>
		<pubDate>Wed, 08 Feb 2012 08:03:02 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=11021</guid>
		<description><![CDATA[This tip shows you how to create a local notification that will be trigged when a user exits your application. I would recommend you use this sparingly, for example, if the user was in the middle of setting up configuration information needed by an application. Let&#8217;s start with a quick look at how this appears [...]]]></description>
			<content:encoded><![CDATA[<p>This tip shows you how to create a local notification that will be trigged when a user exits your application. I would recommend you use this sparingly, for example, if the user was in the middle of setting up configuration information needed by an application.</p>
<p>Let&#8217;s start with a quick look at how this appears to the user. Assume the application TestApp is running in the foreground. When the application is put into the background by pressing the home button, within 1 second (see the code below), the following local notifications will appear (iOS 5 and iOS 4):<br />
<span id="more-11021"></span></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/02/localNotif2.gif" alt="" title="localNotif2" width="414" height="50" class="alignnone size-full wp-image-11023" /></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2012/02/localNotif1.gif" alt="" title="localNotif1" width="297" height="146" class="alignnone size-full wp-image-11022" /></p>
<p>If the user taps on the message, the application return to the foreground.</p>
<p>The code is quite simple, it begins with creating an NSDate object with the current date/time, adding one second to that time, and setting the fire date for a local notification to the configured date. From there, setup the local notification messaging and wrap up by scheduling the notification with the OS.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>applicationDidEnterBackground<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application
<span style="color: #002200;">&#123;</span>
  UILocalNotification <span style="color: #002200;">*</span>localNotification <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILocalNotification alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Current date</span>
  <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>; 
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Add one second to the current time</span>
  <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>dateToFire <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>date dateByAddingTimeInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set the fire date/time</span>
  <span style="color: #002200;">&#91;</span>localNotification setFireDate<span style="color: #002200;">:</span>dateToFire<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>localNotification setTimeZone<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> defaultTimeZone<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;    
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Setup alert notification</span>
  <span style="color: #002200;">&#91;</span>localNotification setAlertBody<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tap to return to TestApp&quot;</span> <span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>localNotification setAlertAction<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Open TestApp&quot;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>localNotification setHasAction<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> scheduleLocalNotification<span style="color: #002200;">:</span>localNotification<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Once the notification has been presented to the user, and the user acknowledges, the method below in your application will be called once the app comes to the foreground:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didReceiveLocalNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UILocalNotification <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Your code here...</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Again, consider this option as a reminder to the user only in cases where an important process was underway, not as the default operation when the app goes into the background.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/encourage-users-to-restart-your-app.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get List of Installed International Keyboards</title>
		<link>http://iPhoneDeveloperTips.com/core-services/get-list-of-installed-international-keyboards.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/get-list-of-installed-international-keyboards.html#comments</comments>
		<pubDate>Tue, 27 Dec 2011 07:22:24 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10800</guid>
		<description><![CDATA[The NSUserDefaults object is typically used to save/restore your application related preferences, configuration data, etc &#8211; see Write and Read User Preferences for more information. In addition to application specifics, there is a system wide default list that is available to all applications, accessible using the class method standardUserDefaults in the NSUserDefaults object. To get [...]]]></description>
			<content:encoded><![CDATA[<p>The <strong>NSUserDefaults</strong> object is typically used to save/restore your application related preferences, configuration data, etc &#8211; see <a href="http://iphonedevelopertips.com/cocoa/read-and-write-user-preferences.html" target="_blank">Write and Read User Preferences</a> for more information. In addition to application specifics, there is a system wide default list that is available to all applications, accessible using the class method <strong>standardUserDefaults</strong> in the <strong>NSUserDefaults</strong> object.</p>
<p>To get a list of the system wide settings:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSDictionary</span><span style="color: #002200;">*</span> defaults <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span> dictionaryRepresentation<span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Defaults: %@&quot;</span>, defaults<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p><span id="more-10800"></span><br />
A partial list of the output on my device follows:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Defaults: {
    AppleICUForce24HourTime = 0;
    AppleITunesStoreItemKinds =     (
        wemix,
        podcast,
        ...
    );
    AppleKeyboards =     (
        &quot;en_US@hw=US;sw=QWERTY&quot;,
        &quot;zh_Hant-HWR@sw=HWR&quot;,
        &quot;emoji@sw=Emoji&quot;
    );
    AppleKeyboardsExpanded = 1;
    AppleLanguages =     (
        en,
        &quot;zh-Hant&quot;,
        fr,
        de,
  ...
}</pre></div></div>

<p>As you can tell, the defaults returned above are contained within a dictionary object. The list of keyboards in the dictionary is stored as an <strong>NSArray</strong> object. To retrieve the array, simply request the object for the key &#8220;<strong>AppleKeyboards</strong>&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSUserDefaults</span> standardUserDefaults<span style="color: #002200;">&#93;</span> objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;AppleKeyboards&quot;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Keyboards: %@&quot;</span>, array<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output of all the installed international keyboards looks as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Keyboards: (
    &quot;en_US@hw=US;sw=QWERTY&quot;,
    &quot;zh_Hant-HWR@sw=HWR&quot;,
    &quot;emoji@sw=Emoji&quot;
)</pre></div></div>

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/get-list-of-installed-international-keyboards.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>iOS 5 : Look Up Definitions Using Dictionary Service</title>
		<link>http://iPhoneDeveloperTips.com/core-services/ios-5-look-up-definitions-using-dictionary-service.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/ios-5-look-up-definitions-using-dictionary-service.html#comments</comments>
		<pubDate>Mon, 12 Dec 2011 08:09:22 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>
		<category><![CDATA[dictionary]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10703</guid>
		<description><![CDATA[With iOS 5, there is now a dictionary service that you can access to look up definitions of words. UIReferenceLibraryViewController makes it as easy as creating an instance of the class and providing a term/word to lookup. The view controller will handle the display of the definition and managing the UI (scrolling, etc). // Create [...]]]></description>
			<content:encoded><![CDATA[<p>With iOS 5, there is now a dictionary service that you can access to look up definitions of words. <strong>UIReferenceLibraryViewController</strong> makes it as easy as creating an instance of the class and providing a term/word to lookup. The view controller will handle the display of the definition and managing the UI (scrolling, etc).</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Create the view controller</span>
UIReferenceLibraryViewController <span style="color: #002200;">*</span>reference <span style="color: #002200;">=</span> 
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIReferenceLibraryViewController alloc<span style="color: #002200;">&#93;</span> initWithTerm<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;homebrew&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>reference animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/reference1.gif" alt="" title="reference1" width="427" height="308" class="alignnone" /><br />
<span id="more-10703"></span></p>
<p>If you provide a word that doesn&#8217;t exist in the reference, the view controller will look as shown in the screenshot on the right.</p>
<p>Rather than show a view controller with no definition, you can make a call to the class method <strong>dictionaryHasDefinitionForTerm</strong> prior to creating an object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Check if the term exists...</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;sabrage&quot;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>UIReferenceLibraryViewController dictionaryHasDefinitionForTerm<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Create the view controller</span>
  UIReferenceLibraryViewController <span style="color: #002200;">*</span>reference <span style="color: #002200;">=</span> 
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIReferenceLibraryViewController alloc<span style="color: #002200;">&#93;</span> initWithTerm<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>reference animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The default animation is to slide the view controller up from the bottom. To change the animation, set <strong>setModalTransitionStyle</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>str <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;homebrew&quot;</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>UIReferenceLibraryViewController dictionaryHasDefinitionForTerm<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Create the view controller</span>
  UIReferenceLibraryViewController <span style="color: #002200;">*</span>reference <span style="color: #002200;">=</span> 
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIReferenceLibraryViewController alloc<span style="color: #002200;">&#93;</span> initWithTerm<span style="color: #002200;">:</span>str<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Change the transition style</span>
  <span style="color: #002200;">&#91;</span>reference setModalTransitionStyle<span style="color: #002200;">:</span>UIModalTransitionStyleFlipHorizontal<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>reference animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Out of curiousity, I looked up a ridiculously long word, one that I would consider rather obscure: pneumonoultramicroscopicsilicovolcanoconiosis. Much to my surprise:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/12/reference2.gif" alt="" title="reference2" width="360" height="225" class="alignnone size-full wp-image-10708" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/ios-5-look-up-definitions-using-dictionary-service.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>iOS 5 : Twitter Framework – Part 3 &#8211; Display List of Twitter Accounts</title>
		<link>http://iPhoneDeveloperTips.com/core-services/ios-5-twitter-framework-%e2%80%93-part-3.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/ios-5-twitter-framework-%e2%80%93-part-3.html#comments</comments>
		<pubDate>Mon, 14 Nov 2011 08:43:48 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>
		<category><![CDATA[iOS 5]]></category>
		<category><![CDATA[twitter framework]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10590</guid>
		<description><![CDATA[In the previous two iOS 5 Twitter Framework posts, I covered using TWTweetComposeViewController to display a pre-built controller for posting to Twitter and also wrote an example on how to use the TWRequest object to create an HTTP request to access the Twitter API. Over the past few weeks I&#8217;ve had a number of requests [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous two iOS 5 Twitter Framework posts, I covered using <a target="_blank" href="http://iphonedevelopertips.com/core-services/ios-5-twitter-framework-part-1.html">TWTweetComposeViewController to display a pre-built controller</a> for posting to Twitter and also wrote an example on <a target="_blank" href="http://iPhoneDeveloperTips.com/core-services/ios-5-twitter-framework-part-2.html">how to use the TWRequest object</a> to create an HTTP request to access the Twitter API. </p>
<p>Over the past few weeks I&#8217;ve had a number of requests to take this series one step further and show how to access the Twitter account data and display the information in a tableview. One of the common threads in the requests was how to do user interface updates as part of the completion handler code.</p>
<p>In this post I will create a short example to create and populate a tableview with the active Twitter account(s), calling code to update the UI from within the completion handler.<br />
<span id="more-10590"></span></p>
<h5>Setup the TableView</h5>
<p>The interface for the primary view controller looks as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> SandboxViewController <span style="color: #002200;">:</span> UIViewController &lt;UITableViewDelegate, UITableViewDataSource&gt;
<span style="color: #002200;">&#123;</span>
  UIButton        <span style="color: #002200;">*</span>testButton;
  UITableView     <span style="color: #002200;">*</span>mainTableView;
  NSInteger       numberOfTwitterAccounts;
  ACAccountStore  <span style="color: #002200;">*</span>account;
  <span style="color: #400080;">NSArray</span>         <span style="color: #002200;">*</span>arrayOfAccounts;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The <strong>testButton</strong> will be used to initiate a request to get the Twitter account names,  <strong>mainTableView</strong> will display those same names. <strong>numberOfTwitterAccounts</strong> will hold the count of valid Twitter accounts. <strong>account</strong> will contain Twitter account information and <strong>arrayOfAccounts</strong> stores an array of <strong>ACAccountStore</strong> objects.</p>
<p>The loadView method builds the basic UI for this example:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>loadView 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>self setView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor blackColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create button  </span>
  testButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeRoundedRect<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>testButton setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">20</span>, <span style="color: #2400d9;">30</span>, <span style="color: #2400d9;">280</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>testButton setTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Get Twitter Accounts&quot;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>testButton addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span> UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;        
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>testButton<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create table view</span>
  mainTableView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">80</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">360</span><span style="color: #002200;">&#41;</span> style<span style="color: #002200;">:</span> UITableViewStylePlain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>mainTableView setDelegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>mainTableView setDataSource<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>mainTableView setSeparatorStyle<span style="color: #002200;">:</span>UITableViewCellSeparatorStyleNone<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Start with an empty table</span>
  numberOfTwitterAccounts <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>mainTableView<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Notice I set <strong>numberOfTwitterAccounts</strong> to 0, so the table will be empty when the view controller is displayed &#8211; the code below shows how I use the <strong>numberOfTwitterAccounts</strong> variable when creating the tableview:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView numberOfRowsInSection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>NSInteger<span style="color: #002200;">&#41;</span>section
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Set to number of twitter accounts</span>
  <span style="color: #a61390;">return</span> numberOfTwitterAccounts;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>At this point, the user interface will look like this:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/11/twitterPart3a.gif" alt="" title="Twitter Framework iOS 5" width="256" height="350" class="alignnone size-full wp-image-10595" /></p>
<h5>Get Twitter Account Data</h5>
<p>When the button is pressed the following code will be called. The first step is to verify a Twitter account is available. From there, create an <strong>ACAccountStore</strong> object and an <strong>ACAccountType</strong> to get access to Twitter account data.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Is Twitter is accessible is there at least one account</span>
  <span style="color: #11740a; font-style: italic;">// setup on the device</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>TWTweetComposeViewController canSendTweet<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Create account store, followed by a twitter account identifer</span>
    account <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ACAccountStore alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
    ACAccountType <span style="color: #002200;">*</span>accountType <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>account accountTypeWithAccountTypeIdentifier<span style="color: #002200;">:</span>ACAccountTypeIdentifierTwitter<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Request access from the user to use their Twitter accounts.</span>
    <span style="color: #002200;">&#91;</span>account requestAccessToAccountsWithType<span style="color: #002200;">:</span>accountType withCompletionHandler<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span> granted, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span> 
    <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Did user allow us access?</span>
      <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>granted <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
      <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Populate array with all available Twitter accounts</span>
        arrayOfAccounts <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>account accountsWithAccountType<span style="color: #002200;">:</span>accountType<span style="color: #002200;">&#93;</span>;
        <span style="color: #002200;">&#91;</span>arrayOfAccounts retain<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// Populate the tableview</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>arrayOfAccounts count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> 
          <span style="color: #002200;">&#91;</span>self performSelectorOnMainThread<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>updateTableview<span style="color: #002200;">&#41;</span> withObject<span style="color: #002200;">:</span><span style="color: #a61390;">NULL</span> waitUntilDone<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Populate Tableview with Twitter Account Data</h5>
<p>The last step is to add account names to the tableview. Notice in the code above, near the bottom inside the completion handler, the reference to <strong>performSelectorOnMainThread</strong>, this will invoke the method <strong>updateTableview</strong> on the main thread, which will allow the UI to be updated.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>updateTableview
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Update the row count used by tableview</span>
  numberOfTwitterAccounts <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>arrayOfAccounts count<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Reload the table with twitter account data</span>
  <span style="color: #002200;">&#91;</span>mainTableView reloadData<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>With this code in place, once the table is reloaded, the method <strong>numberOfRowsInSection</strong> (shown above) will reflect the current number of rows for the table based on the number of active twitter accounts.</p>
<p>Here is the how each cell in the tableview is created:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span>UITableViewCell <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UITableView <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>tableView cellForRowAtIndexPath<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSIndexPath</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>indexPath 
<span style="color: #002200;">&#123;</span>
  UITableViewCellStyle style <span style="color: #002200;">=</span>  UITableViewCellStyleDefault;
  UITableViewCell <span style="color: #002200;">*</span>cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>tableView dequeueReusableCellWithIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tableCell&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>cell<span style="color: #002200;">&#41;</span> 
    cell <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITableViewCell alloc<span style="color: #002200;">&#93;</span> initWithStyle<span style="color: #002200;">:</span>style reuseIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;tableCell&quot;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// View the array data </span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Debug: %@&quot;</span>, arrayOfAccounts<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Access the account data for each entry</span>
  ACAccount <span style="color: #002200;">*</span>acct <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>arrayOfAccounts objectAtIndex<span style="color: #002200;">:</span>indexPath.row<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set the cell text to the username of the twitter account</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>cell textLabel<span style="color: #002200;">&#93;</span> setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>acct username<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">return</span> cell;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Based on the indexpath (which row is being created), create an <strong>ACAccount</strong> object from the array of Twitter accounts and query the username variable to get the cell text.</p>
<p>With the two accounts active on my device, here is the output with the table populated:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/11/twitterPart3b.gif" alt="" title="Twitter Framework iOS 5" width="258" height="351" class="alignnone size-full wp-image-10599" /></p>
<h5>Download the Twitter Framework Example</h5>
<p>You can download the iOS 5 Twitter Framework Xcode project <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/11/iOSDevTips-TwitterPart3.zip">here</a>.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/ios-5-twitter-framework-%e2%80%93-part-3.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>iOS 5 : Twitter Framework – Part 2 &#8211; Call Twitter API with TWRequest</title>
		<link>http://iPhoneDeveloperTips.com/core-services/ios-5-twitter-framework-part-2.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/ios-5-twitter-framework-part-2.html#comments</comments>
		<pubDate>Mon, 24 Oct 2011 08:23:16 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10471</guid>
		<description><![CDATA[In the previous post on iOS 5 and the new Twitter framework, I walked through how to use TWTweetComposeViewController to display a pre-built controller for easily integrating and posting to Twitter. In this post I will show you how to use the TWRequest object to create an HTTP request, and in turn, sending and processing [...]]]></description>
			<content:encoded><![CDATA[<p>In the <a href="http://iphonedevelopertips.com/core-services/ios-5-twitter-framework-part-1.html" target="_blank">previous post on iOS 5 and the new Twitter framework</a>, I walked through how to use <strong>TWTweetComposeViewController</strong> to display a pre-built controller for easily integrating and posting to Twitter.</p>
<p>In this post I will show you how to use the <strong>TWRequest</strong> object to create an HTTP request, and in turn, sending and processing the results of the request. There are three primary components of a Twitter request: the URL of the desired Twitter service you are after, the type of HTTP request (GET, POST or DELETE) and any query parameters (required or optional) of the service requested. It&#8217;s also worth noting, using when using <strong>TWRequest</strong> user authentication is handled for you.<br />
<span id="more-10471"></span></p>
<h5>Twitter Search</h5>
<p>By looking at the <a href="https://dev.twitter.com/docs/api" target="_blank">Twitter API</a> I found how to properly setup a <a href="https://dev.twitter.com/docs/api/1/get/search" target="_blank">search request</a>. Below I do a search for the string &#8220;iOS 5&#8243;, which looks like this when url-encoded: &#8220;q=iOS%205&#8243;. Check out the API for more information about the additional parameters.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Twitter/Twitter.h&gt;</span>
&nbsp;
...
&nbsp;
<span style="color: #11740a; font-style: italic;">// Do a simple search, using the Twitter API</span>
TWRequest <span style="color: #002200;">*</span>request <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TWRequest alloc<span style="color: #002200;">&#93;</span> initWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span>
   <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://search.twitter.com/search.json?q=iOS%205&amp;rpp=5&amp;with_twitter_user_id=true&amp;result_type=recent&quot;</span><span style="color: #002200;">&#93;</span> 
   parameters<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span> requestMethod<span style="color: #002200;">:</span>TWRequestMethodGET<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Notice this is a block, it is the handler to process the response</span>
<span style="color: #002200;">&#91;</span>request performRequestWithHandler<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>responseData, <span style="color: #400080;">NSHTTPURLResponse</span> <span style="color: #002200;">*</span>urlResponse, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span>
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>urlResponse statusCode<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">200</span><span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// The response from Twitter is in JSON format</span>
    <span style="color: #11740a; font-style: italic;">// Move the response into a dictionary and print</span>
    <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error;        
    <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>dict <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>NSJSONSerialization JSONObjectWithData<span style="color: #002200;">:</span>responseData options<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span> error<span style="color: #002200;">:&amp;</span>error<span style="color: #002200;">&#93;</span>;
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Twitter response: %@&quot;</span>, dict<span style="color: #002200;">&#41;</span>;                           
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">else</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Twitter error, HTTP response: %i&quot;</span>, <span style="color: #002200;">&#91;</span>urlResponse statusCode<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>And here is what a partial response, in JSON, looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Twitter response<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;completed_in&quot;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;0.179&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #3366CC;">&quot;max_id&quot;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">128283809723592705</span><span style="color: #339933;">;</span>
    <span style="color: #3366CC;">&quot;max_id_str&quot;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">128283809723592705</span><span style="color: #339933;">;</span>
    <span style="color: #3366CC;">&quot;next_page&quot;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;?page=2&amp;max_id=128283809723592705&amp;q=ios%205&amp;rpp=5&amp;with_twitter_user_id=1&quot;</span><span style="color: #339933;">;</span>
    page <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
    query <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ios+5&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #3366CC;">&quot;refresh_url&quot;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;?since_id=128283809723592705&amp;q=ios%205&amp;with_twitter_user_id=1&quot;</span><span style="color: #339933;">;</span>
    results <span style="color: #339933;">=</span>     <span style="color: #009900;">&#40;</span>
                <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;created_at&quot;</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Mon, 24 Oct 2011 01:36:58 +0000&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #3366CC;">&quot;from_user&quot;</span> <span style="color: #339933;">=</span> XXXXXXXXXXX<span style="color: #339933;">;</span>
            <span style="color: #3366CC;">&quot;from_user_id&quot;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">14379847</span><span style="color: #339933;">;</span>
            <span style="color: #3366CC;">&quot;from_user_id_str&quot;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">14379847</span><span style="color: #339933;">;</span>
            geo <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&lt;null&gt;&quot;</span><span style="color: #339933;">;</span>
            id <span style="color: #339933;">=</span> <span style="color: #CC0000;">128283809723592705</span><span style="color: #339933;">;</span>
            <span style="color: #3366CC;">&quot;id_str&quot;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">128283809723592705</span><span style="color: #339933;">;</span>
            <span style="color: #3366CC;">&quot;iso_language_code&quot;</span> <span style="color: #339933;">=</span> en<span style="color: #339933;">;</span>
            metadata <span style="color: #339933;">=</span>             <span style="color: #009900;">&#123;</span>
                <span style="color: #3366CC;">&quot;result_type&quot;</span> <span style="color: #339933;">=</span> recent<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
  ...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can use the above query even if there is no Twitter account setup on the device &#8211; for example, copy/paste the URL <em><strong>http://search.twitter.com/search.json?q=iOS%205&#038;rpp=5&#038;with_twitter_user_id=true&#038;result_type=recent</strong></em> into a browser to view the search request.</p>
<h5>Twitter Post</h5>
<p>Let&#8217;s look at how to create a custom tweet, that is, post to Twitter without using <strong>TWTweetComposeViewController</strong>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Accounts/Accounts.h&gt;</span>
<span style="color: #6e371a;">#import &lt;Twitter/Twitter.h&gt;</span>
&nbsp;
...
&nbsp;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>TWTweetComposeViewController canSendTweet<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Create account store, followed by a twitter account identifier</span>
  <span style="color: #11740a; font-style: italic;">// At this point, twitter is the only account type available</span>
  ACAccountStore <span style="color: #002200;">*</span>account <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>ACAccountStore alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
  ACAccountType <span style="color: #002200;">*</span>accountType <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>account accountTypeWithAccountTypeIdentifier<span style="color: #002200;">:</span>ACAccountTypeIdentifierTwitter<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Request access from the user to access their Twitter account</span>
  <span style="color: #002200;">&#91;</span>account requestAccessToAccountsWithType<span style="color: #002200;">:</span>accountType withCompletionHandler<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span> granted, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Did user allow us access?</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>granted <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Populate array with all available Twitter accounts</span>
      <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>arrayOfAccounts <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>account accountsWithAccountType<span style="color: #002200;">:</span>accountType<span style="color: #002200;">&#93;</span>;
&nbsp;
      <span style="color: #11740a; font-style: italic;">// Sanity check</span>
      <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>arrayOfAccounts count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> 
      <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Keep it simple, use the first account available</span>
        ACAccount <span style="color: #002200;">*</span>acct <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>arrayOfAccounts objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// Build a twitter request</span>
        TWRequest <span style="color: #002200;">*</span>postRequest <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TWRequest alloc<span style="color: #002200;">&#93;</span> initWithURL<span style="color: #002200;">:</span>
                                  <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://api.twitter.com/1/statuses/update.json&quot;</span><span style="color: #002200;">&#93;</span> 
                                  parameters<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObject<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Custom tweet from iOS 5 Twitter framework. Visit iOSDeveloperTips.com for more information.&quot;</span> 
                                  forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;status&quot;</span><span style="color: #002200;">&#93;</span> requestMethod<span style="color: #002200;">:</span>TWRequestMethodPOST<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// Post the request</span>
        <span style="color: #002200;">&#91;</span>postRequest setAccount<span style="color: #002200;">:</span>acct<span style="color: #002200;">&#93;</span>;
&nbsp;
        <span style="color: #11740a; font-style: italic;">// Block handler to manage the response</span>
        <span style="color: #002200;">&#91;</span>postRequest performRequestWithHandler<span style="color: #002200;">:^</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>responseData, <span style="color: #400080;">NSHTTPURLResponse</span> <span style="color: #002200;">*</span>urlResponse, <span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span>error<span style="color: #002200;">&#41;</span> 
        <span style="color: #002200;">&#123;</span>
          NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Twitter response, HTTP response: %i&quot;</span>, <span style="color: #002200;">&#91;</span>urlResponse statusCode<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
        <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
      <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>On line 14, the request for access will show a dialog as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/10/twitterPart2.gif" alt="" title="twitterPart2" width="240" height="153" class="alignnone size-full wp-image-10501" /></p>
<p>If all is well, we create an array of the available accounts, and using the first entry, create a <strong>TWRequest</strong> to post a tweet. Check out the <a href="https://dev.twitter.com/docs/api" target="_blank">Twitter API</a> for specifics on the parameters.</p>
<p>Once the tweet has been posted, there is block handler to print the response to the console. A successful tweet should print: <em><strong>Twitter response, HTTP response: 200</strong></em>.</p>
<p>The successful Twitter post looks will look like this:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/10/twitterPart2a.gif" alt="" title="twitterPart2a" width="545" height="99" class="alignnone size-full wp-image-10513" /></p>
<p>One last note, don&#8217;t forget to add the Twitter and Accounts frameworks to your project before compiling.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/ios-5-twitter-framework-part-2.html/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>iOS 5 : Twitter Framework &#8211; Part 1 &#8211; Compose a Tweet with TWTweetComposeViewController</title>
		<link>http://iPhoneDeveloperTips.com/core-services/ios-5-twitter-framework-part-1.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/ios-5-twitter-framework-part-1.html#comments</comments>
		<pubDate>Mon, 17 Oct 2011 08:21:04 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10387</guid>
		<description><![CDATA[This is the first post of two on how to work with the new Twitter framework in an iOS 5 app. This post will cover the basics to create and display a dialog for composing a tweet. Part 2 in this series can be found here. The primary controller for working with Twitter in iOS [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first post of two on how to work with the new Twitter framework in an iOS 5 app. This post will cover the basics to create and display a dialog for composing a tweet. <a href="http://iphonedevelopertips.com/core-services/ios-5-twitter-framework-part-2.html" target="_blank">Part 2 in this series can be found here</a>.</p>
<p>The primary controller for working with Twitter in iOS 5 is <strong>TWTweetComposeViewController</strong>. This object handles all the UI related work to display and manage input &#8211; you can pre-populate the initial text, add an image as well as a URL. The code is straight forward to get things rolling:<br />
<span id="more-10387"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;Twitter/Twitter.h&gt;</span>
&nbsp;
...
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create the view controller</span>
TWTweetComposeViewController <span style="color: #002200;">*</span>twitter <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TWTweetComposeViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Optional: set an image, url and initial text</span>
<span style="color: #002200;">&#91;</span>twitter addImage<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIImage imageNamed<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;iOSDevTips.png&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>twitter addURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://iOSDeveloperTips.com/&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>twitter setInitialText<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tweet from iOS 5 app using the Twitter framework.&quot;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Show the controller</span>
<span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>twitter animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Called when the tweet dialog has been closed</span>
twitter.completionHandler <span style="color: #002200;">=</span> <span style="color: #002200;">^</span><span style="color: #002200;">&#40;</span>TWTweetComposeViewControllerResult result<span style="color: #002200;">&#41;</span> 
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>title <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tweet Status&quot;</span>;
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>msg; 
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>result <span style="color: #002200;">==</span> TWTweetComposeViewControllerResultCancelled<span style="color: #002200;">&#41;</span>
    msg <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tweet compostion was canceled.&quot;</span>;
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>result <span style="color: #002200;">==</span> TWTweetComposeViewControllerResultDone<span style="color: #002200;">&#41;</span>
    msg <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Tweet composition completed.&quot;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Show alert to see how things went...</span>
  UIAlertView<span style="color: #002200;">*</span> alertView <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span>title message<span style="color: #002200;">:</span>msg delegate<span style="color: #002200;">:</span>self cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Okay&quot;</span> otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>alertView show<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Dismiss the controller</span>
  <span style="color: #002200;">&#91;</span>self dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>;</pre></div></div>

<p>Notice the Objective-C block (<a href="http://iphonedevelopertips.com/objective-c/introduction-to-blocks-in-objective-c-part-1.html" target="_blank">more on blocks here</a>) that is defined for the completion handler, this is called once the user has finished composing the tweet.</p>
<p>Before running the code you&#8217;ll need to configure a Twitter account. If you skip this step you&#8217;ll see the dialog on the left below. The image on the right is the settings screen to configure an account. Also, you will need to add the Twitter framework to your Xcode project before compiling.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/10/twit1.gif" alt="" title="twit1" width="480" height="360" class="alignnone size-full wp-image-10390" /></p>
<p>The screenshot below shows the Twitter view controller poplated with the content shown above.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/10/twit2.gif" alt="" title="twit2" width="258" height="374" class="alignnone size-full wp-image-10395" /></p>
<p>Below is a screenshot from the twitter page showing how the tweet above looks at twitter.com/iOSDevTips.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/10/twit3.gif" alt="" title="twit3" width="537" height="91"/></p>
<p>That&#8217;s all you need to get started for basic twitter integration. In the next post I&#8217;ll go into more detail including a look at the <strong>ACAccountStore</strong> for managing accounts and <strong>TWRequest</strong> for sending HTTP requests to Twitter.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/ios-5-twitter-framework-part-1.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How to Keep the Screen from Locking</title>
		<link>http://iPhoneDeveloperTips.com/core-services/how-to-keep-the-screen-from-locking.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/how-to-keep-the-screen-from-locking.html#comments</comments>
		<pubDate>Mon, 25 Jul 2011 08:31:01 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=9134</guid>
		<description><![CDATA[By default, iOS will lock the screen and disable the touch sensor if there are no touch events for a specified period of time. Depending on your application, there may be times that you need to keep the screen from locking. For example, if your application is primarily accelerometer driven (such as a game), there [...]]]></description>
			<content:encoded><![CDATA[<p>By default, iOS will lock the screen and disable the touch sensor if there are no touch events for a specified period of time. Depending on your application, there may be times that you need to keep the screen from locking. For example, if your application is primarily accelerometer driven (such as a game), there may be minimal touch events to keep the screen from reseting its internal timer.</p>
<p>You can disable the idle timer through the <strong>idleTimerDisabled</strong> property of the shared <strong>UIApplication</strong> object:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Disable the idle timer</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> setIdleTimerDisabled<span style="color: #002200;">:</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Or for those who prefer dot syntax:</span>
<span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span>.idleTimerDisabled <span style="color: #002200;">=</span> <span style="color: #a61390;">YES</span>;</pre></div></div>

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/how-to-keep-the-screen-from-locking.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using Keychain to Store Username and Password</title>
		<link>http://iPhoneDeveloperTips.com/core-services/using-keychain-to-store-username-and-password.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/using-keychain-to-store-username-and-password.html#comments</comments>
		<pubDate>Mon, 11 Jul 2011 08:09:52 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=8930</guid>
		<description><![CDATA[The keychain services on iOS provide a means to securely store content such as passwords, keys, certificates, etc. Each iOS application has a separate set of keychain items. Beginning with iOS 3.0, it is possible to share keychain items across applications. In this tip, I will demonstrate a starting point for working with the keychain [...]]]></description>
			<content:encoded><![CDATA[<p>The keychain services on iOS provide a means to securely store content such as passwords, keys, certificates, etc. Each iOS application has a separate set of keychain items. Beginning with iOS 3.0, it is possible to share keychain items across applications.</p>
<p>In this tip, I will demonstrate a starting point for working with the keychain by storing and retrieving a username and password.<br />
<span id="more-8930"></span></p>
<h5>Keychain Demo Application</h5>
<p>Before going any further, let me show you the sample application I wrote to test keychain access. The screenshot on the left shows two fields, one each for username and passord, along with a button to initiate the process for writing to the keychain. The figure on the right shows the fields populated upon application startup by retrieving the username and password from the keychain.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/07/keychain.jpg" alt="" width="504" height="169" /></p>
<p>Below are the definitions for the textfields and the button:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">UITextField <span style="color: #002200;">*</span>username <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITextField alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">40</span>, <span style="color: #2400d9;">30</span>, <span style="color: #2400d9;">240</span>, <span style="color: #2400d9;">30</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>username setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleRoundedRect<span style="color: #002200;">&#93;</span>;
...
&nbsp;
UITextField <span style="color: #002200;">*</span>password <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITextField alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">40</span>, <span style="color: #2400d9;">75</span>, <span style="color: #2400d9;">240</span>, <span style="color: #2400d9;">30</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>password setBorderStyle<span style="color: #002200;">:</span>UITextBorderStyleRoundedRect<span style="color: #002200;">&#93;</span>;
...
&nbsp;
UIButton <span style="color: #002200;">*</span>testButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeRoundedRect<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>testButton setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">80</span>, <span style="color: #2400d9;">130</span>, <span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span>testButton addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span> UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;        
...</pre></div></div>

<h5>Keychain Wrapper</h5>
<p>Apple has written an Objective-C wrapper that you can use to simplify working with the keychain, the files in the wrapper are <strong>KeychainItemWrapper.h</strong> and <strong>KeychainItemWrapper.m</strong>, both are included in the attached Xcode project.</p>
<p>To use the wrapper, allocate a new <strong>KeychainItemWrapper</strong> object as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">KeychainItemWrapper <span style="color: #002200;">*</span>keychain <span style="color: #002200;">=</span> 
 <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>KeychainItemWrapper alloc<span style="color: #002200;">&#93;</span> initWithIdentifier<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;TestAppLoginData&quot;</span> accessGroup<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>I&#8217;ve specified a unique identifier (&#8220;TestAppLoginData&#8221;) for the username and password pair that I want to store in the keychain. The <strong>accessGroup</strong> is set to nil as only one application will access the keychain items in this application.</p>
<h5>Writing to the Keychain</h5>
<p>Once the button is pressed to save the username and password, writing to the keychain is quite straight-forward. For the username, I set an object (the username text) with the key <strong>kSecAttrAccount</strong>. I follow this by setting another object, this time the password text, with the key <strong>kSecValueData</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>button <span style="color: #002200;">==</span> testButton<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>	
    <span style="color: #11740a; font-style: italic;">// Store username to keychain 	</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>username text<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
      <span style="color: #002200;">&#91;</span>keychain setObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>username text<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecAttrAccount<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Store password to keychain</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>password text<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
      <span style="color: #002200;">&#91;</span>keychain setObject<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>password text<span style="color: #002200;">&#93;</span> forKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecValueData<span style="color: #002200;">&#93;</span>;    	    
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Reading from the Keychain</h5>
<p>To populate the textfields with stored keychain items at application startup, once the textfields are defined, I read the two key-value pairs:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Get username from keychain (if it exists)</span>
<span style="color: #002200;">&#91;</span>username setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>keychain objectForKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecAttrAccount<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;username: %@&quot;</span>, <span style="color: #002200;">&#91;</span>username text<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>For the password, I request the key <strong>kSecValueData</strong>:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Get password from keychain (if it exists)  </span>
<span style="color: #002200;">&#91;</span>password setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>keychain objectForKey<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>kSecValueData<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;password: %@&quot;</span>, <span style="color: #002200;">&#91;</span>password text<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>For this simple application, at this point we are done, that&#8217;s all that is required to write and read items with the keychain.</p>
<h5>Notes:</h5>
<ul>
<li>Writing and retrieving values from the keychain doesn&#8217;t seem to work in the simulator.</li>
<li>Uninstalling an application will not remove the items from the keychain. If you run the demo application on your device, and uninstall the app, the username and password will remain in the keychain (a firmware reset will, of course, remove the keychain values).
</ul>
<h5>Apple Keychain Resources</h5>
<p><a href="http://developer.apple.com/library/mac/#documentation/Security/Conceptual/keychainServConcepts/iPhoneTasks/iPhoneTasks.html" target="_blank">Keychain Services Programming Guide</a></p>
<p><a href="http://developer.apple.com/library/ios/#samplecode/GenericKeychain/Introduction/Intro.html" target="_blank">GenericKeychain Example (source code)</a></p>
<h5>Keychain Xcode Project</h5>
<p>Download the demo project: &#8211; <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/07/XcodeKeychainExample.zip">Store Username and Password to Keychain</a></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/using-keychain-to-store-username-and-password.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>How to Send an SMS Progammatically</title>
		<link>http://iPhoneDeveloperTips.com/core-services/how-to-send-an-sms-progammatically.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/how-to-send-an-sms-progammatically.html#comments</comments>
		<pubDate>Wed, 01 Jun 2011 11:08:45 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=8505</guid>
		<description><![CDATA[This post shows the basics for sending an SMS message from within an iPhone application. The class you&#8217;ll need to use is MFMessageComposeViewController which presents the standard SMS interface for composing and sending messages. As you&#8217;ll see in the example that follows, you can also pre-populate the body of the message as well one or [...]]]></description>
			<content:encoded><![CDATA[<p>This post shows the basics for sending an SMS message from within an iPhone application. The class you&#8217;ll need to use is <strong>MFMessageComposeViewController</strong> which presents the standard SMS interface for composing and sending messages. As you&#8217;ll see in the example that follows, you can also pre-populate the body of the message as well one or more recipients for the SMS.<br />
<span id="more-8505"></span></p>
<h5>SMS View Controller Interface</h5>
<p>The view controller that will send the SMS is shown below, notice the message composer import statement as well as the reference to the protocol <strong>MFMessageComposeViewControllerDelegate</strong>. The delegate has just one method where you can check the result of the message (sent, cancelled or failed) and this is also where you dismiss the view controller show the message composer &#8211; more on this method in a moment.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &lt;UIKit/UIKit.h&gt;</span>
<span style="color: #6e371a;">#import &lt;MessageUI/MFMessageComposeViewController.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@interface</span> TestViewController <span style="color: #002200;">:</span> UIViewController &lt;MFMessageComposeViewControllerDelegate&gt;
<span style="color: #002200;">&#123;</span>
  UIButton <span style="color: #002200;">*</span>buttonSMS;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>SMS View Controller Implementation</h5>
<p>The implementation code begins with a very simple <strong>loadView</strong> method, this is where the view is created and a button defined to initiate sending the SMS:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>loadView 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>self setView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;  
&nbsp;
  buttonSMS <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIButton buttonWithType<span style="color: #002200;">:</span>UIButtonTypeRoundedRect<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>buttonSMS setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">180</span>, <span style="color: #2400d9;">40</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>buttonSMS setCenter<span style="color: #002200;">:</span>CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">208</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>buttonSMS setTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Send SMS&quot;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>buttonSMS addTarget<span style="color: #002200;">:</span>self action<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> forControlEvents<span style="color: #002200;">:</span>UIControlEventTouchUpInside<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> addSubview<span style="color: #002200;">:</span>buttonSMS<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/06/sms1.png" alt="" width="250" height="375" /></p>
<p>Notice in the button code above the target and action values, when a touch up inside event occurs, the method <strong>buttonPressed</strong> is called. Inside this method we call a method to send an SMS, passing in the body of the SMS message as well as the phone numbers of the recipients of the message:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>button <span style="color: #002200;">==</span> buttonSMS<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#91;</span>self sendSMS<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Body of SMS...&quot;</span> recipientList<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;+1-111-222-3333&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;111-333-4444&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Sending an SMS</h5>
<p>The last two methods to cover manage creating an instance of <strong>MFMessageComposeViewController</strong> to create the SMS content and another method for handling the user interaction with the SMS dialog.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>sendSMS<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>bodyOfMessage recipientList<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>recipients
<span style="color: #002200;">&#123;</span>
  MFMessageComposeViewController <span style="color: #002200;">*</span>controller <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MFMessageComposeViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">if</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>MFMessageComposeViewController canSendText<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    controller.body <span style="color: #002200;">=</span> bodyOfMessage;    
    controller.recipients <span style="color: #002200;">=</span> recipients;
    controller.messageComposeDelegate <span style="color: #002200;">=</span> self;
    <span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>controller animated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>    
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>messageComposeViewController<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MFMessageComposeViewController <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>controller didFinishWithResult<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>MessageComposeResult<span style="color: #002200;">&#41;</span>result
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>self dismissModalViewControllerAnimated<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>result <span style="color: #002200;">==</span> MessageComposeResultCancelled<span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Message cancelled&quot;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #a61390;">else</span> <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>result <span style="color: #002200;">==</span> MessageComposeResultSent<span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Message sent&quot;</span><span style="color: #002200;">&#41;</span>  
  <span style="color: #a61390;">else</span> 
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Message failed&quot;</span><span style="color: #002200;">&#41;</span>  
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>In the sendSMS method, the first check is to ensure the device supports sending messages. Upon success, the body, recipients and delegate are set. The last step is to present a modal view controller to show the SMS dialog. </p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/06/sms2.png" alt="" width="250" height="375" /></p>
<p>Once the user taps either the cancel or send on the SMS dialog, the method <strong>messageComposeViewController</strong> is called, here you can check whether the message was sent, cancelled or failed. This is also the time to dismiss the view controller presented in the previous method.</p>
<h5>Build the Project</h5>
<p>One last step before you can compile the application, you&#8217;ll need to add the <strong>MessageUI.framework</strong> to your project.</p>
<h5>SMS on Simulator</h5>
<p>You cannot send SMS messages from within the simulator, if you do, you will see the message shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/06/sms3.png" alt="" width="250" height="367" /></p>
<h5>Send SMS Xcode Project</h5>
<p><a href="http://iPhoneDeveloperTIps.com/wp-content/uploads/2011/06/smsApp.zip">Download the send SMS Xcode project</a>.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/how-to-send-an-sms-progammatically.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Local Notifications</title>
		<link>http://iPhoneDeveloperTips.com/core-services/local-notifications.html</link>
		<comments>http://iPhoneDeveloperTips.com/core-services/local-notifications.html#comments</comments>
		<pubDate>Mon, 16 May 2011 09:04:47 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Core Services]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=8381</guid>
		<description><![CDATA[In this post I&#8217;ll show you the basics for working with local notifications, which are notifications generated from the device itself, versus a remote notification (aka push notification) that is sent from a remote server. Local notifications are useful when you need to schedule a reminder or otherwise notify the user of a future event. [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;ll show you the basics for working with local notifications, which are notifications generated from the device itself, versus a remote notification (aka push notification) that is sent from a remote server. Local notifications are useful when you need to schedule a reminder or otherwise notify the user of a future event.</p>
<h5>Creating and Processing Local Notifications</h5>
<p>Local notifications are scheduled for a future date by creating a <strong>UILocalNotification</strong> object. When creating the notification you can also specify the type of notification(s) to send, a badge, alert or sound.<br />
<span id="more-8381"></span></p>
<p>If a local notification arrives on the application that is not running in the foreground, the notification is delivered (that is, the alert shown, the badge set, sound played). If the notification is an alert, a dialog is shown to the user, along with an option to open the application. If the user chooses to open the app, the method <strong>didFinishLaunchingWithOptions</strong> in the application delegate is called passing in the local notification object.</p>
<p>If the application is running when a local notification is received, the method <strong>didReceiveLocalNotification</strong> is called, again, passing in the local notification object.</p>
<h5>Screenshots</h5>
<p>The app I&#8217;ve written to work with notification has a range of buttons to schedule various types of notifications:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/05/localNotif1.jpg" alt="" width="334" height="335" /></p>
<p>Once you choose an option, the notification is scheduled &#8211; to keep things simple, in my application I&#8217;ve set all notifications to arrive in 60 seconds. The screenshot below shows an incoming notification when the app has been put into the background.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/05/localNotif2.jpg" alt="" width="334" height="335" /></p>
<p>The screenshot below is an alert I display from within the method called when a notification arrives.</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/05/localNotif3.jpg" alt="" /></p>
<h5>Methods in the App Delegate</h5>
<p>The application delegate contains the two methods needed for managing incoming notifications. The method <strong>didFinishLaunchingWithOptions</strong> is shown below. Notice the check of the <strong>launchOptions</strong> parameter early in the method, if this value is nil, this is a standard application startup. If the value is not nil, the application was started based on a notification presented to the user &#8211; in this case I show an alert along with a few debug messages, including the payload data received from the notification (more on this below).</p>
<p>The code in the later half of the method is typical startup stuff, creating a window and view controller, etc</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">BOOL</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didFinishLaunchingWithOptions<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>launchOptions
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Handle launching from a notification</span>
  UILocalNotification <span style="color: #002200;">*</span>notification <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>launchOptions objectForKey<span style="color: #002200;">:</span>UIApplicationLaunchOptionsLocalNotificationKey<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// If not nil, the application was based on an incoming notifiation</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>notification<span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Notification initiated app startup&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Access the payload content</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Notification payload: %@&quot;</span>, <span style="color: #002200;">&#91;</span>notification.userInfo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;payload&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
    UIAlertView <span style="color: #002200;">*</span>alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Launched with Options&quot;</span> 
                              message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Notification initiated app startup&quot;</span> 
                              delegate<span style="color: #002200;">:</span>self cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok&quot;</span> 
                              otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Reset the badge</span>
  <span style="color: #002200;">&#91;</span>application setApplicationIconBadgeNumber<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create and initialize the window</span>
  window <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIWindow alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> bounds<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create test view controller</span>
  vc <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>TestViewController alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>window addSubview<span style="color: #002200;">:</span>vc.view<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>window makeKeyAndVisible<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">return</span> <span style="color: #a61390;">YES</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The second method in the application delegate is <strong>didReceiveLocalNotification</strong> which will be called when a notification arrives while the applications is running. In this method I show an alert and also write the notification payload to the debug console. For the payload, I am creating an array of objects, packaging the array within a dictionary and passing the later along with the notification.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>application<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIApplication <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>application didReceiveLocalNotification<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UILocalNotification <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification 
<span style="color: #002200;">&#123;</span>
  UIAlertView <span style="color: #002200;">*</span>alert <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIAlertView alloc<span style="color: #002200;">&#93;</span> initWithTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Launched with Options&quot;</span> 
                            message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Incoming notification in running app&quot;</span> 
                            delegate<span style="color: #002200;">:</span>self cancelButtonTitle<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Ok&quot;</span> 
                            otherButtonTitles<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>alert show<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>alert release<span style="color: #002200;">&#93;</span>;
&nbsp;
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Incoming notification in running app&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Access the payload content</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Notification payload: %@&quot;</span>, <span style="color: #002200;">&#91;</span>notification.userInfo objectForKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;payload&quot;</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Scheduling a Local Notification</h5>
<p>The buttons shown in the screenshot above are how we initiate creating and scheduling a local notification. Before handling each event there is some common code for creating a <strong>UILocalNotification</strong> object, setting the fire date and populating the data (payload) that will accompany the notification.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>buttonPressed<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIButton <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>button
<span style="color: #002200;">&#123;</span>
  UILocalNotification <span style="color: #002200;">*</span>localNotification <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UILocalNotification alloc<span style="color: #002200;">&#93;</span> init<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">!</span>localNotification<span style="color: #002200;">&#41;</span> 
    <span style="color: #a61390;">return</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Current date</span>
  <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>date <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDate</span> date<span style="color: #002200;">&#93;</span>; 
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Add one minute to the current time</span>
  <span style="color: #400080;">NSDate</span> <span style="color: #002200;">*</span>dateToFire <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>date dateByAddingTimeInterval<span style="color: #002200;">:</span><span style="color: #2400d9;">60</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set the fire date/time</span>
  <span style="color: #002200;">&#91;</span>localNotification setFireDate<span style="color: #002200;">:</span>dateToFire<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>localNotification setTimeZone<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSTimeZone</span> defaultTimeZone<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create a payload to go along with the notification</span>
  <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>array <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Value 1&quot;</span>, <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Value 2&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSDictionary</span> dictionaryWithObject<span style="color: #002200;">:</span>array forKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;payload&quot;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>localNotification setUserInfo<span style="color: #002200;">:</span>data<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>button <span style="color: #002200;">==</span> buttonAlert || button <span style="color: #002200;">==</span> buttonAll<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>	
    <span style="color: #11740a; font-style: italic;">// Setup alert notification</span>
    <span style="color: #002200;">&#91;</span>localNotification setAlertBody<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Incoming Local Notification&quot;</span> <span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>localNotification setAlertAction<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Open App&quot;</span><span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>localNotification setHasAction<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;      
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>button <span style="color: #002200;">==</span> buttonBadge || button <span style="color: #002200;">==</span> buttonAll<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Set badge notification, increment current badge value</span>
    <span style="color: #002200;">&#91;</span>localNotification setApplicationIconBadgeNumber<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> applicationIconBadgeNumber<span style="color: #002200;">&#93;</span> <span style="color: #002200;">+</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>button <span style="color: #002200;">==</span> buttonSound || button <span style="color: #002200;">==</span> buttonAll<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Setup sound notification</span>
    <span style="color: #002200;">&#91;</span>localNotification setSoundName<span style="color: #002200;">:</span>UILocalNotificationDefaultSoundName<span style="color: #002200;">&#93;</span>;			  
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Schedule the notification        </span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> scheduleLocalNotification<span style="color: #002200;">:</span>localNotification<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Local Notification Xcode Project</h5>
<p><a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/05/Xcode-Local-Notifications.zip">Download Xcode Local Notification Example</a></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/core-services/local-notifications.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

