<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iOS Developer Tips Blog &#187; Device</title>
	<atom:link href="http://iPhoneDeveloperTips.com/category/device/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Wed, 08 Feb 2012 13:40:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Determine MAC Address</title>
		<link>http://iPhoneDeveloperTips.com/device/determine-mac-address.html</link>
		<comments>http://iPhoneDeveloperTips.com/device/determine-mac-address.html#comments</comments>
		<pubDate>Mon, 07 Nov 2011 11:11:10 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Device]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=10557</guid>
		<description><![CDATA[The MAC (Media Access Control) address is an identifier that is associated with a network adapter and uniquely identifies a device on a network. A MAC address consists of 12 hexadecimal numbers, typically formatted as follows XX:XX:XX:YY:YY:YY The XX values in a MAC address identify the manufacturer, the YY values are the serial number assigned [...]]]></description>
			<content:encoded><![CDATA[<p>The MAC (Media Access Control) address is an identifier that is associated with a network adapter and uniquely identifies a device on a network. A MAC address consists of 12 hexadecimal numbers, typically formatted as follows</p>
<p>XX:XX:XX:YY:YY:YY</p>
<p>The XX values in a MAC address identify the manufacturer, the YY values are the serial number assigned to the network adapter.</p>
<p>The MAC address can be useful if you need a way to uniquely identify a device &#8211; this can be used as a substitute for the UDID value that is now deprecated in iOS 5 and greater.<br />
<span id="more-10557"></span></p>
<p>The code below shows how to get the MAC address on an iOS device:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#include &lt;sys/socket.h&gt;</span>
<span style="color: #6e371a;">#include &lt;sys/sysctl.h&gt;</span>
<span style="color: #6e371a;">#include &lt;net/if.h&gt;</span>
<span style="color: #6e371a;">#include &lt;net/if_dl.h&gt;</span>
&nbsp;
...
&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>getMacAddress
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">int</span>                 mgmtInfoBase<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">6</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">char</span>                <span style="color: #002200;">*</span>msgBuffer <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
  <span style="color: #a61390;">size_t</span>              length;
  <span style="color: #a61390;">unsigned</span> <span style="color: #a61390;">char</span>       macAddress<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">6</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #a61390;">struct</span> if_msghdr    <span style="color: #002200;">*</span>interfaceMsgStruct;
  <span style="color: #a61390;">struct</span> sockaddr_dl  <span style="color: #002200;">*</span>socketStruct;
  <span style="color: #400080;">NSString</span>            <span style="color: #002200;">*</span>errorFlag <span style="color: #002200;">=</span> <span style="color: #a61390;">NULL</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Setup the management Information Base (mib)</span>
  mgmtInfoBase<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> CTL_NET;        <span style="color: #11740a; font-style: italic;">// Request network subsystem</span>
  mgmtInfoBase<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> AF_ROUTE;       <span style="color: #11740a; font-style: italic;">// Routing table info</span>
  mgmtInfoBase<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> <span style="color: #2400d9;">0</span>;              
  mgmtInfoBase<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> AF_LINK;        <span style="color: #11740a; font-style: italic;">// Request link layer information</span>
  mgmtInfoBase<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> NET_RT_IFLIST;  <span style="color: #11740a; font-style: italic;">// Request all configured interfaces</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// With all configured interfaces requested, get handle index</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>mgmtInfoBase<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">5</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">=</span> if_nametoindex<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;en0&quot;</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> 
    errorFlag <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;if_nametoindex failure&quot;</span>;
  <span style="color: #a61390;">else</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #11740a; font-style: italic;">// Get the size of the data available (store in len)</span>
    <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sysctl<span style="color: #002200;">&#40;</span>mgmtInfoBase, <span style="color: #2400d9;">6</span>, <span style="color: #a61390;">NULL</span>, <span style="color: #002200;">&amp;</span>length, <span style="color: #a61390;">NULL</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> &lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> 
      errorFlag <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;sysctl mgmtInfoBase failure&quot;</span>;
    <span style="color: #a61390;">else</span>
    <span style="color: #002200;">&#123;</span>
      <span style="color: #11740a; font-style: italic;">// Alloc memory based on above call</span>
      <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#40;</span>msgBuffer <span style="color: #002200;">=</span> <span style="color: #a61390;">malloc</span><span style="color: #002200;">&#40;</span>length<span style="color: #002200;">&#41;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>
        errorFlag <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;buffer allocation failure&quot;</span>;
      <span style="color: #a61390;">else</span>
      <span style="color: #002200;">&#123;</span>
        <span style="color: #11740a; font-style: italic;">// Get system information, store in buffer</span>
        <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>sysctl<span style="color: #002200;">&#40;</span>mgmtInfoBase, <span style="color: #2400d9;">6</span>, msgBuffer, <span style="color: #002200;">&amp;</span>length, <span style="color: #a61390;">NULL</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> &lt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>
          errorFlag <span style="color: #002200;">=</span> <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;sysctl msgBuffer failure&quot;</span>;
      <span style="color: #002200;">&#125;</span>
    <span style="color: #002200;">&#125;</span>
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Befor going any further...</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>errorFlag <span style="color: #002200;">!=</span> <span style="color: #a61390;">NULL</span><span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Error: %@&quot;</span>, errorFlag<span style="color: #002200;">&#41;</span>;
    <span style="color: #a61390;">return</span> errorFlag;
  <span style="color: #002200;">&#125;</span>
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Map msgbuffer to interface message structure</span>
  interfaceMsgStruct <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> if_msghdr <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> msgBuffer;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Map to link-level socket structure</span>
  socketStruct <span style="color: #002200;">=</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">struct</span> sockaddr_dl <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#40;</span>interfaceMsgStruct <span style="color: #002200;">+</span> <span style="color: #2400d9;">1</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Copy link layer address data in socket structure to an array</span>
  <span style="color: #a61390;">memcpy</span><span style="color: #002200;">&#40;</span><span style="color: #002200;">&amp;</span>macAddress, socketStruct<span style="color: #002200;">-</span>&gt;sdl_data <span style="color: #002200;">+</span> socketStruct<span style="color: #002200;">-</span>&gt;sdl_nlen, <span style="color: #2400d9;">6</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Read from char array into a string object, into traditional Mac address format</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>macAddressString <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;%02X:%02X:%02X:%02X:%02X:%02X&quot;</span>, 
                                macAddress<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>, macAddress<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">1</span><span style="color: #002200;">&#93;</span>, macAddress<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">2</span><span style="color: #002200;">&#93;</span>, 
                                macAddress<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">3</span><span style="color: #002200;">&#93;</span>, macAddress<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">4</span><span style="color: #002200;">&#93;</span>, macAddress<span style="color: #002200;">&#91;</span><span style="color: #2400d9;">5</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;Mac Address: %@&quot;</span>, macAddressString<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Release the buffer memory</span>
  <span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>msgBuffer<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #a61390;">return</span> macAddressString;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The output will look as follows: <strong>Mac Address: E0:F8:47:C0:E3:C9</strong></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/device/determine-mac-address.html/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Using the Proximity Sensor</title>
		<link>http://iPhoneDeveloperTips.com/device/using-the-proximity-sensor.html</link>
		<comments>http://iPhoneDeveloperTips.com/device/using-the-proximity-sensor.html#comments</comments>
		<pubDate>Thu, 02 Jun 2011 08:10:48 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Device]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=8550</guid>
		<description><![CDATA[The proximity sensor on the iPhone detects when the device is close to your face (or otherwise covered). There aren&#8217;t many times when using the sensor is of value, however, the Google Voice Search application has put this to good use as a means to trigger voice recording for a search request. If you have [...]]]></description>
			<content:encoded><![CDATA[<p>The proximity sensor on the iPhone detects when the device is close to your face (or otherwise covered). There aren&#8217;t many times when using the sensor is of value, however, the Google Voice Search application has put this to good use as a means to trigger voice recording for a search request. If you have an interest in doing something similar, read on.<br />
<span id="more-8550"></span></p>
<h5>Proximity Sensor Monitoring</h5>
<p>It all begins by enabling proximity monitoring, this is followed by setting up a notification request to call a method when the proximity state changes:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Enabled monitoring of the sensor</span>
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> setProximityMonitoringEnabled<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;">// Set up an observer for proximity changes</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>sensorStateChange<span style="color: #002200;">:</span><span style="color: #002200;">&#41;</span> 
	name<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;UIDeviceProximityStateDidChangeNotification&quot;</span> object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>The method below will be called when the sensor state is updated, a message is printed to the debug console based on the sensor proximity.</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>sensorStateChange<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSNotificationCenter</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>notification
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> proximityState<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span>
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Device is close to user.&quot;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #a61390;">else</span> 
    NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Device is ~not~ closer to user.&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Detecting Proximity Sensor</h5>
<p>Not all iOS device have proximity sensors. The Apple API documentation states that you should enable proximity monitoring and check the proximityState, if the return value is NO, then the device does not have a sensor. </p>
<p>I was unable to successfully use this approach to determine if a device has a sensor. Any additional ideas or suggestions are welcome.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/device/using-the-proximity-sensor.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Query the Battery State and Level of Charge</title>
		<link>http://iPhoneDeveloperTips.com/device/display-battery-state-and-level-of-charge.html</link>
		<comments>http://iPhoneDeveloperTips.com/device/display-battery-state-and-level-of-charge.html#comments</comments>
		<pubDate>Tue, 01 Feb 2011 07:03:10 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Device]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=7673</guid>
		<description><![CDATA[There are times when checking in on the battery status can be a good idea. The code that follows shows how to monitor battery states and levels (percent of charge) using methods in the UIDevice class. Enable Battery Monitor To query information regarding the iPhone battery status, you start by enabling battery monitoring through a [...]]]></description>
			<content:encoded><![CDATA[<p>There are times when checking in on the battery status can be a good idea. The code that follows shows how to monitor battery states and levels (percent of charge) using methods in the <strong>UIDevice</strong> class.</p>
<h5>Enable Battery Monitor</h5>
<p>To query information regarding the iPhone battery status, you start by enabling battery monitoring through a call in the <strong>UIDevice</strong> class. With enabling active, you can then get the battery level and state (e.g. charging, discharging, etc):<br />
<span id="more-7673"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> setBatteryMonitoringEnabled<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> batteryLevel<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> batteryState<span style="color: #002200;">&#93;</span>;</pre></div></div>

<h5>Notification of Battery Changes</h5>
<p>You can monitor on going battery changes by registering to receive notifications of battery level and status updates:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// Request to be notified when battery charge or state changes</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>batteryStatus<span style="color: #002200;">&#41;</span> name<span style="color: #002200;">:</span>UIDeviceBatteryLevelDidChangeNotification object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<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>batteryStatus<span style="color: #002200;">&#41;</span> name<span style="color: #002200;">:</span>UIDeviceBatteryStateDidChangeNotification object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;</pre></div></div>

<p>Changes in the battery level/state will result in a call to the method <strong>batteryStatus</strong>, where you can update a visual que or otherwise handle changes.</p>
<h5>Display Battery Status</h5>
<p>To test the battery status I&#8217;ve written a simply view controller that has nothing more than a textview, where the current status and level are displayed. </p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/01/batterystatus.png" alt="" width="331" height="122" /></p>
<p>There are two relevant sections of code for this application, the first is within the loadView method where I setup a textview, enable battery monitoring and add observers for the two notifications I am interested in:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>loadView 
<span style="color: #002200;">&#123;</span>
  <span style="color: #002200;">&#91;</span>self setView<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIScreen mainScreen<span style="color: #002200;">&#93;</span> applicationFrame<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> autorelease<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Setup the textview to display battery status</span>
  textViewStatus <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UITextView alloc<span style="color: #002200;">&#93;</span> initWithFrame<span style="color: #002200;">:</span>CGRectMake	<span style="color: #002200;">&#40;</span><span style="color: #2400d9;">20</span>, <span style="color: #2400d9;">20</span>, <span style="color: #2400d9;">280</span>, <span style="color: #2400d9;">64</span><span style="color: #002200;">&#41;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>textViewStatus setFont<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIFont boldSystemFontOfSize<span style="color: #002200;">:</span><span style="color: #2400d9;">18.0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>textViewStatus setEditable<span style="color: #002200;">:</span><span style="color: #a61390;">NO</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>textViewStatus setTextAlignment<span style="color: #002200;">:</span>UITextAlignmentCenter<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>textViewStatus<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Round the corners and set border color  </span>
  <span style="color: #002200;">&#91;</span>textViewStatus setBackgroundColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>UIColor whiteColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>textViewStatus layer<span style="color: #002200;">&#93;</span> setBorderColor<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIColor blueColor<span style="color: #002200;">&#93;</span> CGColor<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>textViewStatus layer<span style="color: #002200;">&#93;</span> setBorderWidth<span style="color: #002200;">:</span><span style="color: #2400d9;">2.3</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>textViewStatus layer<span style="color: #002200;">&#93;</span> setCornerRadius<span style="color: #002200;">:</span><span style="color: #2400d9;">15</span><span style="color: #002200;">&#93;</span>;
  <span style="color: #002200;">&#91;</span>textViewStatus setClipsToBounds<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;">// Enable monitoring of battery status</span>
  <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> setBatteryMonitoringEnabled<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;">// Print current status</span>
  <span style="color: #002200;">&#91;</span>self batteryStatus<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Request to be notified when battery charge or state changes</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>batteryStatus<span style="color: #002200;">&#41;</span> name<span style="color: #002200;">:</span>UIDeviceBatteryLevelDidChangeNotification object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <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>batteryStatus<span style="color: #002200;">&#41;</span> name<span style="color: #002200;">:</span>UIDeviceBatteryStateDidChangeNotification object<span style="color: #002200;">:</span><span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
<span style="color: #002200;">&#125;</span></pre></div></div>

<p>The second method of interest is <strong>batteryStatus</strong>, which is the other end of the notification requests, that is, the method called via the OS when the battery status or level changes:</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>batteryStatus
<span style="color: #002200;">&#123;</span>
 <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>batteryStatus <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSArray</span> arrayWithObjects<span style="color: #002200;">:</span> 
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Battery status is unknown.&quot;</span>, 
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Battery is in use (discharging).&quot;</span>, 
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Battery is charging.&quot;</span>, 
    <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Battery is fully charged.&quot;</span>, <span style="color: #a61390;">nil</span><span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> batteryState<span style="color: #002200;">&#93;</span> <span style="color: #002200;">==</span> UIDeviceBatteryStateUnknown<span style="color: #002200;">&#41;</span>
  <span style="color: #002200;">&#123;</span>
    <span style="color: #002200;">&#91;</span>textViewStatus setText<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>batteryStatus objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</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>, <span style="color: #002200;">&#91;</span>batteryStatus objectAtIndex<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
  <span style="color: #002200;">&#125;</span>
  <span style="color: #a61390;">else</span>
  <span style="color: #002200;">&#123;</span>  	
    <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>msg <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithFormat<span style="color: #002200;">:</span>
      <span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Battery charge level: %0.2f%%<span style="color: #2400d9;">\n</span>%@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> batteryLevel<span style="color: #002200;">&#93;</span> <span style="color: #002200;">*</span> <span style="color: #2400d9;">100</span>,
    <span style="color: #002200;">&#91;</span>batteryStatus objectAtIndex<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> batteryState<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span> <span style="color: #002200;">&#93;</span>;
&nbsp;
    <span style="color: #002200;">&#91;</span>textViewStatus setText<span style="color: #002200;">:</span>msg<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>, msg<span style="color: #002200;">&#41;</span>;
  <span style="color: #002200;">&#125;</span>
<span style="color: #002200;">&#125;</span></pre></div></div>

<h5>Notes</h5>
<p>The battery status returned from the API&#8217;s above may not always be in sync with the status displayed on device &#8211; notice in the screenshot above that the device status is different than what is displayed in the textview (bug or feature, I&#8217;m not certain).</p>
<p>Also, Apple recommends that you enable battery monitoring only when your application needs to be notified of changes versus leaving enabling monitoring during the entire lifecycle of your app.</p>
<h5>Xcode Project Source Code</h5>
<p>Download Xcode Project: <a href="http://iPhoneDeveloperTips.com/wp-content/uploads/2011/01/BatteryStatusIndicator.zip">Battery Status Indicator</a></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/device/display-battery-state-and-level-of-charge.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get iPhone Device Name, Unique Device Identifier (UDID), OS and Model</title>
		<link>http://iPhoneDeveloperTips.com/device/get-iphone-device-name-unique-device-identifier-udid-os-and-model.html</link>
		<comments>http://iPhoneDeveloperTips.com/device/get-iphone-device-name-unique-device-identifier-udid-os-and-model.html#comments</comments>
		<pubDate>Thu, 17 Dec 2009 04:21:58 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Device]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=3219</guid>
		<description><![CDATA[The iPhone SDK includes a singleton of class type UIDevice that you can use to retrieve device specific information (this works equally as well with the simulator). Available Properties in UIDevice uniqueIdentifier &#8211; identifier guaranteed to be unique for every device name &#8211; arbitrary name found in General > About setting on device systemName &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>The iPhone SDK includes a singleton of class type <strong>UIDevice</strong> that you can use to retrieve device specific information (this works equally as well with the simulator).</p>
<h5>Available Properties in UIDevice</h5>
<blockquote>
<ul>
<li>uniqueIdentifier &#8211; identifier guaranteed to be unique for every device</li>
<li>name &#8211; arbitrary name found in General > About setting on device</li>
<li>systemName &#8211; name of the OS running on the device</li>
<li>systemVersion &#8211; current version of the OS</li>
<li>model- model, such as ”iPhone” or ”iPod touch”</li>
<li>localizedModel &#8211; same as above using a localized string</li>
</ul>
</blockquote>
<p><span id="more-3219"></span></p>
<h5>Stats for Simulator</h5>
<p>This code will list all stats shown above:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;uniqueIdentifier: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> uniqueIdentifier<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;name: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> name<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;systemName: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> systemName<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;systemVersion: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> systemVersion<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;model: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> model<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;localizedModel: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> localizedModel<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Running the above on the simulator returns:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/12/device2.png" /></p>
<h5>Stats for iPhone</h5>
<p>The device stats when running on my iPhone:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/12/device1.png" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/device/get-iphone-device-name-unique-device-identifier-udid-os-and-model.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Determine Device Type &#8211; 3G or 3GS / iPod First or Second Generation</title>
		<link>http://iPhoneDeveloperTips.com/device/determine-if-iphone-is-3g-or-3gs-determine-if-ipod-is-first-or-second-generation.html</link>
		<comments>http://iPhoneDeveloperTips.com/device/determine-if-iphone-is-3g-or-3gs-determine-if-ipod-is-first-or-second-generation.html#comments</comments>
		<pubDate>Mon, 24 Aug 2009 14:28:19 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Device]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=3453</guid>
		<description><![CDATA[Using the UIDevice class, you can figure out if your application is running on a iPod or an iPhone. To get this information, access the property named model which will return an NSString of iPhone or iPod touch. However, there may be times when you need more specifics, for example is the iPhone the latest [...]]]></description>
			<content:encoded><![CDATA[<p>Using the <strong>UIDevice</strong> class, you can figure out if your application is running on a iPod or an iPhone. To get this information, access the property named <strong>model</strong> which will return an <strong>NSString</strong> of <strong>iPhone</strong> or <strong>iPod touch</strong>. However, there may be times when you need more specifics, for example is the iPhone the latest device, a 3GS or on the flip side, is the iPod a first or second generation device?</p>
<p>Accessing the system hardware will reveal the information we need to make the above distinctions. We&#8217;ll use a <strong>category</strong> to extend the <strong>UIDevice</strong> class with a method that returns the &#8220;machine&#8221; type as a string. Begin with the interface definition below:<br />
<span id="more-3453"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> UIDevice<span style="color: #002200;">&#40;</span>machine<span style="color: #002200;">&#41;</span>
<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>machine;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>To access the hardware type we&#8217;ll call a function in the standard C library:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">sysctlbyname<span style="color: #002200;">&#40;</span><span style="color: #a61390;">const</span> <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>name, <span style="color: #a61390;">void</span> <span style="color: #002200;">*</span>oldp, <span style="color: #a61390;">size_t</span> <span style="color: #002200;">*</span>oldlenp, <span style="color: #a61390;">void</span> <span style="color: #002200;">*</span>newp, <span style="color: #a61390;">size_t</span> newlen<span style="color: #002200;">&#41;</span>;</pre></div></div>

<blockquote>
<ul>
<li>
<a target="_blank" href="http://developer.apple.com/documentation/Darwin/Reference/Manpages/man3/sysctlbyname.3.html">Read more about sysctlbyname.</a>
</li>
</ul>
</blockquote>
<p>The first parameter is the name of the system information we are after. The information will be copied into the buffer &#8216;oldp&#8217; and we specify the size of this buffer in the &#8216;oldlenp&#8217; parameter. We set &#8216;newp&#8217; to NULL as this is for setting a value (versus getting a value). On a similar note, we set &#8216;newlen&#8217; to 0, as that is reserved for calls to set information.</p>
<p>Putting all that together into, the implementation of our class looks as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;UIDevice+machine.h&quot;</span>
<span style="color: #6e371a;">#include &lt;sys/types.h&gt;</span>
<span style="color: #6e371a;">#include &lt;sys/sysctl.h&gt;</span>
&nbsp;
<span style="color: #a61390;">@implementation</span> UIDevice<span style="color: #002200;">&#40;</span>machine<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>machine
<span style="color: #002200;">&#123;</span>
  <span style="color: #a61390;">size_t</span> size;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Set 'oldp' parameter to NULL to get the size of the data</span>
  <span style="color: #11740a; font-style: italic;">// returned so we can allocate appropriate amount of space</span>
  sysctlbyname<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;hw.machine&quot;</span>, <span style="color: #a61390;">NULL</span>, <span style="color: #002200;">&amp;</span>size, <span style="color: #a61390;">NULL</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>; 
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Allocate the space to store name</span>
  <span style="color: #a61390;">char</span> <span style="color: #002200;">*</span>name <span style="color: #002200;">=</span> <span style="color: #a61390;">malloc</span><span style="color: #002200;">&#40;</span>size<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Get the platform name</span>
  sysctlbyname<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;hw.machine&quot;</span>, name, <span style="color: #002200;">&amp;</span>size, <span style="color: #a61390;">NULL</span>, <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Place name into a string</span>
  <span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>machine <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSString</span> stringWithCString<span style="color: #002200;">:</span>name<span style="color: #002200;">&#93;</span>;
&nbsp;
  <span style="color: #11740a; font-style: italic;">// Done with this</span>
  <span style="color: #a61390;">free</span><span style="color: #002200;">&#40;</span>name<span style="color: #002200;">&#41;</span>;
&nbsp;
  <span style="color: #a61390;">return</span> machine;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>We can now call the new method as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#import &quot;UIDevice+machine.h&quot;</span>
&nbsp;
...
&nbsp;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;device: %@&quot;</span>, <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span> machine<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>The return values will be as follows:</p>
<ul>
<li>iPhone Simulator == i386</li>
<li>iPhone  == iPhone1,1</li>
<li>3G iPhone == iPhone1,2</li>
<li>3GS iPhone == iPhone2,1</li>
<li>4 iPhone == iPhone3,1</li>
<li>1st Gen iPod == iPod1,1</li>
<li>2nd Gen iPod == iPod2,1</li>
<li>3rd Gen iPod == iPod3,1</li>
</ul>
<p>The following images show the output in Xcode (running in the simulator) and on my 3G iphone:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/08/sim.png" /></p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/08/dev.png" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/device/determine-if-iphone-is-3g-or-3gs-determine-if-ipod-is-first-or-second-generation.html/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Is Application Running on iPhone or iPod touch</title>
		<link>http://iPhoneDeveloperTips.com/device/is-application-running-on-iphone-or-ipod-touch.html</link>
		<comments>http://iPhoneDeveloperTips.com/device/is-application-running-on-iphone-or-ipod-touch.html#comments</comments>
		<pubDate>Thu, 06 Aug 2009 18:07:36 +0000</pubDate>
		<dc:creator>John Muchow</dc:creator>
				<category><![CDATA[Device]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=3203</guid>
		<description><![CDATA[If you ever need to know whether your application is running on an iPod touch or an iPhone, the answer is as close as this one line of code: NSString *deviceType = &#91;UIDevice currentDevice&#93;.model; To get the model of the device as a localized string: NSString *deviceType = &#91;UIDevice currentDevice&#93;.modellocalizedModel; NSLog&#40;@&#34;type: %@&#34;, deviceType&#41;; One use [...]]]></description>
			<content:encoded><![CDATA[<p>If you ever need to know whether your application is running on an iPod touch or an iPhone, the answer is as close as this one line of code:</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>deviceType <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span>.model;</pre></div></div>

<p>To get the model of the device as a localized string:<br />
<span id="more-3203"></span></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>deviceType <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span>.modellocalizedModel;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;type: %@&quot;</span>, deviceType<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>One use could be to customize messages shown within your applications, for example:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">float</span> version <span style="color: #002200;">=</span> <span style="color: #2400d9;">1.1</span>;
...
<span style="color: #400080;">NSString</span> <span style="color: #002200;">*</span>deviceType <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>UIDevice currentDevice<span style="color: #002200;">&#93;</span>.model;
NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Your %@ has version %2.1f of Today's Horoscope installed.&quot;</span>, 
    deviceType, version<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>Here&#8217;s the output from both of the code snippets is shown below:</p>
<p><img src="http://iPhoneDeveloperTips.com/wp-content/uploads/2009/08/type.png" /></p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/device/is-application-running-on-iphone-or-ipod-touch.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

