<?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>[iPhoneiOS dev:tips];</title>
	<atom:link href="http://iPhoneDeveloperTips.com/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>Tips and Tricks for iPhone developers</description>
	<lastBuildDate>Wed, 25 Aug 2010 11:38:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>HTTP Basic Authentication &#8211; Accessing Password Protected Servers</title>
		<link>http://iPhoneDeveloperTips.com/networking/handling-url-authentication-challenges-accessing-password-protected-servers.html</link>
		<comments>http://iPhoneDeveloperTips.com/networking/handling-url-authentication-challenges-accessing-password-protected-servers.html#comments</comments>
		<pubDate>Wed, 25 Aug 2010 07:07:13 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6941</guid>
		<description><![CDATA[It&#8217;s not uncommon for a server to require credentials for access. A common example is a password protected directory on a web-server that will prompt for a username and password before allowing access. When using a NSURLRequest object to access a server, the delegate methods of NSURLConnection offer a means to be notified when an [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s not uncommon for a server to require credentials for access. A common example is a password protected directory on a web-server that will prompt for a username and password before allowing access.</p>
<p>When using a <strong>NSURLRequest</strong> object to access a server, the delegate methods of <strong>NSURLConnection</strong> offer a means to be notified when an authentication challenge has been requested.</p>
<p>This post will show one approach for managing URL requests that require authentication. </p>
<h5>Initiate the URL Request</h5>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Begin the authentication process 
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>startAuthentication
<span style="color: #002200;">&#123;</span>
  <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>url <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> URLWithString<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;http://protectedURL.com&quot;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span>urlRequest <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableURLRequest</span> requestWithURL<span style="color: #002200;">:</span>url<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Start the connection request</span>
  <span style="color: #11740a; font-style: italic;">// Assumes NSURLConnection *urlConnection is defined as an</span>
  <span style="color: #11740a; font-style: italic;">// instance variable</span>
   urlConnection <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLConnection</span> alloc<span style="color: #002200;">&#93;</span> initWithRequest<span style="color: #002200;">:</span>urlRequest delegate<span style="color: #002200;">:</span>self<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Answer Authentication Challenge</h5>
<p>If an authentication challenge is returned from the URL request, the method below will be called. To respond to the challange, create a <strong>NSURLCredential</strong> object, setting the username and password. </p>
<p>Notice that we can check to see how many times the challenge/response has failed and present an error message as needed.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Received a server challenge
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection 
      didReceiveAuthenticationChallenge<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLAuthenticationChallenge</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>challenge
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Access has failed two times...</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>challenge previousFailureCount<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>urlConnection release<span style="color: #002200;">&#93;</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;Authentication Error&quot;</span>
                                     message<span style="color: #002200;">:</span><span style="color: #bf1d1a;">&quot;Too many unsuccessul login attempts.&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>;
&nbsp;
    <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>
  <span style="color: #a61390;">else</span> 
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Answer the challenge</span>
    <span style="color: #400080;">NSURLCredential</span> <span style="color: #002200;">*</span>cred <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURLCredential</span> alloc<span style="color: #002200;">&#93;</span> initWithUser<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;admin&quot;</span> password<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;password&quot;</span>
       persistence<span style="color: #002200;">:</span>NSURLCredentialPersistenceForSession<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>challenge sender<span style="color: #002200;">&#93;</span> useCredential<span style="color: #002200;">:</span>cred forAuthenticationChallenge<span style="color: #002200;">:</span>challenge<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* Finished loading URL
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>connectionDidFinishLoading<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Connection success.&quot;</span><span style="color: #002200;">&#41;</span>;  
&nbsp;
  <span style="color: #002200;">&#91;</span>urlConnection release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* URL connection fail
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>connection<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>connection didFailWithError<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSError</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>error
<span style="color: #002200;">&#123;</span>
  NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Connection failure.&quot;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #002200;">&#91;</span>urlConnection release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/networking/handling-url-authentication-challenges-accessing-password-protected-servers.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iOS Open Source : Heads Up Display with MBProgressHUD</title>
		<link>http://iPhoneDeveloperTips.com/ios-open-source/ios-open-source-heads-up-display-with-mbprogresshud.html</link>
		<comments>http://iPhoneDeveloperTips.com/ios-open-source/ios-open-source-heads-up-display-with-mbprogresshud.html#comments</comments>
		<pubDate>Fri, 13 Aug 2010 03:29:25 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[iOS Open Source]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6872</guid>
		<description><![CDATA[When the iPhone SDK was released, just over 2 years ago, open source projects specifically targeting the iPhone OS were few and far between. Over the past year there has been a significant uptick in iPhone/iOS open source projects, and with that, I&#8217;d like to share various projects as I come across them. Should a [...]]]></description>
			<content:encoded><![CDATA[<p>When the iPhone SDK was released, just over 2 years ago, open source projects specifically targeting the iPhone OS were few and far between. Over the past year there has been a significant uptick in iPhone/iOS open source projects, and with that, I&#8217;d like to share various projects as I come across them. Should a good Cocoa library cross my radar, one that is applicable to iOS, I&#8217;ll pass that on as well.<br />
<span id="more-6872"></span></p>
<h5>Heads Up Display &#8211; MBProgressHUD</h5>
<p>The inaugural iOS open source project is MBProgressHUD, you can find source code here on git:  <a href="http://github.com/jdg/MBProgressHUD">MBProgressHUD</a> </p>
<p>For those unfamiliar with HUD, it refers to Heads Up Display, a view that is overlayed on another view, typically to show an activity indicator and/or offer a means to interact with the UI. A good example of an HUD is when playing a movie with the built in movie player. When you tap the screen, the movie player will show a series of controls on the display. Another common example, and one that is pertinent to MBProgressHUD, is showing progress indicators on top of an existing view. For example, when accessing a remote service, to provide feedback to the user you may opt to display a &#8220;Please Wait&#8221; message.</p>
<p>MBProgressHUD offers a number of interesting options from a simple activity indicator to a mode switching display that can show multiple messages, in sequence. See the figures below for a few examples:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/08/hud1.png" /></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/08/hud2.png" /></p>
<p>MBProgressHUD includes a demo project that you compile and run in the simulator to get a feel for how things work. </p>
<h5>Open Source Code / Project Recommendations ?</h5>
<p>If you have written, worked with or otherwise know of an interesting open source project, one that is focused on iOS or Cocoa, feel free to send me a note. Even a simple class/category that is open source is welcome &#8211; for example MBProgressHUD is only two files, an interface (.h) and implementation (.m) . You can reach me at <a href="http://iphonedevelopertips.com/contact">here</a>.</p>
<h5>Licensing and Caveats</h5>
<p>Regarding all open source projects, please review any license agreement that is included with the project for more information about permissions, limitations and warranties.</p>
<p>As you would expect, iPhone Developer Tips offers no warranties of any kind regarding open source projects, use at your own risk.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/ios-open-source/ios-open-source-heads-up-display-with-mbprogresshud.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Obfuscate / Encrypt a String (NSString)</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/obfuscation-encryption-of-string-nsstring.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/obfuscation-encryption-of-string-nsstring.html#comments</comments>
		<pubDate>Thu, 05 Aug 2010 03:46:55 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6829</guid>
		<description><![CDATA[Apple Keychain Services offer a secure means to store sensitive information. Through the keychain, all the hardwork is managed for you to store and retrieve content. As powerful as the keychain services are, I was recently tinkering with some code to see if I could obfuscate content within an application. I had a few strings [...]]]></description>
			<content:encoded><![CDATA[<p>Apple Keychain Services offer a secure means to store sensitive information. Through the keychain, all the hardwork is managed for you to store and retrieve content. As powerful as the keychain services are, I was recently tinkering with some code to see if I could obfuscate content within an application. </p>
<p>I had a few strings defined as constants and I was interested to see if there was a painless way to store the values as obfuscated strings, and when running the application, un-obfuscate the strings and use the same within the application.<br />
<span id="more-6829"></span></p>
<h5>Using Exclusive-Or (XOR) to Obfuscate an NSString</h5>
<p>The basic concept is that for each character in a string, I XOR the value against a key value, replacing the original character with the new XOR&#8217;d character. This will create a string that is unrecognizable from the original. To get the original string back, perform the same XOR operation with the same key. The obfuscate method is shown below.</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: #400080;">NSString</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>obfuscate<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><span style="color: #a61390;">string</span> withKey<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>key
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Create data object from the string</span>
  <span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span>data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #a61390;">string</span> dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Get pointer to data to obfuscate</span>
  <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>dataPtr <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">char</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span>data bytes<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Get pointer to key data</span>
  <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>keyData <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">char</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>key dataUsingEncoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span> bytes<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Points to each char in sequence in the key</span>
  <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>keyPtr <span style="color: #002200;">=</span> keyData;
  <span style="color: #a61390;">int</span> keyIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// For each character in data, xor with current value in key</span>
  <span style="color: #a61390;">for</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">int</span> x <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>; x &lt; <span style="color: #002200;">&#91;</span>data length<span style="color: #002200;">&#93;</span>; x<span style="color: #002200;">++</span><span style="color: #002200;">&#41;</span> 
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Replace current character in data with </span>
    <span style="color: #11740a; font-style: italic;">// current character xor'd with current key value.</span>
    <span style="color: #11740a; font-style: italic;">// Bump each pointer to the next character</span>
    <span style="color: #002200;">*</span>dataPtr <span style="color: #002200;">=</span> <span style="color: #002200;">*</span>dataPtr<span style="color: #002200;">++</span> <span style="color: #002200;">^</span> <span style="color: #002200;">*</span>keyPtr<span style="color: #002200;">++</span>; 
&nbsp;
    <span style="color: #11740a; font-style: italic;">// If at end of key data, reset count and </span>
    <span style="color: #11740a; font-style: italic;">// set key pointer back to start of key value</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">++</span>keyIndex <span style="color: #002200;">==</span> <span style="color: #002200;">&#91;</span>key length<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
      keyIndex <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>, keyPtr <span style="color: #002200;">=</span> keyData;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">return</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> alloc<span style="color: #002200;">&#93;</span> initWithData<span style="color: #002200;">:</span>data encoding<span style="color: #002200;">:</span>NSUTF8StringEncoding<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Test Obfuscation</h5>
<p>Pass in the string to obfuscate as well as a key which will provide the characters to use in the XOR process. To un-obfuscate a string, call the method with the obfuscated string and the same key.</p>
<p>Below is a short test of the obfuscation:</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;iPhoneDeveloperTips&quot;</span>; 
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Input string:%@&quot;</span>, str<span style="color: #002200;">&#41;</span>;		
&nbsp;
<span style="color: #11740a; font-style: italic;">// Obfuscate string</span>
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>obfuscatedStr <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self obfuscate<span style="color: #002200;">:</span>str withKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;+@$&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;Obfuscated string:%@&quot;</span>, obfuscatedStr<span style="color: #002200;">&#41;</span>;	
&nbsp;
<span style="color: #11740a; font-style: italic;">// Run obfuscate again to get the original string</span>
str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>self obfuscate<span style="color: #002200;">:</span>obfuscatedStr withKey<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;+@$&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;Final string:%@&quot;</span>, str<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output will look as follows:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/08/obfuscate.png" /></p>
<p><strong>Note:</strong> The resulting obfuscated string may include non-printable characters, so if you print to the console as shown above, don&#8217;t be surprised if the output contains what looks to be spaces or newline characters.</p>
<h5>Caveat</h5>
<p>For any number of reasons, including the key is stored as part of the application, this approach is by no means a substitution for real encryption. If you have sensitive data you need to protect, use the Keychain services. With that said, there are times when storing a string in a format other than clear text in your application may be of interest.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/obfuscation-encryption-of-string-nsstring.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Voices That Matter iPhone Developers Conference &#8211; Philadelphia PA &#8211; October 16th and 17th, 2010</title>
		<link>http://iPhoneDeveloperTips.com/announcements/voices-that-matter-iphone-developers-conference-philadelphia-pa-october-16th-and-17th-2010.html</link>
		<comments>http://iPhoneDeveloperTips.com/announcements/voices-that-matter-iphone-developers-conference-philadelphia-pa-october-16th-and-17th-2010.html#comments</comments>
		<pubDate>Tue, 03 Aug 2010 02:37:44 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Announcements]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6804</guid>
		<description><![CDATA[Pearson Education just announced the Voices That Matter iPhone Developers Conference to be held October 16-17, 2010 in Philadelphia. As with all previous Voices That Matters iPhone events, you will have an opportunity to learn from industry leaders who literally &#8220;Wrote the Books&#8221; on iOS Development. This two-day conference features a wide range of sessions, [...]]]></description>
			<content:encoded><![CDATA[<p>Pearson Education just announced the <a  target="_blank"  href="http://iphonedevelopertips.com/?wp_ct=31">Voices That Matter iPhone Developers Conference</a> to be held October 16-17, 2010 in Philadelphia.</p>
<p>As with all <a  target="_blank"  href="http://iphonedevelopertips.com/announcements/voices-that-matter-iphone-developers-conference-register-to-win-a-free-pass.html">previous</a> Voices That Matters iPhone events, you will have an opportunity to learn from industry leaders who literally &#8220;Wrote the Books&#8221; on iOS Development. This two-day conference features a wide range of sessions, including:</p>
<p>* Multitasking<br />
* Game design<br />
* Accessibility<br />
* Designing a killer user interface<br />
* Augmented reality<br />
* Unit testing<br />
* Internationalization<br />
* Many other timely and compelling discussions</p>
<p>Lead by industry experts and authors, this is a weekend packed with sessions and interactive discussions, </p>
<p>Enjoy a unique venue that offers an opportunity to network with other developers and mingle with experts including Mike Lee, Aaron Hillegass, Erica Sadun, Stephen Kochan, Rod Strougo, and Christian Hofstader, to name a few.</p>
<h5>Register Early for the Best Prices!</h5>
<p><a  target="_blank"  href="http://www.voicesthatmatter.com/iphonefall2010/register.aspx">Register before September 10th</a> and the price is only $495. Provide priority code PHEM232 when registering to take an additional $100 off and attend for just $395!</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/announcements/voices-that-matter-iphone-developers-conference-philadelphia-pa-october-16th-and-17th-2010.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Capture iPhone Simulator Screenshots &#8211; Revisited &#8211; iPhone Simulator Cropper</title>
		<link>http://iPhoneDeveloperTips.com/tools/capture-screenshots-of-the-iphone-simulator-with-iphone-simulator-cropper.html</link>
		<comments>http://iPhoneDeveloperTips.com/tools/capture-screenshots-of-the-iphone-simulator-with-iphone-simulator-cropper.html#comments</comments>
		<pubDate>Wed, 28 Jul 2010 07:04:13 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6775</guid>
		<description><![CDATA[A colleague on a recent iPhone project was looking for a tool to capture a series of screenshots for an upcoming application demo. In a Google search he first bumped into a post I wrote back in February of 2009: Capture iPhone Simulator Screenshots – Open Source Screen Capture Tool, a nice Mac tool for [...]]]></description>
			<content:encoded><![CDATA[<p>A colleague on a recent iPhone project was looking for a tool to capture a series of screenshots for an upcoming application demo. In a Google search he first bumped into a post I wrote back in February of 2009: <a href="http://iphonedevelopertips.com/general/open-source-screen-capture-tool.html">Capture iPhone Simulator Screenshots – Open Source Screen Capture Tool</a>, a nice Mac tool for taking screenshots. As luck would have it, he also found another screen capture tool, one focused solely on iPhone screenshots: <a  target="_blank"   href="http://www.curioustimes.de/iphonesimulatorcropper/">iPhone-Simulator Cropper</a>.<br />
<span id="more-6775"></span></p>
<h5>iPhone-Simulator Cropper</h5>
<p>This tool works by capturing the screen of the simulator runnning on your system. One really slick feature is the option to create captured images in two primary formats &#8211; first, a format suitable for upload to iTunes for your application screenshots &#8211; second, capturing a screenshot that is suitable for display on a website.</p>
<p>The following images show the iPhone-Simulator Cropper application, as well as two sample images captured:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/cropper0.jpg" /></p>
<p>iTunes Connect / App Store:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/cropper1.jpg" /></p>
<p>Website (iPhone Device from Apple Marketing):</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/cropper2.jpg" /></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/tools/capture-screenshots-of-the-iphone-simulator-with-iphone-simulator-cropper.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Truncate a String and Append an Ellipsis, Respecting the Font Size</title>
		<link>http://iPhoneDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html</link>
		<comments>http://iPhoneDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html#comments</comments>
		<pubDate>Mon, 26 Jul 2010 11:52:06 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Cocoa]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6737</guid>
		<description><![CDATA[A number of the UI related controls will automatically truncate and append ellipsis with no effort required on your part. For example, with UILabel you can specify the linebreak mode to indicate how you would like the system to manage wrapping and truncating the label text. However, they are times when having a method to [...]]]></description>
			<content:encoded><![CDATA[<p>A number of the UI related controls will automatically truncate and append ellipsis with no effort required on your part. For example, with UILabel you can specify the linebreak mode to indicate how you would like the system to manage wrapping and truncating the label text.</p>
<p>However, they are times when having a method to truncate a string without using a UI control would be handy. In this post I&#8217;ll create a category which will add a method to the  <strong>NSString</strong> class to do just that. You can read more about working with categories in this post: <a  target="_blank"  href="http://iphonedevelopertips.com/objective-c/categories.html">Introduction to Categories</a>.<br />
<span id="more-6737"></span></p>
<h5>NSString Category</h5>
<p>Start by creating a new interface file with the name: <strong>NSString+TruncateToWidth.h</strong> &#8211; appending the method name to the class is a common naming convention when working with categories.</p>
<p>The code below defines a method that will be available to all <strong>NSString</strong> objects. The method will truncate a string to a specific width, using the font size to properly determine the string size before truncating:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">&#40;</span>TruncateToWidth<span style="color: #002200;">&#41;</span>
&nbsp;
<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>stringByTruncatingToWidth<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>width withFont<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIFont <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>font;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<h5>Code to Truncate NSString</h5>
<p>The implementation of the <strong>stringByTruncatingToWidth</strong> method follows, add this code to the <strong>NSString+TruncateToWidth.m</strong> file.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;NSString+TruncateToWidth.h&quot;</span>
&nbsp;
<span style="color: #6e371a;">#define ellipsis @&quot;…&quot;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> <span style="color: #400080;">NSString</span> <span style="color: #002200;">&#40;</span>TruncateToWidth<span style="color: #002200;">&#41;</span>
&nbsp;
<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>stringByTruncatingToWidth<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>CGFloat<span style="color: #002200;">&#41;</span>width withFont<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span>UIFont <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>font
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Create copy that will be the returned result</span>
  <span style="color: #400080;">NSMutableString</span> <span style="color: #002200;">*</span>truncatedString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self mutableCopy<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Make sure string is longer than requested width</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>self sizeWithFont<span style="color: #002200;">:</span>font<span style="color: #002200;">&#93;</span>.width &gt; width<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Accommodate for ellipsis we'll tack on the end</span>
    width <span style="color: #002200;">-=</span> <span style="color: #002200;">&#91;</span>ellipsis sizeWithFont<span style="color: #002200;">:</span>font<span style="color: #002200;">&#93;</span>.width;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Get range for last character in string</span>
    <span style="color: #a61390;">NSRange</span> range <span style="color: #002200;">=</span> <span style="color: #002200;">&#123;</span>truncatedString.length <span style="color: #002200;">-</span> <span style="color: #2400d9;">1</span>, <span style="color: #2400d9;">1</span><span style="color: #002200;">&#125;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Loop, deleting characters until string fits within width</span>
    <span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>truncatedString sizeWithFont<span style="color: #002200;">:</span>font<span style="color: #002200;">&#93;</span>.width &gt; width<span style="color: #002200;">&#41;</span> 
    <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Delete character at end</span>
      <span style="color: #002200;">&#91;</span>truncatedString deleteCharactersInRange<span style="color: #002200;">:</span>range<span style="color: #002200;">&#93;</span>;
&nbsp;
      <span style="color: #11740a; font-style: italic;">// Move back another character</span>
      range.location<span style="color: #002200;">--</span>;
    <span style="color: #002200;">&#125;</span>
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Append ellipsis</span>
    <span style="color: #002200;">&#91;</span>truncatedString replaceCharactersInRange<span style="color: #002200;">:</span>range withString<span style="color: #002200;">:</span>ellipsis<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #a61390;">return</span> truncatedString;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>Notice that the desired final width of the string is passed in, as well as the font that will be used when calculating the size (width) of the string. The process is quite simple: change the incoming width to accommodate an ellipsis on the end of the string &#8211; loop through the string removing characters from the end until the desired width is reached &#8211; append an ellipsis onto the string.</p>
<h5>Calling the Truncate NSString Method</h5>
<p>You can now call the method <strong>stringByTruncatingToWidth:withFont:</strong> on any NSString object:</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;Lorem ipsum dolor sit amet, consectetuer adipiscing&quot;</span>;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Adjust the width parameter to change the final string width</span>
str <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>str stringByTruncatingToWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">170</span> withFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont systemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">15</span><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;%@&quot;</span>, str<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The output from above would look as follows:</p>
<p><strong>Lorem ipsum dolor sit…</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cocoa/truncate-an-nsstring-and-append-an-ellipsis-respecting-the-font-size.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Download and Install Older Versions of Xcode (Xcode Previous Releases)</title>
		<link>http://iPhoneDeveloperTips.com/xcode/download-and-install-older-versions-of-xcode-xcode-previous-releases.html</link>
		<comments>http://iPhoneDeveloperTips.com/xcode/download-and-install-older-versions-of-xcode-xcode-previous-releases.html#comments</comments>
		<pubDate>Fri, 23 Jul 2010 01:02:07 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Xcode]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6710</guid>
		<description><![CDATA[As a general rule of thumb, it&#8217;s best to stick with the latest release of Xcode. That said, there are times when running an older release can be a good thing. You can download older versions of Xcode by logging into Apple Developer Connection &#8211; Downloads and ADC Program Assests (http://connect.apple.com) &#8211; You will need [...]]]></description>
			<content:encoded><![CDATA[<p>As a general rule of thumb, it&#8217;s best to stick with the latest release of Xcode. That said, there are times when running an older release can be a good thing. </p>
<p>You can download older versions of Xcode by logging into <a href="http://connect.apple.com">Apple Developer Connection &#8211; Downloads and ADC Program Assests</a> (<a href="http://connect.apple.com">http://connect.apple.com</a>) &#8211; You will need to be a member of the developer program to login.<br />
<span id="more-6710"></span></p>
<p>Once on the site, from the <strong>Downloads</strong> section, choose <strong>Developer Tools</strong> &#8211; you will be shown with a list of all Xcode versions, back to 1.0 released in October of 2004.</p>
<h5>Install Multiple Versions of Xcode</h5>
<p>To keep from overwriting an existing installation, you can specify the path for Xcode during the install process. For example, the default location is /Developer, to install an older version you could instruct Xcode to install into a simliar name such as /Developer-3.1</p>
<p>To change the install path, click on the Location header in the install dialog:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/08/xcodeInstall-1.png" /></p>
<p>Choose a new path for the Essentials Package:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/08/xcodeInstall-2.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/xcode/download-and-install-older-versions-of-xcode-xcode-previous-releases.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Play Movies from the Application Bundle or from a Remote Server (URL)</title>
		<link>http://iPhoneDeveloperTips.com/video/how-to-play-movies-from-application-bundle-or-from-a-remote-server-url.html</link>
		<comments>http://iPhoneDeveloperTips.com/video/how-to-play-movies-from-application-bundle-or-from-a-remote-server-url.html#comments</comments>
		<pubDate>Wed, 21 Jul 2010 00:51:45 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6691</guid>
		<description><![CDATA[In the previous two posts I wrote about how to create a movie player that would work across OS versions, as well as how to play a movie in portrait mode (on 3.2 and above) : Display iPhone Movies in Portrait Mode and Getting MPMoviePlayerController to Cooperate with iOS4, 3.2 (iPad) and Earlier Versions of [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous two posts I wrote about how to create a movie player that would work across OS versions, as well as how to play a movie in portrait mode (on 3.2 and above) : <a href="http://iphonedevelopertips.com/video/display-iphone-movies-in-portrait-mode-updated.html">Display iPhone Movies in Portrait Mode</a> and <a  target="_blank"  href="http://iphonedevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html">Getting MPMoviePlayerController to Cooperate with iOS4, 3.2 (iPad) and Earlier Versions of iPhone SDK</a> </p>
<p>For both posts, in order to create a working example that one could download and run as is, the code examples referenced a movie that I included in the application bundle. Although handy, this prompted a number of questions about how to play a movie from a remote resource.</p>
<p>I&#8217;ve updated the original examples for both posts, with an option to play movies from the application bundle or from a remote server.<br />
<span id="more-6691"></span></p>
<h5>Play Movie from Server</h5>
<p>The first change I made was in <strong>CustomMoviePlayerViewController.m</strong>, where I added a method to initialize the movie player with a NSURL object:</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;">id</span><span style="color: #002200;">&#41;</span>initWithURL<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>URL
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    movieURL <span style="color: #002200;">=</span> URL;
    <span style="color: #002200;">&#91;</span>movieURL retain<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithPath<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>moviePath
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    movieURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>moviePath<span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>movieURL retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For both methods, the end result is an initialized NSURL object.</p>
<p>The other code change is in <strong>TestViewController.m</strong> where I updated the <strong>loadMoviePlayer</strong> method to support calling either of the above methods:</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
</pre></td><td 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>loadMoviePlayer
<span style="color: #002200;">&#123;</span>  
<span style="color: #11740a; font-style: italic;">/*
  // Play movie from app bundle
  NSString *path = [[NSBundle mainBundle] pathForResource:@&quot;Movie-1&quot; ofType:@&quot;mp4&quot; inDirectory:nil];
  moviePlayer = [[[CustomMoviePlayerViewController alloc] initWithPath:path] autorelease];
*/</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Play movie from URL</span>
  <span style="color: #400080;">NSURL</span> <span style="color: #002200;">*</span>movieURL <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://someurlsomewhere.com/movie.mp4&quot;</span><span style="color: #002200;">&#93;</span>;
  moviePlayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CustomMoviePlayerViewController alloc<span style="color: #002200;">&#93;</span> initWithURL<span style="color: #002200;">:</span>movieURL<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Show the movie player as modal</span>
  <span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>moviePlayer 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;">// Prep and play the movie</span>
  <span style="color: #002200;">&#91;</span>moviePlayer readyPlayer<span style="color: #002200;">&#93;</span>;    
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>Comment in/out the relevant code in lines 4 &#8211; 11 to swap between playing from a file in the bundle and a movie accessible on a remote server.</p>
<h5>Download Movie Player Source Code</h5>
<p><a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/MoviePlayer-ios4-Landscape.zip">MoviePlayer in Landscape Mode</a></p>
<p><a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/MoviePlayer-ios4-Portrait.zip">MoviePlayer in Portrait Mode (requires 3.2 OS and greater)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/video/how-to-play-movies-from-application-bundle-or-from-a-remote-server-url.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Display iPhone Movies in Portrait Mode (Updated)</title>
		<link>http://iPhoneDeveloperTips.com/video/display-iphone-movies-in-portrait-mode-updated.html</link>
		<comments>http://iPhoneDeveloperTips.com/video/display-iphone-movies-in-portrait-mode-updated.html#comments</comments>
		<pubDate>Wed, 14 Jul 2010 00:36:36 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6652</guid>
		<description><![CDATA[In the previous post, Getting MPMoviePlayerController to Cooperate with iOS4 and Earlier Versions of iPhone SDK, I demonstrated how to work with the most recent changes in iPhone OS 3.2 and 4.0 MPMoviePlayerController to enable one application to play movies regardless of the OS version on the device. In this post I want to show [...]]]></description>
			<content:encoded><![CDATA[<p>In the previous post, <a  target="_blank"  href="http://iphonedevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html">Getting MPMoviePlayerController to Cooperate with iOS4 and Earlier Versions of iPhone SDK</a>, I demonstrated how to work with the most recent changes in iPhone OS 3.2 and 4.0 <strong>MPMoviePlayerController</strong> to enable one application to play movies regardless of the OS version on the device.</p>
<p>In this post I want to show you a few minor changes you can make to the previous app to display a movie in portrait mode, something that was not possible in earlier (pre 3.2) OS versions. With that said, back in January of 2010 I did cover a means to create the illusion that a movie was in portrait mode, if this piques your interest, have a look here: <a  target="_blank"  href="http://iphonedevelopertips.com/video/play-movies-in-portrait-mode-with-mpmovieplayercontroller-using-public-apis.html">Play iPhone Movies in Portrait Mode with MPMoviePlayerController using Public API’s</a><br />
<span id="more-6652"></span></p>
<p>The figure below shows the same movie as played back in landscape and portrait:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/portraitMode.png" /></p>
<h5>Play Movies in Portrait Mode</h5>
<p>The code below is from the previous post and shows how to configure the movie player view to display in landscape:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">MPMoviePlayerController <span style="color: #002200;">*</span>mp <span style="color: #002200;">=</span>  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MPMoviePlayerController alloc<span style="color: #002200;">&#93;</span>
     initWithContentURL<span style="color: #002200;">:</span>movieURL<span style="color: #002200;">&#93;</span>;
&nbsp;
...
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> moviePlayerLoadStateChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><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;">// Rotate the view for landscape playback</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setBounds<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;">480</span>, <span style="color: #2400d9;">320</span><span style="color: #002200;">&#41;</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> setCenter<span style="color: #002200;">:</span>CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">240</span><span style="color: #002200;">&#41;</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> setTransform<span style="color: #002200;">:</span>CGAffineTransformMakeRotation<span style="color: #002200;">&#40;</span>M_PI <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set frame of movieplayer</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>mp view<span style="color: #002200;">&#93;</span> 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;">480</span>, <span style="color: #2400d9;">320</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>To playback a movie in portrait mode is as easy as removing the code for the rotation and changing the frame of the movieplayer. I&#8217;ve also added a call to scale the movie so it fills the screen, this will maintain the aspect ratio.</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> moviePlayerLoadStateChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><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;">// Rotate the view for landscape playback</span>
  <span style="color: #11740a; font-style: italic;">//	  [[self view] setBounds:CGRectMake(0, 0, 480, 320)];</span>
  <span style="color: #11740a; font-style: italic;">//		[[self view] setCenter:CGPointMake(160, 240)];</span>
  <span style="color: #11740a; font-style: italic;">//		[[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)]; </span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set frame of movieplayer</span>
  <span style="color: #11740a; font-style: italic;">// [[mp view] setFrame:CGRectMake(0, 0, 480, 320)];</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>mp view<span style="color: #002200;">&#93;</span> setFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">0</span>, <span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">320</span>, <span style="color: #2400d9;">160</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Scale uniformly, maintaining aspect ratio</span>
  <span style="color: #002200;">&#91;</span>mp setScalingMode<span style="color: #002200;">:</span>MPMovieScalingModeAspectFill<span style="color: #002200;">&#93;</span>;
&nbsp;
  ...
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>This approach works on 3.2 (iPad) as well as 4.0 and greater. Needless to say, given the large display on the iPad this is a nice alternative as the user can watch videos without rotating the device.</p>
<h5>Source Code</h5>
<p><a href="http://iphonedevelopertips.com/wp-content/uploads/2010/07/MoviePlayer-Portrait-Mode-ios4.zip">Download the source code</a> to build an iPhone app that displays movies in portrait mode.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/video/display-iphone-movies-in-portrait-mode-updated.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Getting MPMoviePlayerController to Work with iOS4, 3.2 (iPad) and Earlier Versions of iPhone SDK</title>
		<link>http://iPhoneDeveloperTips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html</link>
		<comments>http://iPhoneDeveloperTips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html#comments</comments>
		<pubDate>Fri, 09 Jul 2010 02:52:29 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Video]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=6579</guid>
		<description><![CDATA[The API and overall approach for working with MPMoviePlayerController has changed just enough in the 3.2 SDK (iPad) and iOS4 SDK to cause working applications in earlier releases to be problematic when running on later SDKs. In this post I&#8217;ll walk through a short example of a movie player application that will work with 3.x, [...]]]></description>
			<content:encoded><![CDATA[<p>The API and overall approach for working with <strong>MPMoviePlayerController</strong> has changed just enough in the  3.2 SDK (iPad) and iOS4 SDK to cause working applications in earlier releases to be problematic when running on later SDKs. In this post I&#8217;ll walk through a short example of a movie player application that will work with 3.x, 3.2 and 4.0 SDKs. </p>
<p>In the next post, <a href="http://iphonedevelopertips.com/video/display-iphone-movies-in-portrait-mode-updated.html">Display iPhone Movies in Portrait Mode</a>, I&#8217;ll show how to create a movie player that runs in a view that is <em>not</em> fullscreen as well as how to show a few lines of code to display a movie in portrait mode &#8211; the one caveat here is that both of the tips in the second post will apply only to OS versions 3.2 and up.<br />
<span id="more-6579"></span></p>
<h5>What&#8217;s Changed with MPMoviePlayerController in 3.2 and 4.0?</h5>
<p>There are any number of changes in the <strong>MPMoviePlayerController</strong>, however, a few standout as potential roadblocks to getting a movie to display:</p>
<p>- In 3.1 and earlier versions, <strong>MPMoviePlayerController</strong> was full-screen only. Playing a movie was straight-forward, create a player, initialize with a file (path or URL) and call a method to start playback &#8211; the rest was taken care of for you.</p>
<p>- With 3.2 and later, movies can playback in fullscreen or a custom view, as well as portrait or landscape.</p>
<p>- The notification <strong>MPMoviePlayerContentPreloadDidFinishNotification</strong>  has been deprecated. This notification was used in earlier versions to notify observers that a movie was loaded and ready to play.</p>
<h5>The Movie Application</h5>
<p>The application in this post is quite simple, it consists of a view controller with nothing more than a button to start playback and a second view controller to manage a <strong>MPMoviePlayerController</strong> and the <strong>NSURL</strong> of the movie. The two views are shown in the figures below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/movie1.png" /></p>
<h5>Primary View Controller</h5>
<p>The interface definition for the primary view is shown below:</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>
&nbsp;
<span style="color: #a61390;">@class</span> CustomMoviePlayerViewController;
&nbsp;
<span style="color: #a61390;">@interface</span> TestViewController <span style="color: #002200;">:</span> UIViewController
<span style="color: #002200;">&#123;</span>
  CustomMoviePlayerViewController  <span style="color: #002200;">*</span>moviePlayer;
  UIButton  <span style="color: #002200;">*</span>playButton;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p><strong>CustomMoviePlayerViewController</strong> is the controller for managing the movie, we&#8217;ll look at that code in a moment. </p>
<p>In the code below we create the view, add a play button, create a method for processing a button press event and within <strong>loadMoviePlayer</strong>, we get a reference to the movie file and create an instance of the <strong>CustomMoviePlayerViewController</strong>, which will load and play the movie.</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: #11740a; font-style: italic;">// Setup the view</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 grayColor<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> setUserInteractionEnabled<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;">// Add play button </span>
  playButton <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIButton alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">53</span>, <span style="color: #2400d9;">212</span>, <span style="color: #2400d9;">214</span>, <span style="color: #2400d9;">36</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;    
  <span style="color: #002200;">&#91;</span>playButton setBackgroundImage<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;playButton.png&quot;</span><span style="color: #002200;">&#93;</span> forState<span style="color: #002200;">:</span>UIControlStateNormal<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>playButton 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>playButton<span style="color: #002200;">&#93;</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>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;">// If pressed, play movie</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>button <span style="color: #002200;">==</span> playButton<span style="color: #002200;">&#41;</span>
    <span style="color: #002200;">&#91;</span>self loadMoviePlayer<span style="color: #002200;">&#93;</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>loadMoviePlayer
<span style="color: #002200;">&#123;</span>  
  <span style="color: #11740a; font-style: italic;">// Play movie from the bundle</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>path <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSBundle</span> mainBundle<span style="color: #002200;">&#93;</span> pathForResource<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Movie-1&quot;</span> ofType<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;mp4&quot;</span> inDirectory<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Create custom movie player   </span>
  moviePlayer <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>CustomMoviePlayerViewController alloc<span style="color: #002200;">&#93;</span> initWithPath<span style="color: #002200;">:</span>path<span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Show the movie player as modal</span>
  <span style="color: #002200;">&#91;</span>self presentModalViewController<span style="color: #002200;">:</span>moviePlayer 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;">// Prep and play the movie</span>
  <span style="color: #002200;">&#91;</span>moviePlayer readyPlayer<span style="color: #002200;">&#93;</span>;    
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Notice that the movie for this example is loaded from the application bundle. Also, once the custom movie player is created, the view is shown as a modal view.</p>
<h5>Custom Movie Player View Controller</h5>
<p>The interface definition for the movie view controller is below:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> CustomMoviePlayerViewController <span style="color: #002200;">:</span> UIViewController 
<span style="color: #002200;">&#123;</span>
  MPMoviePlayerController <span style="color: #002200;">*</span>mp;
  <span style="color: #400080;">NSURL</span>  <span style="color: #002200;">*</span>movieURL;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">id</span><span style="color: #002200;">&#41;</span>initWithPath<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>moviePath;
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>readyPlayer;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The initialization code is where the movie player and associated URL are created. The primary goal of the initialization is to create the <strong>NSURL</strong> needed by the movie player.</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;">id</span><span style="color: #002200;">&#41;</span>initWithPath<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>moviePath
<span style="color: #002200;">&#123;</span>
  <span style="color: #11740a; font-style: italic;">// Initialize and create movie URL</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>self <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>super init<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    movieURL <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSURL</span> fileURLWithPath<span style="color: #002200;">:</span>moviePath<span style="color: #002200;">&#93;</span>;    
    <span style="color: #002200;">&#91;</span>movieURL retain<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">return</span> self;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The code to create the player and setup the notifications is where we start to deal with the differences in how the movie player works on various OS versions. Notice below the call to <strong>respondsToSelector</strong>, this is the Apple recommended way to check for feature availability, versus looking for a specific OS version. </p>
<p>For devices running 3.2 and above, the movie player controller has a method named <strong>loadstate</strong>, if this exists we can access a few additional methods to set the player to fullscreen as well as request the movie to begin preloading.  </p>
<p>Equally important is the distinction of which notification to setup &#8211; see lines 16 and 24.</p>
<p>You can read more about managing different OS version in this post <a  target="_blank"  href="http://iphonedevelopertips.com/xcode/base-sdk-and-iphone-os-deployment-target-developing-apps-with-the-4-x-sdk-deploying-to-3-x-devices.html">Developing iPhone Apps with iOS4 SDK, Deploying to 3.x Devices</a></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
</pre></td><td 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> readyPlayer
<span style="color: #002200;">&#123;</span>
   mp <span style="color: #002200;">=</span>  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>MPMoviePlayerController alloc<span style="color: #002200;">&#93;</span> initWithContentURL<span style="color: #002200;">:</span>movieURL<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// For 3.2 devices and above</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>mp respondsToSelector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>loadState<span style="color: #002200;">&#41;</span><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;">// Set movie player layout</span>
    <span style="color: #002200;">&#91;</span>mp setControlStyle<span style="color: #002200;">:</span>MPMovieControlStyleFullscreen<span style="color: #002200;">&#93;</span>;
    <span style="color: #002200;">&#91;</span>mp setFullscreen<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;">// May help to reduce latency</span>
    <span style="color: #002200;">&#91;</span>mp prepareToPlay<span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Register that the load state changed (movie is ready)</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moviePlayerLoadStateChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
        name<span style="color: #002200;">:</span>MPMoviePlayerLoadStateDidChangeNotification 
        object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>  
  <span style="color: #a61390;">else</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Register to receive a notification when the movie is in memory and ready to play.</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moviePreloadDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
        name<span style="color: #002200;">:</span>MPMoviePlayerContentPreloadDidFinishNotification 
        object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Register to receive a notification when the movie has finished playing. </span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> addObserver<span style="color: #002200;">:</span>self 
        selector<span style="color: #002200;">:</span><span style="color: #a61390;">@selector</span><span style="color: #002200;">&#40;</span>moviePlayBackDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
        name<span style="color: #002200;">:</span>MPMoviePlayerPlaybackDidFinishNotification 
        object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The next two chunks of code are for each of the selectors, one for each notification. The code below is for earlier OS versions &#8211; nothing more than removing the notification and asking the movie to play.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* For 3.1.x devices
* For 3.2 and 4.x see moviePlayerLoadStateChanged: 
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> moviePreloadDidFinish<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><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;">// Remove observer</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> 	defaultCenter<span style="color: #002200;">&#93;</span> 
    removeObserver<span style="color: #002200;">:</span>self
    name<span style="color: #002200;">:</span>MPMoviePlayerContentPreloadDidFinishNotification
    object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Play the movie</span>
   <span style="color: #002200;">&#91;</span>mp play<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>For the notification generated in 3.2 and above, there are a few more details to tend to:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">/*---------------------------------------------------------------------------
* For 3.2 and 4.x devices
* For 3.1.x devices see moviePreloadDidFinish:
*--------------------------------------------------------------------------*/</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span> moviePlayerLoadStateChanged<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotification</span><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;">// Unless state is unknown, start playback</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>mp loadState<span style="color: #002200;">&#93;</span> <span style="color: #002200;">!=</span> MPMovieLoadStateUnknown<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Remove observer</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSNotificationCenter</span> defaultCenter<span style="color: #002200;">&#93;</span> 
      removeObserver<span style="color: #002200;">:</span>self
      name<span style="color: #002200;">:</span>MPMoviePlayerLoadStateDidChangeNotification 
      object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// When tapping movie, status bar will appear, it shows up</span>
    <span style="color: #11740a; font-style: italic;">// in portrait mode by default. Set orientation to landscape</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIApplication sharedApplication<span style="color: #002200;">&#93;</span> setStatusBarOrientation<span style="color: #002200;">:</span>UIInterfaceOrientationLandscapeRight animated<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Rotate the view for landscape playback</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>self view<span style="color: #002200;">&#93;</span> setBounds<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;">480</span>, <span style="color: #2400d9;">320</span><span style="color: #002200;">&#41;</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> setCenter<span style="color: #002200;">:</span>CGPointMake<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">160</span>, <span style="color: #2400d9;">240</span><span style="color: #002200;">&#41;</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> setTransform<span style="color: #002200;">:</span>CGAffineTransformMakeRotation<span style="color: #002200;">&#40;</span>M_PI <span style="color: #002200;">/</span> <span style="color: #2400d9;">2</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>; 
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Set frame of movie player</span>
    <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>mp view<span style="color: #002200;">&#93;</span> 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;">480</span>, <span style="color: #2400d9;">320</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Add movie player as subview</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><span style="color: #002200;">&#91;</span>mp view<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;   
&nbsp;
    <span style="color: #11740a; font-style: italic;">// Play the movie</span>
    <span style="color: #002200;">&#91;</span>mp play<span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>Beyond removing the notification, we also adjust the status bar, rotate the view, set the frame of the movie player, add the movie player as a subview in the view controller and wrap it all up by asking the movie to play.</p>
<p><strong>Note:</strong> MPMoviePlayerViewController is also an option over creating your own view controller as I&#8217;ve done here.</p>
<h5>Source Code</h5>
<p>The easiest way to see all this working is to <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2010/07/MoviePlayer-ios4.zip">download the source code</a> and step through the code in the debugger.</p>
]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html/feed</wfw:commentRss>
		<slash:comments>47</slash:comments>
		</item>
	</channel>
</rss>
