<?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>Indie iPhone Development Blog &#187; objective-C</title>
	<atom:link href="http://blog.indieiphonedev.com/tag/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.indieiphonedev.com</link>
	<description>The musings of an independent iPhone developer</description>
	<lastBuildDate>Mon, 20 Jun 2011 18:48:16 +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>On constructive critizism</title>
		<link>http://blog.indieiphonedev.com/2011/06/04/on-constructive-critizism/</link>
		<comments>http://blog.indieiphonedev.com/2011/06/04/on-constructive-critizism/#comments</comments>
		<pubDate>Sat, 04 Jun 2011 18:23:53 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[app review]]></category>
		<category><![CDATA[iPhone app]]></category>
		<category><![CDATA[iPhone OS]]></category>
		<category><![CDATA[objective-C]]></category>

		<guid isPermaLink="false">http://blog.indieiphonedev.com/?p=304</guid>
		<description><![CDATA[There has been a couple of blog posts lately regarding the bloggy criticism of iOS applications by other app developers as well as general tech bloggers. These posts seem to suggest that iOS developers should band together and not criticize each others applications. I generally agree with the statement &#8220;be excellent to each other&#8221; &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>There has been a couple of <a href="http://www.cimgf.com/2011/06/03/why-so-serious/">blog</a> <a href="http://iphonedevelopment.blogspot.com/2011/06/on-being-excellent-to-each-other.html">posts</a> lately regarding the bloggy criticism of iOS applications by other app developers as well as general tech bloggers.  These posts seem to suggest that iOS developers should band together and not criticize each others applications.  I generally agree with the statement &#8220;be excellent to each other&#8221; &#8211; but if that means patting each others backs and saying good job even if the application has flaws then the iOS platform is in trouble.  I think that one of the great things about the iOS platform is the constant push to make something that is greater and more amazing than what has been done before.<br />
<P><br />
Getting criticism, be it from users through app reviews or emails or from bloggers, is a great way to push everyone to make better applications.  I can clearly identify with the fact that developers have an emotional connection to the applications that they make.  None of my applications make a huge amount of money and my main motivation is to provide people with great tools to use on their phone &#8211; so getting emails or reviews saying &#8220;this app sucks&#8221; is a feeling not much different than helping a complete stranger just to have them spit at you.<br />
<P><br />
In my opinion, the app review system setup by Apple is one of the greatest innovations of the iOS platform and has been instrumental in the success of the App Store &#8211; before this the only way to give feedback was to email the developer and then they could do whatever they wanted &#8211; you had already paid for the app so they had no incentive to actually fix the issue.  With public reviews and a rating system the incentive to fix issues brought up is much greater and this is something that will push apps to become better.  Several times this has brought great new features to my apps and alerted me to features that were not as intuitive as I had originally thought.<br />
<P><br />
Like many other Apple enthusiasts I was very excited for the launch of The Daily.  Like many others I was disappointed at the initial product.  I think that a lot of people expected the app to be Apple Great (like Maps, Garage Band etc) because of the hype and media buildup for the app and the apparent endorsement of the app by Apple.  However, the app has several short falls <a href="http://blogs.telegraph.co.uk/technology/shanerichmond/100006286/the-daily-ipad-app-review-a-complete-failure-of-imagination/">which</a> <a href="http://technorati.com/technology/article/ipad-app-review-the-daily/">were</a> <a href="http://www.tuaw.com/2011/02/06/more-thoughts-on-the-daily/">pointed</a> <a href="http://daringfireball.net/linked/2011/02/02/the-daily-in-action">out</a> and <a href="http://daringfireball.net/linked/2011/02/06/brichter-daily">suggestions for improvements</a> with implementation were posted.  I do not understand why the developer of the app would be surprised that the app got a harsh reception when the developer admits himself that there were issues with the app that if they had longer time could have been sorted out.<br />
<P><br />
I hope that the iOS developer community is grown up enough that we can accept criticism of our work and that we can learn from this and all strive towards making excellent if not perfect apps.  </p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.indieiphonedev.com%2F2011%2F06%2F04%2Fon-constructive-critizism%2F&amp;title=On%20constructive%20critizism" id="wpa2a_2"><img src="http://blog.indieiphonedev.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.indieiphonedev.com/2011/06/04/on-constructive-critizism/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing iAd in iPhone applications</title>
		<link>http://blog.indieiphonedev.com/2011/04/27/implementing-iad-in-iphone-applications/</link>
		<comments>http://blog.indieiphonedev.com/2011/04/27/implementing-iad-in-iphone-applications/#comments</comments>
		<pubDate>Wed, 27 Apr 2011 05:11:33 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[free app]]></category>
		<category><![CDATA[iAd]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[objective-C]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://blog.indieiphonedev.com/?p=296</guid>
		<description><![CDATA[There are a couple of tricks to implementing iAd in iPhone applications that are not 100% spelled out in the documentation provided by Apple. If you do not implement right your app will crash on phones running older versions of iOS such as iOS4.1 and iOS3. For your .h file: In the .m file you [...]]]></description>
			<content:encoded><![CDATA[<p>There are a couple of tricks to implementing iAd in iPhone applications that are not 100% spelled out in the documentation provided by Apple.  If you do not implement right your app will crash on phones running older versions of iOS such as iOS4.1 and iOS3. </p>
<p>For your .h file:</p>
<pre class="brush: objc; title: ; notranslate">
#import &lt;UIKit/UIKit.h&gt;;
#import &lt;iAd/iAd.h&gt;;
@interface iAdViewController : UIViewController &lt;ADBannerViewDelegate&gt; {
	ADBannerView *adView;
 	BOOL bannerIsVisible;
}
@property (nonatomic,assign) BOOL bannerIsVisible;
@end
</pre>
<p>In the .m file you need the following:</p>
<pre class="brush: objc; title: ; notranslate">
- (void) viewWillAppear:(BOOL)animated {
	// check if iAd is available
	Class classAdBannerView = NSClassFromString(@&quot;ADBannerView&quot;);
	if (classAdBannerView) {
		// create iAd
		ADBannerView *bannerView = [[classAdBannerView alloc] initWithFrame:CGRectZero];

		if (&amp;ADBannerContentSizeIdentifierPortrait != nil) {
			// NEWER
			DLog(@&quot;NEWER&quot;);
			bannerView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
			bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
		} else {
			// OLDER
			DLog(@&quot;OLDER&quot;);
			bannerView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
			bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
		}

		bannerView.delegate=self;

		self.adView = bannerView;
		[self.view addSubview:adView];
		self.bannerIsVisible=NO;
		[bannerView release];

	}
	[super viewWillAppear: animated];
}
</pre>
<p>I am creating the bannerView programatically in order to have the app also work on pre iOS 4.1 devices.  The check for ADBannerContentSizeIdentifierPortrait is to see if the user is using iOS 4.1 or newer iOS.  In iOS 4.1 the identifiers were based on size &#8211; but in newer iOS the iPad got iAds and the size no longer made sense &#8211; thus the landscape/portrait designations.</p>
<p>Finally &#8211; if your app supports multiple interface orientations &#8211; you need to let the banner know that the device rotated so it can show the right size ad.</p>
<pre class="brush: objc; title: ; notranslate">
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    if (&amp;ADBannerContentSizeIdentifierPortrait != nil) {
        // NEWER
		DLog(@&quot;NEWER&quot;);
        if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
            adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
        else
            adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    } else {
        // OLDER
		DLog(@&quot;OLDER&quot;);
        if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
            adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier480x32;
        else
            adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
    }
}
</pre>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.indieiphonedev.com%2F2011%2F04%2F27%2Fimplementing-iad-in-iphone-applications%2F&amp;title=Implementing%20iAd%20in%20iPhone%20applications" id="wpa2a_4"><img src="http://blog.indieiphonedev.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.indieiphonedev.com/2011/04/27/implementing-iad-in-iphone-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting the timezone on the device</title>
		<link>http://blog.indieiphonedev.com/2011/01/09/getting-the-timezone-on-the-device/</link>
		<comments>http://blog.indieiphonedev.com/2011/01/09/getting-the-timezone-on-the-device/#comments</comments>
		<pubDate>Sun, 09 Jan 2011 20:02:41 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[iPhone app]]></category>
		<category><![CDATA[objective-C]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[timezone]]></category>

		<guid isPermaLink="false">http://blog.indieiphonedev.com/?p=258</guid>
		<description><![CDATA[Since the LogYourRun iPhone application will show you your running data in week and month views it is necessary to know the timezone of the user in order to bring up the data for the correct week/month. At first I thought I could use the NSDateFormatter to do this. The documentation says that you can [...]]]></description>
			<content:encoded><![CDATA[<p>Since the LogYourRun iPhone application will show you your running data in week and month views it is necessary to know the timezone of the user in order to bring up the data for the correct week/month.  At first I thought I could use the NSDateFormatter to do this.  The documentation says that you can get timezone informationby using %z or %Z as the format.  The %z should get you the offset in hours while the %Z should get you the name.  However when tested it turned out that the opposite is the case&#8230; </p>
<p>The following code gets you the timezone offset in hours:</p>
<pre class="brush: objc; title: ; notranslate">
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@&quot;Z&quot;]; // error in documentation - the zone in hours should be 'z' not 'Z'
timezoneoffset = (int) [[dateFormatter stringFromDate:[NSDate date]] intValue]/100;
DLog(@&quot;format %@&quot;, [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter release];
</pre>
<p>(Note the division by 100 is because the timezone is reported in military time so for 30 min offset the final timezone would be read as x.30 which is not really correct &#8211; but for getting a ballpark figure it would suffice).</p>
<p>Since the code is not behaving according to documentation it is not a good idea to use this to get the timezone because the code will break if the code ever starts behaving according to documentation.  </p>
<p>Instead it turns out that there is a very nice NSTimeZone object which will give you the timezone offset from GMT in seconds:</p>
<pre class="brush: objc; title: ; notranslate">
int timezoneoffset = ([[NSTimeZone systemTimeZone] secondsFromGMT] / 3600);
</pre>
<p>Then all you have to do to get the hour difference is divide by 3600 (seconds per hour).  If you cared about the half hour offsets you would make this a float &#8211; but this is plenty for getting a ballpark figure.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.indieiphonedev.com%2F2011%2F01%2F09%2Fgetting-the-timezone-on-the-device%2F&amp;title=Getting%20the%20timezone%20on%20the%20device" id="wpa2a_6"><img src="http://blog.indieiphonedev.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.indieiphonedev.com/2011/01/09/getting-the-timezone-on-the-device/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MGTwitterEngine and Locations</title>
		<link>http://blog.indieiphonedev.com/2010/09/03/mgtwitterengine-and-locations/</link>
		<comments>http://blog.indieiphonedev.com/2010/09/03/mgtwitterengine-and-locations/#comments</comments>
		<pubDate>Sat, 04 Sep 2010 04:35:41 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[GPS]]></category>
		<category><![CDATA[iPhone app]]></category>
		<category><![CDATA[iPhone OS]]></category>
		<category><![CDATA[objective-C]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://blog.indieiphonedev.com/?p=212</guid>
		<description><![CDATA[How to implement location of tweets from iPhone applications using the MGTwitterEngine.]]></description>
			<content:encoded><![CDATA[<p>So Twitter just updated their API to disallow the use of authentication through the REST API.  They were nice enough to announce that in an email 2 days after they turned it off so that meant that I had to learn all about OAuth overnight and scramble to find something that could take over the Twitter functionality of the LogYourRun iPhone app.</p>
<p>Luckily most of the leg work in this are has been done by Matt Gemmell and his <a href="http://mattgemmell.com/2008/02/22/mgtwitterengine-twitter-from-cocoa">Twitter engine</a>.  Some slight modifications are required in order to get it <a href="http://github.com/bengottlieb/Twitter-OAuth-iPhone">running on the iPhone</a> and <a href="http://www.2bros1blog.com/2010/07/switching-from-basic-to-xauth-with-mgtwitterengine-on-iphone/">working with OAuth</a>.  Update:  The guys at iCodeBlog have a <a href="http://icodeblog.com/2010/09/16/dealing-with-the-twitter-oauth-apocalypse/">great tutorial</a> on how to implement the libraries.</p>
<p>With these libraries dropped in the new Twitter authentication system is fairly easy to implement.  The main problem is that the MGTwitterEngine does not support the Twitter location API.  Since this is an important piece of <a href="http://tweetmydistance.com/">TweetMyDistance</a> I modified the MGTwitterEngine.m and added a function for tweeting with location.  The new method takes latitude and longitude as well as the tweet and if this is a reply to a previous tweet.  As you can see from <a href="http://twitter.com/tim221175/statuses/22930742608">my tweet</a> it works great.  The code below should replace the code in MGTwitterEngine and don&#8217;t forget to also update your .h file.</p>
<pre class="brush: objc; title: ; notranslate">
- (NSString *)sendUpdate:(NSString *)status
{
    return [self sendUpdate:status inReplyTo:0];
}

- (NSString *)sendUpdate:(NSString *)status inReplyTo:(unsigned long)updateID
{

	return [self sendUpdate: status inReplyTo: updateID withLatitude: 0.0 andLongitude: 0.0];

}

- (NSString *)sendUpdate:(NSString *)status
			   inReplyTo:(unsigned long)updateID
			withLatitude:(double) lat
			andLongitude: (double) lng
{
	if (!status) {
        return nil;
    }

    NSString *path = [NSString stringWithFormat:@&quot;statuses/update.%@&quot;, API_FORMAT];

    NSString *trimmedText = status;
    if ([trimmedText length] &gt; MAX_MESSAGE_LENGTH) {
        trimmedText = [trimmedText substringToIndex:MAX_MESSAGE_LENGTH];
    }

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    [params setObject:trimmedText forKey:@&quot;status&quot;];
    if (updateID &gt; 0) {
        [params setObject:[NSString stringWithFormat:@&quot;%u&quot;, updateID] forKey:@&quot;in_reply_to_status_id&quot;];
    }
    if (lat != 0.0 &amp;&amp; lng != 0.0) {
		// lat=%1.6f&amp;long=%1.6f&amp;display_coordinates=true
        [params setObject:[NSString stringWithFormat:@&quot;%1.6f&quot;, lat] forKey:@&quot;lat&quot;];
        [params setObject:[NSString stringWithFormat:@&quot;%1.6f&quot;, lng] forKey:@&quot;long&quot;];
        [params setObject:@&quot;true&quot; forKey:@&quot;display_coordinates&quot;];
    }
    NSString *body = [self _queryStringWithBase:nil parameters:params prefixed:NO];

	DLog(@&quot; twitterbody: %@&quot;, body);

    return [self _sendRequestWithMethod:HTTP_POST_METHOD path:path
                        queryParameters:params body:body
                            requestType:MGTwitterUpdateSendRequest
                           responseType:MGTwitterStatus];
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.indieiphonedev.com/2010/09/03/mgtwitterengine-and-locations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Converting App from iPhone to Universal</title>
		<link>http://blog.indieiphonedev.com/2010/04/29/converting-app-from-iphone-to-universal/</link>
		<comments>http://blog.indieiphonedev.com/2010/04/29/converting-app-from-iphone-to-universal/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 03:54:47 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone app]]></category>
		<category><![CDATA[objective-C]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://blog.indieiphonedev.com/?p=194</guid>
		<description><![CDATA[With the iPad selling like hot cakes it seems like a good idea to get on the bandwagon and make some apps for the iPad.  The LogYourRun app is not particularly well suited for running on the iPad &#8211; I doubt that people will be strapping their iPads to their arms and run around the [...]]]></description>
			<content:encoded><![CDATA[<p>With the iPad selling like hot cakes it seems like a good idea to get on the bandwagon and make some apps for the iPad.  The LogYourRun app is not particularly well suited for running on the iPad &#8211; I doubt that people will be strapping their iPads to their arms and run around the park.  However, the heart rate app seems like something that people would enjoy using even on iPads.  Since I did not want to have to maintain two separate apps I decided to make the app universal.  This was actually quite an easy process and took only about 10-12 hours (could have been faster if I had known what I was doing &#8211; which is why I wanted to share what I learned).</p>
<p>First step is to make sure you copy your application folder so you will be able to go back to the original application if you mess up.  Then with your newly copied application upgrade your target to work on the iPad in addition to the iPhone.  Click on your target and go into the menu &#8211; under Project you will find an entry that says &#8220;upgrade current target for iPad&#8221;.  Select the &#8220;One Universal application&#8221; option.  This will generate a new group which contains your new iPad version of the MainWindow.xib file &#8211; MainWindow-iPad.xib.  You can use this new nib to load all your iPad specific views.</p>
<p>At this point I went through all my existing nibs and created iPad versions (can be done in the Interface Builder) and saved them as XxxViewController-iPad.xib in the Resources-iPad folder.  For iPad nibs you have to make sure that you have versions of the nib that look decent and are usable in both landscape and horizontal orientations.  This can be tricky if you have a lot of UI elements but is very important since iPad apps have to support all orientations.  Make sure you anchor your UI elements to the right edges and that they scale to look nice.</p>
<p>The heart rate app uses a tab bar.  It took me the longest time to figure out how to get this to rotate.  The reason for this was that I had not read <a href="http://developer.apple.com/iphone/library/featuredarticles/ViewControllerPGforiPhoneOS/TabBarControllers/TabBarControllers.html#//apple_ref/doc/uid/TP40007457-CH102-SW26" target="_blank">Apple&#8217;s documentation</a>.  Turns out that <strong>all</strong> the view controllers that the tab bar links to have to return YES for all shouldAutorotateToInterfaceOrientation: orientations.  This makes sense conceptually.  In addition you need to <a href="http://blog.costan.us/2009/01/auto-rotating-tab-bars-on-iphone.html">set your view controllers</a> to automatically resize when rotation occurs.  Once these are set you will be able to see what the rotation looks like in the iPad simulator (hopefully great).</p>
<p>Loading the iPad specific nibs using the view controller is a great way to get these to show up on the iPad only &#8211; but there are some cases where you will need to load a view controller programatically (in my case when the user clicks on the help button it brings up the help VC).  To make sure that you load the right nib you can check if the app is running on the iPad.  The following code is what I used:</p>
<pre>
<pre class="brush: objc; title: ; notranslate">
- (BOOL) isIPad {
#if (__IPHONE_OS_VERSION_MAX_ALLOWED &gt;= 30200)
  if ([[UIDevice currentDevice] respondsToSelector: @selector(userInterfaceIdiom)])
    return ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad);
#endif

  return NO;
}
</pre>
</pre>
<p>This code will check if the iDevice responds to the userInterfaceIdiom selector and if it does it checks if the device is an iPad.  If it is I load the iPad version of the helpVC if not then I load the iPhone version of the helpVC.  Make sure you also put the respondToRotation in these VCs since they will need to respond predictably also.</p>
<p>Finally you will need to create a default image that can be presented on the iPhone.  In your info.plist you can set the &#8220;Launch image (iPad)&#8221; to the base name of the iPad graphic (e.g. Default-iPad.png).  You can then create two images named Default-iPad-Landscape and Default-iPad-Portrait and add them to your project.  The iPad will pick whichever of the default images that fits with the rotation of the device.</p>
<p>When uploading your app &#8211; build the app in OS3.2.  The app will run on both iPhone and iPad from one binary. You will also have to upload screenshots for the iPad version for display in the iPad app store and create two new icons (50&#215;50 and 72&#215;72).  Once you have all that together your app is ready for submission and you can start praying that it will be a speedy path to acceptance.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.indieiphonedev.com%2F2010%2F04%2F29%2Fconverting-app-from-iphone-to-universal%2F&amp;title=Converting%20App%20from%20iPhone%20to%20Universal" id="wpa2a_8"><img src="http://blog.indieiphonedev.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.indieiphonedev.com/2010/04/29/converting-app-from-iphone-to-universal/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using audio services to play alert sound</title>
		<link>http://blog.indieiphonedev.com/2010/03/22/using-audio-services-to-play-alert-sound/</link>
		<comments>http://blog.indieiphonedev.com/2010/03/22/using-audio-services-to-play-alert-sound/#comments</comments>
		<pubDate>Tue, 23 Mar 2010 04:24:36 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[iPhone app]]></category>
		<category><![CDATA[objective-C]]></category>
		<category><![CDATA[sounds]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://blog.indieiphonedev.com/?p=150</guid>
		<description><![CDATA[Some times it your application may inadvertently quit with out the user meaning to quit your app.  For example people run with the running/GPS app and the iPhone may be in a pocket / arm band / hand where the square button could be pressed unintentionally.  If the app quits while running it will not [...]]]></description>
			<content:encoded><![CDATA[<p>Some times it your application may inadvertently quit with out the user meaning to quit your app.  For example people run with the running/GPS app and the iPhone may be in a pocket / arm band / hand where the square button could be pressed unintentionally.  If the app quits while running it will not collect running data for the rest of the run and the user will be very disappointed.  To avoid this I have setup an alert which will make a sound if the user quits the app while the app is collecting running data.  I was able to find the original apple alert aiff files via Google and I chose to implement the Indigo sound as the alert &#8211; the cricket is not really loud enough to be an alert sound.</p>
<p>By implementing the playback of the sound as an alert sound the sound will be played even on the 1st gen iPod Touch which does not have a speaker.  Alert sounds can also be set to finish playback after the app finishes &#8211; which is an important feature for this use.  I was able to find how to set this property on <a href="http://nagano.monalisa-au.org/?p=721">Nagano&#8217;s blog</a>.  Not sure what most of the page says but the language of Objective C is universal. Remember to add the audiotoolbox to your project.</p>
<pre class="brush: objc; title: ; notranslate">

#include &amp;lt;AudioToolbox/AudioToolbox.h&amp;gt;
</pre>
<p>I then load the sound in the viewDidLoad init function for the view controller where I want to play the sound &#8211; this way you are not trying to load the sound as the app is quitting.  The soundFileObject isa global for this VC.</p>
<pre class="brush: objc; title: ; notranslate">
// SOUNDS
 NSString *path = [[NSBundle mainBundle] pathForResource:@&amp;quot;Indigo&amp;quot; ofType:@&amp;quot;aiff&amp;quot;];
 NSURL *fileURL = [NSURL fileURLWithPath:path];

 OSStatus err;
 err = AudioServicesCreateSystemSoundID((CFURLRef)fileURL, &amp;amp;soundFileObject);
 if(err) {
   DLog(@&amp;quot;AudioServicesCreateSystemSoundID err = %d&amp;quot;,err);
   exit(1);
 }

 UInt32 flag = 1;
 err = AudioServicesSetProperty(kAudioServicesPropertyCompletePlaybackIfAppDies,
     sizeof(UInt32),
     &amp;amp;soundFileObject,
     sizeof(UInt32),
     &amp;amp;flag);
 if(err){
   DLog(@&amp;quot;AudioServicesSetProperty err = %d&amp;quot;,err);
 }
</pre>
<p>Then under viewWillDisappear I have the following:</p>
<pre class="brush: objc; title: ; notranslate">
 if (runActivity.isStarted) {
   // play alert sound if exiting while running
   AudioServicesPlayAlertSound (self.soundFileObject);
 }
</pre>
<p>This way the sound will play if the view is unloading while the user is expecting the app to be collecting data.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.indieiphonedev.com%2F2010%2F03%2F22%2Fusing-audio-services-to-play-alert-sound%2F&amp;title=Using%20audio%20services%20to%20play%20alert%20sound" id="wpa2a_10"><img src="http://blog.indieiphonedev.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.indieiphonedev.com/2010/03/22/using-audio-services-to-play-alert-sound/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WebKitErrorDomain error 101</title>
		<link>http://blog.indieiphonedev.com/2010/02/19/webkiterrordomain-101/</link>
		<comments>http://blog.indieiphonedev.com/2010/02/19/webkiterrordomain-101/#comments</comments>
		<pubDate>Sat, 20 Feb 2010 00:48:21 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[error code]]></category>
		<category><![CDATA[iPhone app]]></category>
		<category><![CDATA[objective-C]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://blog.indieiphonedev.com/?p=136</guid>
		<description><![CDATA[The LogYourRun app uses both UIWebView and NSURLRequest to communicate with the LogYourRun database.  To let users upload and access their data.  These requests often contain fields that the user enter such as running notes, distance, time etc.  I had the WebKitErrorDomain 101 error come up on occasions when users tried to send requests that [...]]]></description>
			<content:encoded><![CDATA[<p>The LogYourRun app uses both UIWebView and NSURLRequest to communicate with the LogYourRun database.  To let users upload and access their data.  These requests often contain fields that the user enter such as running notes, distance, time etc.  I had the WebKitErrorDomain 101 error come up on occasions when users tried to send requests that contained spaces that were not properly encoded.  I was not able to find any documentation on this error so it took a while to figure out what was going on.</p>
<div id="attachment_137" class="wp-caption aligncenter" style="width: 310px"><a href="http://blog.indieiphonedev.com/wp-content/uploads/2010/02/webkiterror.png"><img class="size-medium wp-image-137" title="webkiterror" src="http://blog.indieiphonedev.com/wp-content/uploads/2010/02/webkiterror-300x172.png" alt="" width="300" height="172" /></a><p class="wp-caption-text">WebKitErrorDomain error 101</p></div>
<p>Once I identified that the issue was that some users had a space in their username it is a very easy fix.  Just use the stringByAddingPercentEscapesUsingEncoding function to have the string properly encoded:</p>
<pre class="brush: objc; title: ; notranslate">
[username stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
</pre>
<p>So if you come along a WebKitErrorDomain 101 error &#8211; take a look at the URL you are trying to submit and see if all the parts of it are properly encoded.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.indieiphonedev.com%2F2010%2F02%2F19%2Fwebkiterrordomain-101%2F&amp;title=WebKitErrorDomain%20error%20101" id="wpa2a_12"><img src="http://blog.indieiphonedev.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.indieiphonedev.com/2010/02/19/webkiterrordomain-101/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to test location and accelerometer functions from the comfort of your home</title>
		<link>http://blog.indieiphonedev.com/2010/02/07/how-to-test-location-and-accelerometer-functions-from-the-comfort-of-your-home/</link>
		<comments>http://blog.indieiphonedev.com/2010/02/07/how-to-test-location-and-accelerometer-functions-from-the-comfort-of-your-home/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 03:01:11 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[accelerometer]]></category>
		<category><![CDATA[core location]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[iPhone app]]></category>
		<category><![CDATA[objective-C]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://blog.indieiphonedev.com/?p=123</guid>
		<description><![CDATA[The LogYourRun iPhone app uses both the GPS and the accelerometer functions of the iPhone.  Neither the GPS nor the accelerometer can be accessed on the iPhone simulator so to circumvent this I used the following methods: Accelerometer: Otto Chrons has a great Google code project which contains the code for an app that can [...]]]></description>
			<content:encoded><![CDATA[<p>The LogYourRun iPhone app uses both the GPS and the accelerometer functions of the iPhone.  Neither the GPS nor the accelerometer can be accessed on the iPhone simulator so to circumvent this I used the following methods:</p>
<p><strong>Accelerometer: </strong> Otto Chrons has a great <a href="http://code.google.com/p/accelerometer-simulator/wiki/Home" target="_blank">Google code project</a> which contains the code for an app that can be run on an iPod touch or iPhone which is on the same subnet as your development computer.  The app will transmit accelerometer data which can be caught by some code that you can insert into your Xcode project.  When you run your app on the iPhone simulator it will thus receive the accelerometer data from the real life device.  This was a great help in troubleshooting the setup of the pedometer functionality.  I was running around in my living room counting steps and comparing to the # of steps on the iPhone simulator.  Be sure to remove the accelerometer simulator code from your application before releasing.</p>
<p><strong>GPS:</strong> Everyone who has played with the location manager on the iPhone Simulator knows that you will only get only one location from the location manager on the iPhone Simulator.  Testing the GPS functionality is a critical part of testing the LogYourRun app.  I could run the app on an iPhone and then run/walk/drive around outside &#8211; but that would take much too long and the weather in Saint Louis is not conductive to such testing.  Instead I added a button to the pedometer screen of the application which when clicked runs the following code:</p>
<pre class="brush: objc; title: ; notranslate">
- (IBAction) locationButton:(id)sender {
   CLLocation *thisLocation;
   double latitude  = 37.33168900 + (float) ((random() % 100) +1) / 1000000.0;
   double longitude = -122.03073100 + (float) ((random() % 100) +1) / 1000000.0;
   thisLocation = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
   [self locationManager:locationManager didUpdateToLocation:thisLocation fromLocation:thisLocation];
   [thisLocation release];
}</pre>
<p>This piece of code creates a random location close to the location that the iPhone Simulator will usually get from the core location (One Infinite Loop).  The location is sent to the location manager delegate function of the pedometer class and is handled as if it came from the real location manager.  Since the location is different from the previous location the getDistanceFrom: still works.  When I am ready to submit the application all I have to do is hide the two buttons (probably should be done programatically &#8211; since one of these days I will forget).</p>
<div id="attachment_122" class="wp-caption aligncenter" style="width: 210px"><a href="http://blog.indieiphonedev.com/wp-content/uploads/2010/02/Screenshot.png"><img class="size-medium wp-image-122" title="Pedometer showing location and step button" src="http://blog.indieiphonedev.com/wp-content/uploads/2010/02/Screenshot-200x300.png" alt="Pedometer showing location and step button" width="200" height="300" /></a><p class="wp-caption-text">The pedometer screen showing location and step button</p></div>
<p style="text-align: center;">
<div id="attachment_127" class="wp-caption aligncenter" style="width: 210px"><a href="http://blog.indieiphonedev.com/wp-content/uploads/2010/02/Screenshot-2010.02.07-21.45.30.png"><img class="size-medium wp-image-127" title="Locations shown on map" src="http://blog.indieiphonedev.com/wp-content/uploads/2010/02/Screenshot-2010.02.07-21.45.30-200x300.png" alt="Locations shown on map" width="200" height="300" /></a><p class="wp-caption-text">The random locations shown on map</p></div>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.indieiphonedev.com%2F2010%2F02%2F07%2Fhow-to-test-location-and-accelerometer-functions-from-the-comfort-of-your-home%2F&amp;title=How%20to%20test%20location%20and%20accelerometer%20functions%20from%20the%20comfort%20of%20your%20home" id="wpa2a_14"><img src="http://blog.indieiphonedev.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.indieiphonedev.com/2010/02/07/how-to-test-location-and-accelerometer-functions-from-the-comfort-of-your-home/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How the LogYourRun App switches between pedometer and GPS mode</title>
		<link>http://blog.indieiphonedev.com/2010/01/24/how-the-logyourrun-app-switches-between-pedometer-and-gps-mode/</link>
		<comments>http://blog.indieiphonedev.com/2010/01/24/how-the-logyourrun-app-switches-between-pedometer-and-gps-mode/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 03:36:29 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[iPhone app]]></category>
		<category><![CDATA[objective-C]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://blog.indieiphonedev.com/?p=49</guid>
		<description><![CDATA[The LogYourRun application tracks distance traveled by using the GPS as well as by using the accelerometer as a pedometer. This allows the application to measure distance even when GPS reception is not available &#8211; so on those cold mornings you do not have to wait around for the iPhone to get good GPS reception [...]]]></description>
			<content:encoded><![CDATA[<p>The LogYourRun application tracks distance traveled by using the GPS as well as by using the accelerometer as a pedometer. This allows the application to measure distance even when GPS reception is not available &#8211; so on those cold mornings you do not have to wait around for the iPhone to get good GPS reception before starting your run &#8211; once GPS reception becomes adequate the GPS takes over from the pedometer.</p>
<p>The application exist in two states:</p>
<p>1) Record distance using GPS</p>
<p>2) Record distance using pedometer</p>
<p>Switching from pedometer to GPS is instant &#8211; once GPS signal is adequate distance is now recorded using GPS.</p>
<p>Switching from GPS to pedometer will occur if an adequate GPS signal has not been received over a period of 20 seconds.  At this point the pedometer will take over calculating the distance and since it had not been recording distance for the previous 20 seconds the pedometer will calculate the distance that was traveled over those 20 seconds by rolling up the number of steps that were taken since the last good GPS signal was received.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/J7S0R8gYm4c&amp;hl=en_US&amp;fs=1&amp;color1=0x3a3a3a&amp;color2=0x999999" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/J7S0R8gYm4c&amp;hl=en_US&amp;fs=1&amp;color1=0x3a3a3a&amp;color2=0x999999" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.indieiphonedev.com%2F2010%2F01%2F24%2Fhow-the-logyourrun-app-switches-between-pedometer-and-gps-mode%2F&amp;title=How%20the%20LogYourRun%20App%20switches%20between%20pedometer%20and%20GPS%20mode" id="wpa2a_16"><img src="http://blog.indieiphonedev.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.indieiphonedev.com/2010/01/24/how-the-logyourrun-app-switches-between-pedometer-and-gps-mode/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Encoding latitude and longitude the Google way</title>
		<link>http://blog.indieiphonedev.com/2010/01/22/encoding-latitude-and-longitude-the-google-way/</link>
		<comments>http://blog.indieiphonedev.com/2010/01/22/encoding-latitude-and-longitude-the-google-way/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 01:07:34 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[cocoa]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[google polyline]]></category>
		<category><![CDATA[objective-C]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://blog.indieiphonedev.com/?p=15</guid>
		<description><![CDATA[The LogYourRun iPhone App uses the GPS functionality of the iPhone to track running distance as well as the running route.  When data is uploaded from the app to the website the list of GPS coordinates have to be transferred from the phone to the website.  Since I already store route GPS data on the [...]]]></description>
			<content:encoded><![CDATA[<p>The LogYourRun iPhone App uses the GPS functionality of the iPhone to track running distance as well as the running route.  When data is uploaded from the app to the website the list of GPS coordinates have to be transferred from the phone to the website.  Since I already store route GPS data on the LogYourRun website as an encoded Google polyline I decided to have the iPhone encode the GPS data before sending to the website.  This takes processing load off the server and allows for faster transfer of data from the iPhone to the website.</p>
<p>I already have a JavaScript and a PHP version of the functions that both encode and decode GPS data as a Google polyline &#8211; so it was just a matter of converting the functions to Objective-C / Cocoa.</p>
<p>The way that the encoder works is first it multiples the coordinate by 10e5, takes the floor value and compares it to the coordinate to the 10e5 value of the previous coordinate and encodes the difference between the two points.  The following are functions used to perform the basic math.</p>
<p>This function is used to multiply the coordinate by 10e5:</p>
<pre class="brush: objc; title: ; notranslate">
- (int) floor1e5: (double) coordinate {
 return (int) floor(coordinate * 1e5);
}
</pre>
<p>This function is used to encode a signed number:</p>
<pre class="brush: objc; title: ; notranslate">
- (NSString *) encodeSignedNumber: (int) num {
int sgn_num = num &lt;&lt; 1;
 if (num &lt; 0) {
 sgn_num = ~(sgn_num);
 }
 return [self encodeNumber: sgn_num];
}
</pre>
<p>And this function is used to encode an unsigned int:</p>
<pre class="brush: objc; title: ; notranslate">
- (NSString *) encodeNumber: (int) num {
 int nextValue;
 NSMutableString *encodeString = [[NSMutableString alloc] initWithCapacity:5];
 [encodeString autorelease];

 while (num &amp;gt;= 0x20) {
 nextValue = (0x20 | (num &amp;amp; 0x1f)) + 63;
 [encodeString appendFormat:@&quot;%c&quot;, ((char) (nextValue))];
 num &amp;gt;&amp;gt;= 5;
 }

 num += 63;
 [encodeString appendFormat:@&quot;%c&quot;, ((char) (num))];

 return encodeString;
}
</pre>
<p>The following function is used to encode an array of CoreLocation objects (CLLocation *).  This array is called <em>locations</em> in the following code.  The  first thing the function does is to make sure that there are points in our <em>locations</em> assay.  Next I setup a mutable string which is going to contain the encoded polyline string as well as a temporary CoreLocation object (<em>tmploc</em>).  Since we have not started going through the locations we set the previous latitude and longitude to 0 each (<em>plat</em> and <em>plng</em>).  Then I go through the <em>locations</em> array and put them into the <em>temploc</em>.  For each location the previous latitude and longitude is subtracted from the current after the 1e5 conversion.  The encoded latitude and longitude are then added to the encoded polyline string which is returned at the end of the function.</p>
<pre class="brush: objc; title: ; notranslate">
- (NSString *) googlePolyline {
 int i, late5, lnge5, dlat, dlng, plat, plng;

 if ([locations count] == 0) {
 return @&quot;&quot;;
 }

 NSMutableString *encodedPoints = [[NSMutableString alloc] initWithCapacity:100];
 [encodedPoints autorelease];
 CLLocation *tmploc;

 plat = 0;
 plng = 0;

 for (i = 0; i &amp;lt; [locations count]; i++) {
 tmploc = [locations objectAtIndex:i];

 late5 = [self floor1e5: tmploc.coordinate.latitude];
 lnge5 = [self floor1e5: tmploc.coordinate.longitude];

 dlat = late5 - plat;
 dlng = lnge5 - plng;

 plat = late5;
 plng = lnge5;

 [encodedPoints appendFormat:@&quot;%@%@&quot;, [self encodeSignedNumber:dlat], [self encodeSignedNumber:dlng]];

 }
 return [[[NSString alloc] initWithFormat: @&quot;%@&quot;, encodedPoints] autorelease];
}
</pre>
<p>The googlePolyline function can be used to convert an array of location objects to a simple string which can then applied directly to a google map.</p>
<p>For other ports of the google polyline encoder see:</p>
<p><a href="http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/" target="_blank">http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/</a></p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.indieiphonedev.com%2F2010%2F01%2F22%2Fencoding-latitude-and-longitude-the-google-way%2F&amp;title=Encoding%20latitude%20and%20longitude%20the%20Google%20way" id="wpa2a_18"><img src="http://blog.indieiphonedev.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.indieiphonedev.com/2010/01/22/encoding-latitude-and-longitude-the-google-way/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

