<?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>MentorMate &#187; Iordan</title>
	<atom:link href="http://mentormate.com/blog/author/iordan/feed/" rel="self" type="application/rss+xml" />
	<link>http://mentormate.com</link>
	<description>Web, Application, Mobile &#38; Custom Software Development</description>
	<lastBuildDate>Wed, 16 May 2012 15:12:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Mapping XML Document to Object Model Using DOM in iPhone Application</title>
		<link>http://mentormate.com/blog/mapping-xml-document-object-model-dom-iphone-application/</link>
		<comments>http://mentormate.com/blog/mapping-xml-document-object-model-dom-iphone-application/#comments</comments>
		<pubDate>Mon, 12 Apr 2010 12:23:35 +0000</pubDate>
		<dc:creator>Iordan</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://mentormate.com/blog/?p=606</guid>
		<description><![CDATA[This tutorial introduces you to the basic concepts of XML and using a Document Object Model (DOM) as a parser for the iPhone to parse XML. The intended audience is XML beginners with knowledge of Objective C.]]></description>
			<content:encoded><![CDATA[<p>The idea here is to parse the XML response from a Wunderground Data Feed Web Service, which provides information for current weather conditions. The conceptual part of target XML follows. More details about XML and available weather API can be found on the <a title="http://wiki.wunderground.com/index.php/API_-_XML" href="http://wiki.wunderground.com/index.php/API_-_XML">Wunderground Data Feed</a> site.</p>
<pre class="wp-code-highlight prettyprint">
&amp;lt;current_observation&amp;gt;
  &amp;lt;credit&amp;gt;Weather Underground NOAA Weather Station&amp;lt;/credit&amp;gt;
  &amp;lt;credit_URL&amp;gt;http://wunderground.com/&amp;lt;/credit_URL&amp;gt;
  &amp;lt;image&amp;gt;
    &amp;lt;url&amp;gt;http://icons.wunderground.com/graphics/wu2/logo_130x80.png&amp;lt;/url&amp;gt;
    &amp;lt;title&amp;gt;Weather Underground&amp;lt;/title&amp;gt;
    &amp;lt;link&amp;gt;http://wunderground.com/&amp;lt;/link&amp;gt;
  &amp;lt;/image&amp;gt;
  +&amp;lt;display_location&amp;gt;&amp;lt;/display_location&amp;gt;
  +&amp;lt;observation_location&amp;gt;&amp;lt;/observation_location&amp;gt;
  &amp;lt;station_id&amp;gt;KSFO&amp;lt;/station_id&amp;gt;
  &amp;lt;observation_time&amp;gt;Last Updated on July 14, 9:56 AM PDT&amp;lt;/observation_time&amp;gt;
  ...
  &amp;lt;local_time&amp;gt;July 14, 10:01 AM PDT&amp;lt;/local_time&amp;gt;
  ...
  &amp;lt;local_epoch&amp;gt;1247590877&amp;lt;/local_epoch&amp;gt;
  &amp;lt;weather&amp;gt;fog&amp;lt;/weather&amp;gt;
  &amp;lt;temperature_string&amp;gt;52 F (11 C)&amp;lt;/temperature_string&amp;gt;
  &amp;lt;temp_f&amp;gt;52&amp;lt;/temp_f&amp;gt;
  &amp;lt;temp_c&amp;gt;11&amp;lt;/temp_c&amp;gt;
  &amp;lt;relative_humidity&amp;gt;76%&amp;lt;/relative_humidity&amp;gt;
  ...
  &amp;lt;icon&amp;gt;fog&amp;lt;/icon&amp;gt;
  ...
&amp;lt;/current_observation&amp;gt;
</pre>
<p>For processing this XML we decided to use DOM parser for the following reasons:</p>
<ul>
<li>To save the structure of data      (more flexible usage for presentation and further processing)</li>
<li>To ensure good      maintainability and adaptability (external provider may change XML      structure)</li>
<li>The XML is not so large that it would not cause memory issues.</li>
</ul>
<div class="caption"><a href="http://mentormate.com/wp-content/uploads/2010/03/Fig.1-The-DOM-presents-an-XML-document-as-a-tree-structure.png" rel="lightbox[606]" title="Mapping XML Document to Object Model Using DOM in iPhone Application"><img class="size-medium wp-image-610" src="http://mentormate.com/wp-content/uploads/2010/03/Fig.1-The-DOM-presents-an-XML-document-as-a-tree-structure-268x300.png" alt="" width="268" height="300" /></a>
<div>Figure 1 the DOM presents an XML document as a tree-structure</div>
</div>
<h2><strong>DOM Tree</strong></h2>
<p>The DOM presents an XML document as a tree-structure. The parser creates a tree object out of the document. The user accesses data by traversing the tree. The API allows for constructing, accessing and manipulating the structure and content of XML documents (Figure 2).</p>
<div class="caption"><a href="http://mentormate.com/wp-content/uploads/2010/03/Fig-2-Using-a-DOM-Tree.png" rel="lightbox[606]" title="Mapping XML Document to Object Model Using DOM in iPhone Application"><img class="size-medium wp-image-612" src="http://mentormate.com/wp-content/uploads/2010/03/Fig-2-Using-a-DOM-Tree-300x86.png" alt="" width="300" height="86" /></a>
<div>Figure 2 Using a DOM Tree.</div>
</div>
<p>For our implementation we use a very simple and effective parser - <a title="http://osmorphis.blogspot.com/2009/03/enhancing-standard-nsxmlparser-class.html" href="http://osmorphis.blogspot.com/2009/03/enhancing-standard-nsxmlparser-class.html">XMLTreeParser/XMLTreeNode</a>. The implementation consists of two classes: <em>XMLTreeParser</em> and <em>XMLTreeNode</em>. It uses <em>NSXMLParser</em> to read the document and for every one <em>&lt;element&gt;</em> tag creates a <em>XMLTreeNode</em> object. That way, it creates the tree that contains nodes of type <em>XMLTreeNode</em>.</p>
<p>To create a parser, simply instantiate the <em>XMLTreeParser</em> class:</p>
<pre class="wp-code-highlight prettyprint">XMLTreeParser* parser = [[XMLTreeParser alloc] init];</pre>
<p><strong> </strong></p>
<p class="MsoNormal">After that start parsing &#8211; call the parse method and provide the XML data:</p>
<p class="MsoNormal"><span> </span></p>
<pre class="wp-code-highlight prettyprint">&amp;lt;strong&amp;gt;XMLTreeNode* root = [parser parse:xmlData];</pre>
<p><strong> </strong></p>
<p class="MsoNormal">Now you can traverse through the tree manually looking for things.</p>
<p class="MsoNormal"><strong><span> </span></strong></p>
<p><strong> </strong></p>
<pre class="wp-code-highlight prettyprint">NSArray* items = [stuffindChildren:@&amp;quot;icon&amp;quot;];</pre>
<h2><strong>Architecture of an XML application using DOM</strong></h2>
<p class="MsoNormal"><span style="font-weight: normal;">The DOM model is easiest to understand. On Figure 3, a basic architecture of an XML application using DOM is presented:</span></p>
<p class="MsoNormal">
<div class="caption"><a href="http://mentormate.com/wp-content/uploads/2010/03/Fig-3-Architecture-of-an-XML-application-using-DOM.png" rel="lightbox[606]" title="Mapping XML Document to Object Model Using DOM in iPhone Application"><img class="size-medium wp-image-613" src="http://mentormate.com/wp-content/uploads/2010/03/Fig-3-Architecture-of-an-XML-application-using-DOM-300x145.png" alt="" width="300" height="145" /></a>
<div>Figure 3 Architecture of an XML application using DOM</div>
</div>
<p class="MsoNormal"><span style="font-weight: normal;">So here&#8217;s what happens: A parser reads the XML file and builds a DOM document to match the XML file. From that point until a save is performed, all interaction between the application and XML hits the DOM document rather than the corresponding XML file. It&#8217;s interesting to note that almost all XML parsers use SAX. Before you build a DOM document you must detect events such as the start of an element (start tag encountered), end of an element (end tag encountered) and/or a new attribute (name followed by equal sign followed by quoted string encountered). DOM can be thought of as an extra abstraction to lessen the programmer&#8217;s workload, at the expense of memory usage.</span></p>
<p class="MsoNormal"><span style="font-weight: normal;">Modifications are made directly to the DOM document. Elements can be added, deleted, renamed, and rearranged. Text nodes can be added, delete,d or changed. Elements can be moved either within the same level, or promoted or demoted to different levels.</span></p>
<h2><span style="font-weight: normal;"> </span><strong>Choose a design pattern for implementation</strong></h2>
<p class="MsoNormal"><span style="font-weight: normal;">Choosing the appropriate pattern is a critical step. The Dynamic Document architectural pattern was used in our case. This pattern contains XML not typed by DTD or schema, but follows assessors for underlying program objects. It allows for unlimited extension by multiple, uncoordinated parties at the cost of lack of type-checking, and it&#8217;s simple to implement.</span></p>
<p class="MsoNormal"><span style="font-weight: normal;"> </span></p>
<div class="caption"><a href="http://mentormate.com/wp-content/uploads/2010/03/Fig-4-Map-the-XML-document-to-object-model.png" rel="lightbox[606]" title="Mapping XML Document to Object Model Using DOM in iPhone Application"><img class="size-medium wp-image-614" src="http://mentormate.com/wp-content/uploads/2010/03/Fig-4-Map-the-XML-document-to-object-model-300x210.png" alt="" width="300" height="210" /></a>
<div>Figure 4 Map the XML document to object model.</div>
</div>
<p class="MsoNormal"><span style="font-weight: normal;">The DOM API is used to read information from an XML document. There is an easier way of getting around using DOM for modifying and saving the XML data &#8212; creating an object model for the information in the document. You can create this object model by giving it a DOM object that holds all the XML document information. That is, the XML document should be mapped to an object model.</span></p>
<p class="MsoNormal"><span style="font-weight: normal;">Figure 4 shows how to map the Current Observation XML document to a set of related classes (object model). The &lt;current_observation&gt; element is mapped to the CurrentObservation class; the &lt;display_location&gt; element is mapped to the DisplayLocation class. The &lt;observation_location&gt; element is mapped to the ObservationLocation class. Single elements (without sub elements or others attributes) are mapped to the parent element class properties.  Each class can be instantiated using a method or Factory implementation by providing a part of XML tree, with root – class element. In this the exampleinit method was used -</span><!--[if gte mso 9]&gt; 08D0C9EA79F9BACE118C8200AA004BA90B02000000080000000E0000005F005200650066003200350037003300360039003900370034000000 &lt;![endif]--><em><span style="font-weight: normal;">(id)initWithData:(NSData *)data</span></em><span style="font-weight: normal;">;.</span></p>
<p class="MsoNormal"><span style="font-weight: normal;">Here is a partial code listing from CurrentObservation.h:</span></p>
<pre class="wp-code-highlight prettyprint">
  @interface WUCurrWeather : NSObject {
   NSString *credit;
   ...
   Image *image;
   DisplayLocation *displayLocation;
   ...
   NSString *icon;
  ...
  }

  @property(nonatomic, retain) NSString *credit;
  @property(nonatomic, retain) Image *image;
  @property(nonatomic, retain) DisplayLocation *displayLocation;
  ...
  @property(nonatomic, retain) NSString *icon;
  ...

  - (id)initWithData:(NSData *)data;

  -(NSString *)description;
  @end;
</pre>
<div>and Image.h <span style="font-size: 13.2px;">files</span></div>
<div>
<p><span style="font-size: 13.2px;"> </span></p>
<div>
<pre class="wp-code-highlight prettyprint">@interface Image : NSObject {
NSString *url;
NSString *title;
NSString *link;
}
@property(nonatomic, retain) NSString *url;
@property(nonatomic, retain) NSString *title;
@property(nonatomic, retain) NSString *link;
- (id)initWidthData:(XMLTreeNode *)root;
- (NSString *) description;
@end
</pre>
</div>
<div>Now you can use your object model to manage data as you wish – save it in a database or show it in a GUI for example.</div>
<h2><strong>Example</strong></h2>
<div>
<p class="MsoNormal">Below is an example of a View Based iPhone Application that calls a Wunderground Data Feed Web Service to get current weather conditions. The update button repeats the call and refreshes the screen when new data arrives. The screenshots below visualize the application&#8217;s behavior:</p>
<table width="100%">
<tbody>
<tr>
<td><a href="http://mentormate.com/wp-content/uploads/2010/03/xml2obj.png" rel="lightbox[606]" title="Mapping XML Document to Object Model Using DOM in iPhone Application"><img class="aligncenter size-full wp-image-617" src="http://mentormate.com/wp-content/uploads/2010/03/xml2obj.png" alt="" width="79" height="87" /></a></td>
<td><a href="http://mentormate.com/wp-content/uploads/2010/03/screenshot1.png" rel="lightbox[606]" title="Mapping XML Document to Object Model Using DOM in iPhone Application"><img class="aligncenter size-full wp-image-615" src="http://mentormate.com/wp-content/uploads/2010/03/screenshot1.png" alt="" width="161" height="241" /></a></td>
<td><a href="http://mentormate.com/wp-content/uploads/2010/03/screenshot2.png" rel="lightbox[606]" title="Mapping XML Document to Object Model Using DOM in iPhone Application"><img class="aligncenter size-full wp-image-616" src="http://mentormate.com/wp-content/uploads/2010/03/screenshot2.png" alt="" width="164" height="247" /></a></td>
</tr>
</tbody>
</table>
<p class="MsoNormal">In the provided <a href="http://mentormate.com/wp-content/uploads/2010/03/xml2obj.zip">source code</a> you can find all these steps implemented.</p>
</div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://mentormate.com/blog/mapping-xml-document-object-model-dom-iphone-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>iPhone Application Development XML Parser/Parsing Libraries</title>
		<link>http://mentormate.com/blog/iphone-application-development-xml-parser-parsing-libraries/</link>
		<comments>http://mentormate.com/blog/iphone-application-development-xml-parser-parsing-libraries/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 14:46:02 +0000</pubDate>
		<dc:creator>Iordan</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[dom]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[libraries]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[sax]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://mentormate.com/blog/?p=552</guid>
		<description><![CDATA[XML (Extensible Markup Language) is a set of rules for encoding documents electronically. It is defined in the XML 1.0 Specification produced by the W3C. XML’s design goals emphasize simplicity, generality, and usability over the Internet. Although XML’s design focuses on documents, it is widely used for the representation of arbitrary data structures, for example in web services. A variety of APIs for accessing XML have been developed and used, and some have been standardized.]]></description>
			<content:encoded><![CDATA[<p>Existing APIs for XML processing fall into two categories:</p>
<ul>
<li><a title="http://en.wikipedia.org/wiki/Simple_API_for_XML" href="http://en.wikipedia.org/wiki/Simple_API_for_XML">SAX (Simple      API for XML)</a> is a serial access parser API for XML. It      provides a mechanism for reading data from an XML document. The quantity      of memory that a SAX parser must use in order to function is typically      much smaller than that of a DOM parser. Unlike DOM, there is no formal      specification for SAX.</li>
<li>The <a title="http://en.wikipedia.org/wiki/Document_Object_Model" href="http://en.wikipedia.org/wiki/Document_Object_Model">Document      Object Model (DOM)</a> is a cross-platform and      language-independent convention for representing and interacting with      objects in HTML, XHTML and XML documents. It represents a tree structure      based API. The Dom parser implements the DOM API and it creates a DOM tree      in memory for a XML document. Because DOM supports navigation in any      direction (e.g., parent and previous sibling) and allows for arbitrary      modifications, an implementation must at least buffer the document that has      been read so far (or some parsed form of it).</li>
</ul>
<p>Cocoa offers a &#8220;complete&#8221; XML parser with the NSXML family of classes.</p>
<ul>
<li><strong>NSXMLParser</strong> is a SAX parser, meaning that it traverses      the XML tree and informs a delegate of events as they happen. This is an      event-driven parser, which calls methods on a delegate to handle      &#8220;events&#8221; as it parses through the XML.</li>
<li><strong>NSXMLDocument</strong> provides tree parser functionality. An      instance of NSXMLDocument represents an XML document as internalized into      a logical tree structure. This tree-based parser is fairly sophisticated      but it is not included in Cocoa-Touch.</li>
</ul>
<p>In the spirit of small and simple, iPhone developers have the NSXMLParser class to use. Because NSXMLParser only generates messages for a delegate, and doesn’t store data about the XML tree, it requires less memory and is better for accessing small pieces of data or parsing large XML documents. The basic operation of the NSXMLParser class will not be covered here. This is well documented in the <a title="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/XMLParsing/XMLParsing.html" href="http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/XMLParsing/XMLParsing.html">Apple Programming Guide</a>.</p>
<p>For devices like iPhone where memory usage is critical, using tree parser is not recommended. I guess that is why Apple doesn’t provide NSXMLDocument on iPhone/iPod touch. However, the model of event parsing may not be acceptable by your code. NSXMLParser is probably adequate, but it is not going to handle all your XML parsing needs. The biggest problem with this type of streaming parser is that you lose the structure of your data; your call-backs do not provide any context or hierarchy. Because of the above mentioned or by some other reason like often changing XML format [1] you may need to use something like DOM model implementation.</p>
<h2>Available Libraries (DOM model implementations) for iPhone</h2>
<ul>
<li>XPath/XQuery &#8211; used to      navigate through elements and attributes in an XML document.</li>
<li><a title="http://xmlsoft.org/" href="http://xmlsoft.org/">Libxml2</a> &#8211;      XML C parser and toolkit developed for the Gnome project. At Cocoa with      Love, very nice explanation and code sample to write wrapper for the      libxml2 was posted. It has a title, <a title="http://go2.wordpress.com/?id=725X1342&amp;site=jongampark.wordpress.com&amp;url=http%3A%2F%2Fcocoawithlove.com%2F2008%2F10%2Fusing-libxml2-for-parsing-and-xpath.html" href="http://go2.wordpress.com/?id=725X1342&amp;site=jongampark.wordpress.com&amp;url=http%3A%2F%2Fcocoawithlove.com%2F2008%2F10%2Fusing-libxml2-for-parsing-and-xpath.html">“Using      libxml2 for XML parsing and XPath queries in Cocoa”</a>[2]</li>
<li><a title="http://code.google.com/p/kissxml/" href="http://code.google.com/p/kissxml/">KissXML</a>,<a title="http://go2.wordpress.com/?id=725X1342&amp;site=jongampark.wordpress.com&amp;url=http%3A%2F%2Fcode.google.com%2Fp%2Ftouchcode%2Fwiki%2FTouchXML" href="http://go2.wordpress.com/?id=725X1342&amp;site=jongampark.wordpress.com&amp;url=http%3A%2F%2Fcode.google.com%2Fp%2Ftouchcode%2Fwiki%2FTouchXML">TouchXML</a> &#8211;      two tree XML parsers for iPhone.</li>
<li><a title="http://github.com/arashpayan/apxml/" href="http://github.com/arashpayan/apxml/">APXML</a> &#8211; not a perfect      implementation of the W3C XML 1.0 standard, but it’s close enough for a      lot of usage.</li>
<li>XMLTreeParser/XMLTreeNode      -  <a title="http://osmorphis.blogspot.com/2009/03/enhancing-standard-nsxmlparser-class.html" href="http://osmorphis.blogspot.com/2009/03/enhancing-standard-nsxmlparser-class.html">Enhancing      the standard NSXMLParser class</a></li>
</ul>
<h2>Which parser (DOM or SAX) should you use?</h2>
<p>Well, if you’re going to need to parse very large XML documents, then you should probably consider NSXMLParser. What “very large” means depends a lot on the available memory you have to work with? The model of iPhone and the memory already in use by your app may prevent you from being able to use DOM model.</p>
<p>While DOM model usage may seem easier, NSXMLParser really excels in situations where you’re loading in many instances of the same kind of data, such as an address book or twitter feed. However, if memory is not an issue, I would recommend DOM model. It makes it easy to access the data you want and also allows you to manipulate the tree.</p>
<h2>References</h2>
<ol>
<li>Arash Payan(Salman),<a title="http://arashpayan.com/blog/index.php/2009/01/14/apxml-nsxmldocument-substitute-for-iphoneipod-touch/" href="http://arashpayan.com/blog/index.php/2009/01/14/apxml-nsxmldocument-substitute-for-iphoneipod-touch/">APXML: NSXMLDocument ’substitute’ for iPhone/iPod Touch – Arash Payan</a>,Sep 27th, 2009</li>
<li>jongampark, <a title="http://jongampark.wordpress.com/2009/08/23/tree-xml-parser-for-iphone/" href="http://jongampark.wordpress.com/2009/08/23/tree-xml-parser-for-iphone/">Tree XML parser for iPhone</a>, 2009</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://mentormate.com/blog/iphone-application-development-xml-parser-parsing-libraries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using iPhone UIWebView Class with local CSS &amp; JavaScript resources</title>
		<link>http://mentormate.com/blog/iphone-uiwebview-class-local-css-javascript-resources/</link>
		<comments>http://mentormate.com/blog/iphone-uiwebview-class-local-css-javascript-resources/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 17:16:50 +0000</pubDate>
		<dc:creator>Iordan</dc:creator>
				<category><![CDATA[Mobile]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[iphone]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[uiwebview]]></category>

		<guid isPermaLink="false">http://mentormate.com/blog/?p=433</guid>
		<description><![CDATA[With the great significance of the large screen on iPhone / iPod Touch, the graphical user interfaces experience of iPhone applications are more important than on other mobile platforms. One possible approach to make your application more interactive and dynamic is to use the default iPhone Design Resource UIWebView with local style definition (CSS) and JavaScript.]]></description>
			<content:encoded><![CDATA[<div id="_mcePaste">This post will cover the basic setup and creation of an application with web content for iPhone that will load local CSS and some useful JavaScript functions. Most of these hints I found partially in different blogs and forums listed in the reference section. The idea is to collect all them together. You can use the following technique to create a more attractive application design.</div>
<div id="_mcePaste">Creating an application using UIWebView control is not the focus of this post. Here is a helpful beginner’s tutorial for creating an application using UIWebView. We assume that you are already familiar with this topic.</div>
<h3>UIWebView load</h3>
<p>Our work will start with a little change in calling load method of the UIWebView instance. Instead of <em>loadRequest</em></p>
<pre class="wp-code-highlight prettyprint">[[webView graphWebView] loadRequest:requestObj];</pre>
<p>we use the <em>loadHTMLString</em> method, providing an NSString object that contains HTML code of our page as a parameter:</p>
<pre class="wp-code-highlight prettyprint">[[webView graphWebView] loadHTMLString:htmlPageStr baseURL:baseURL];</pre>
<p>To get this page to load resources and styles correctly, you have to set up the right URL:</p>
<h3>Set baseURL property of UIWebView to refer your bundle&#8217;s resources directory</h3>
<div id="attachment_437" class="wp-caption alignright" style="width: 143px"><a style="float: right;" href="http://mentormate.com/wp-content/uploads/2010/03/screen1.png" target="_blank" rel="lightbox[433]" title="Using iPhone UIWebView Class with local CSS &#038; JavaScript resources"><img class="size-medium wp-image-437" src="http://mentormate.com/wp-content/uploads/2010/03/screen1-208x300.png" alt="MentorMate" width="133" height="192" /></a><p class="wp-caption-text">Initial loading screen with progress indicator</p></div>
<p>On the following reference<sup>[2]</sup> you can find a short and clear revision of the problem of displaying local images in a UIWebView. To use relative paths or files in UIWebView, you have to load the HTML into the view with the correct baseURL parameter. The following example shows how to do this:</p>
<pre class="wp-code-highlight prettyprint">NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];</pre>
<p>And then call load method of UIWebView provide above NSURL parameter:</p>
<pre class="wp-code-highlight prettyprint">[[webView graphWebView] loadHTMLString:pageStr baseURL:baseURL];</pre>
<p>A short explanation: An <em>NSBundle</em> object represents a location in the file system that groups code and resources that can be used in a program. <em>mainBundle </em>method returns the <em>NSBundle</em>object that corresponds to the directory where the current <em>application executable</em> is located. <em>bundlePath </em>instance method of <em>NSBundle</em> returns the full pathname of the receiver’s bundle directory. <em>NSURL</em> class is used to create an object which will hold the URL information. It provides a way to manipulate URLs and the resources they reference. <em>loadHTMLString</em> method of UIWebView has parameter <em>baseURL</em> that accept an NSURL object instead of a pathname as the file reference. Giving the above baseURL to UIWebView, you can refer to your bundle&#8217;s resources directory like this:</p>
<pre class="wp-code-highlight prettyprint">&amp;lt;img src=&amp;quot;myimage.png&amp;quot;&amp;gt;</pre>
<p>Or from within CSS like this:</p>
<pre class="wp-code-highlight prettyprint">background-image: url(loading.gif)</pre>
<p>It’s important to note that resources (images, CSS and JavaScript Files) inside your application bundle are at the root of the bundle, even if you place them in an separate group (folder) in your project.</p>
<p>Giving the correct URL to UIWebView will allow you to refer to local resources in your page, but it does not work in all cases. If you want to use JavaScipt, some additional settings have to be done.</p>
<h3>Link a CSS and JavaScript files</h3>
<div id="attachment_438" class="wp-caption alignright" style="width: 140px"><a href="http://mentormate.com/wp-content/uploads/2010/03/screen2.png" target="_blank" rel="lightbox[433]" title="Using iPhone UIWebView Class with local CSS &#038; JavaScript resources"><img class="size-medium wp-image-438" src="http://mentormate.com/wp-content/uploads/2010/03/screen2-204x300.png" alt="MentorMate" width="130" height="192" /></a><p class="wp-caption-text">Second loading screen with progress indicator</p></div>
<p>Now you can use generic HTML technique to add CSS and JavaScript to the web page and displaying in a UIWebView.</p>
<pre class="wp-code-highlight prettyprint">&amp;lt;link href=&amp;quot;default.css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; type=&amp;quot;text/css&amp;quot; /&amp;gt;
 &amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;showhide.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;</pre>
<p>Do not forget to add your CSS and Javascript files to the xCode project (if you are using external files).</p>
<p>Some additional actions have to be taken before starting your application and using JavaScript functions.<sup>[3]</sup></p>
<p>XCode setup (*.js) javascript as some type of source code needs to be compiled in the application. We would like to include it as a resource so I&#8217;ve solved it by doing two things:</p>
<ol>
<li>Select .js file and in the &#8220;Detail&#8221; view unselect the bullseye column indicating it is compiled code</li>
<li>In the &#8220;Groups &amp; files&#8221; view expand the &#8220;Targets&#8221; tree and expand the application then go to &#8220;Copy Bundle Resources&#8221; and drag the *.js files into it.</li>
</ol>
<p>The Apple dev forums has a posted solution. <sup>[4]</sup> You need to do two things &#8211; select the .js file in your project, and turn off the checkbox that indicates that it is compiled (the &#8220;bullseye&#8221; column). If you don&#8217;t do this, you&#8217;ll get a warning in your build log about being unable to compile the file (which should be your first warning &#8211; always try to figure out and and correct any and all warnings that appear in your build).</p>
<h3>Set transparent background color for UIWebView</h3>
<div id="attachment_436" class="wp-caption alignright" style="width: 139px"><a href="http://mentormate.com/wp-content/uploads/2010/03/screen3.png" target="_blank" rel="lightbox[433]" title="Using iPhone UIWebView Class with local CSS &#038; JavaScript resources"><img class="size-medium wp-image-436" src="http://mentormate.com/wp-content/uploads/2010/03/screen3-201x300.png" alt="MentorMate" width="129" height="192" /></a><p class="wp-caption-text">Final screen with graph representation.</p></div>
<p>In our example we need to figure out a transparent background for UIWebView. On the parent View Container we have additional controls (buttons, labels) and already defined background. The following code will set this:</p>
<pre class="wp-code-highlight prettyprint">[[webView graphWebView] setBackgroundColor:[UIColor clearColor]];&amp;lt;/code&amp;gt;

[[webView graphWebView] setOpaque:NO];
</pre>
<h3>Example</h3>
<p>MentorMateDemoJS is a simple application example which uses the settings described above. It is a view-based application that shows a graphical representation of some data. Application behavior consists of a predefined call with formatted data to Vvidget Service<sup>[5]</sup> for graph generation. The service returns an image that is loaded in UIWebView Design Resource. The update button is added to resend the graph request and update view.</p>
<p><span style="text-decoration: underline;">The Problem</span>: Graph image requires some time for loading.</p>
<p><span style="text-decoration: underline;">Our Decision</span>: Notify user of graph loading process by showing a progress bar during load time.</p>
<p>To achieve this, we use UIWebView with local resource of two JavaScript functions. One to show/hide notification messages and the other to visualize a progress bar during loading time. In the provided sources you can find all these steps implemented.</p>
<p><a href="http://mentormate.com/wp-content/uploads/2010/03/MentorMateDemoJS.zip">Sources of the example iPhone application</a></p>
<h3>References</h3>
<ol>
<li>iPhone SDK Articles, <a title="http://www.iphonesdkarticles.com/2008/08/uiwebview-tutorial.html" rel="nofollow" href="http://www.iphonesdkarticles.com/2008/08/uiwebview-tutorial.html" target="_blank">UIWebView Tutorial</a>, August 19, 2008</li>
<li>iPhone Development Blog, <a title="http://iphoneincubator.com/blog/windows-views/uiwebview-revisited" rel="nofollow" href="http://iphoneincubator.com/blog/windows-views/uiwebview-revisited" target="_blank">UIWebView – Loading External Images and CSS</a>, February, 2009</li>
<li>StackOverflow, <a title="http://stackoverflow.com/questions/843820/iphone-uiwebview-local-resources-using-javascript-and-handling-onorientationchang" rel="nofollow" href="http://stackoverflow.com/questions/843820/iphone-uiwebview-local-resources-using-javascript-and-handling-onorientationchang" target="_blank">iPhone UIWebView local resources using Javascript and handling onorientationChange</a></li>
<li>Developer Forum, <a title="https://devforums.apple.com/message/32282#32282" rel="nofollow" href="https://devforums.apple.com/message/32282#32282" target="_blank">UIWebView and JavaScript</a>, 2009</li>
<li>Vvidget, <a title="http://www.vvidget.org/service/manual/index.html" rel="nofollow" href="http://www.vvidget.org/service/manual/index.html" target="_blank">Vvidget Server™ Reference Manual </a>, 2010</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://mentormate.com/blog/iphone-uiwebview-class-local-css-javascript-resources/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

