<?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>Spice World! &#187; Objective C</title>
	<atom:link href="http://www.cyberspice.org.uk/blog/category/geek/programming/objective-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cyberspice.org.uk/blog</link>
	<description>The life and times of a jet setting software engineer!</description>
	<lastBuildDate>Fri, 23 Jul 2010 10:25:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using PHP sessions from Objective C and Cocoa</title>
		<link>http://www.cyberspice.org.uk/blog/2009/06/06/using-php-sessions-from-objective-c-and-cocoa/</link>
		<comments>http://www.cyberspice.org.uk/blog/2009/06/06/using-php-sessions-from-objective-c-and-cocoa/#comments</comments>
		<pubDate>Sat, 06 Jun 2009 15:34:34 +0000</pubDate>
		<dc:creator>cyberspice</dc:creator>
				<category><![CDATA[Geek]]></category>
		<category><![CDATA[Objective C]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.cyberspice.org.uk/blog/?p=197</guid>
		<description><![CDATA[A little while ago I wrote a post &#8220;Using PHP sessions from Java&#8221;.  This is a companion post looking at the same issue but using Objective C and Apple&#8217;s Cocoa Framework which are used both in OS X and in iPhone OS.

I suggest you read the start of my Java article for detail on [...]]]></description>
			<content:encoded><![CDATA[<p>A little while ago I wrote a post <a href="http://www.cyberspice.org.uk/blog/2009/03/19/using-php-sessions-from-java/">&#8220;Using PHP sessions from Java&#8221;</a>.  This is a companion post looking at the same issue but using Objective C and <a href="http://developer.apple.com/referencelibrary/Cocoa/index.html">Apple&#8217;s Cocoa Framework</a> which are used both in OS X and in iPhone OS.</p>
<p><span id="more-197"></span><br />
I suggest you read the start of my Java article for detail on PHP sessions but I&#8217;ll summarise it here.  PHP session handling is a way of having persistent or stateful data across HTTP requests.  PHP manages this by using a session ID stored in a cookie. PHP&#8217;s <em>session_start()</em> call will parse the value of the <strong>&#8220;Cookie:&#8221;</strong> HTTP request header field looking for a value similar to <strong>PHPSESSID=b330c9b604d371e5c88cd0919990f41c</strong>. It will use the session ID value to retrieve the appropriate session data and deserialise any stored values and store them in the $_SESSION global array variable.  If the <strong>PHPSESSID</strong> does not exist <em>session_start()</em> will create a new empty session (and <strong>$_SESSION</strong>) and use <strong>&#8220;Set-Cookie:&#8221;</strong> in the HTTP response to pass the session ID cookie to the browser so it can be returned on the next request. The session cookie is valid for the lifetime of the browser.</p>
<p>The Core Framework has a class, <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/index.html"><em>NSURLConnection</em></a>, that is very similar to Java&#8217;s <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/net/HttpURLConnection.html"><em>HttpURLConnection</em></a>.  NSURLConnection provides two class methods one which provides an asynchronous connection and one that will synchronously load the data from a remote server. </p>
<p> <em>connectionWithRequest:delegate:</em> is a factory method that returns an instance of <em>NSURLConnection</em> that represents the current connection.  It takes two arguments, a pointer to an <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURLRequest"><em>NSURLRequest</em></a> object and a pointer to a delegate object that is called back when certain events happen such as an error, data being received, connection completed and so on.</p>
<p><em>sendSynchronousRequest:returningResponse:error</em> is the other method provided by the class for connections.  As before it takes a pointer to a <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURLRequest"><em>NSURLRequest</em></a> object.  It also takes a pointer to a variable in which to return a pointer to an <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSError_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSError">NSError</a> object.  This will be nil if no error occurs.  The return argument is a pointer to an <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSData">NSData</a> object containing the data in the body of the document returned by the server.  Unfortunately it provides no way of obtaining any header information returned by the server so I will be using  <em>connectionWithRequest:delegate:</em> for the rest of the post.</p>
<p>Below is the interface definition for a class that uses <em>NSURLConnection</em> to connect to a remote server.  There are three instance variables, the connection object, the data object (that will contain the body of the response from the server), and an array of cookies.  The five new methods (other than those inherited from <em>NSObject</em>) is one that makes a connection to the server and four delegate methods.  The delegate methods will be called by the connection object when an event happens.  There are other delegate methods that can be implemented but this is the minimal set required for a connection.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@interface</span> MyConnection <span style="color: #002200;">:</span> <span style="color: #400080;">NSObject</span> <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// A pointer to the connection.</span>
	<span style="color: #400080;">NSURLConnection</span> <span style="color: #002200;">*</span>_connection;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// A pointer to the received data object.</span>
	<span style="color: #400080;">NSMutableData</span> <span style="color: #002200;">*</span>_data;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// The stored cookies.</span>
	<span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_cookies;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Initialise the cookies property.</span>
<span style="color: #a61390;">@property</span> <span style="color: #002200;">&#40;</span>noatomic,retain<span style="color: #002200;">&#41;</span> <span style="color: #400080;">NSArray</span> <span style="color: #002200;">*</span>_cookies;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Makes the connection to the remote server and GETs data.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>sendRequest<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>URLString;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Delegate method called if an error occurred during the connection.</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;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Degate method called when a response has been received from the server.  All of the headers</span>
<span style="color: #11740a; font-style: italic;">// will have been received but not the body data yet.</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 didReceiveResponse<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>response;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Delegate method called when body data has been received.  This will be called one or more times.</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 didReceiveData<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>data;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Delegate method called when the entire transaction has completed.</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;
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The implementation of the class follows.  It implements the methods in the interface and the cookies property.  Unlike the Java <em>HttpURLConnection</em> class, which has methods for setting internal data, the body data, header fields, HTTP method and so on are defined in either an <em>NSURLRequest</em> object or returned in an <em>NSURLResponse</em> object as appropriate.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">@implementation</span> MyConnection
&nbsp;
<span style="color: #11740a; font-style: italic;">// Create the cookies property</span>
<span style="color: #a61390;">@synthesize</span> _cookies;
&nbsp;
<span style="color: #11740a; font-style: italic;">// Makes the connection to the remote server and GETs data.</span>
<span style="color: #002200;">-</span> <span style="color: #002200;">&#40;</span><span style="color: #a61390;">void</span><span style="color: #002200;">&#41;</span>sendRequest<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>URLString <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Create a URL to the server</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> URLString<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Make a request (By default this is a GET request with no body).  It is mutable as we need to</span>
	<span style="color: #11740a; font-style: italic;">// Add the cookies to the request.</span>
	<span style="color: #400080;">NSMutableURLRequest</span> <span style="color: #002200;">*</span>request <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;">// If we have cookies, iterate though them and add them to the request.</span>
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>_cookies<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #400080;">NSEnumerator</span> <span style="color: #002200;">*</span>enumerator <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>_cookies objectEnumerator<span style="color: #002200;">&#93;</span>;
		<span style="color: #a61390;">id</span> cookie;
&nbsp;
		<span style="color: #a61390;">while</span> <span style="color: #002200;">&#40;</span>cookie <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>enumerator nextObject<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #002200;">&#91;</span>urlRequest addValue<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>cookie value<span style="color: #002200;">&#93;</span> forHTTPHeaderField<span style="color: #002200;">:</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Cookies&quot;</span><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;">// Make the connection.  The connection will start immediately.  The connection is</span>
	<span style="color: #11740a; font-style: italic;">// asynchronous and the data will be returned via the delegate methods.  nil will be </span>
	<span style="color: #11740a; font-style: italic;">// returned on error.</span>
	_connection <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>request delegate<span style="color: #002200;">:</span>self startImmediately<span style="color: #002200;">:</span><span style="color: #a61390;">YES</span><span style="color: #002200;">&#93;</span>;
	<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>_connection<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #11740a; font-style: italic;">// Create the data object</span>
		_data <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#91;</span><span style="color: #400080;">NSMutableData</span> data<span style="color: #002200;">&#93;</span> retain<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;">// Delegate method called if an error occurred during the connection.</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>
	<span style="color: #11740a; font-style: italic;">// We got an error.  Just for this example log it.</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Error: %@&quot;</span>, <span style="color: #002200;">&#91;</span>error localizedDescription<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Then tidy up</span>
	<span style="color: #002200;">&#91;</span>_connection release<span style="color: #002200;">&#93;</span>;
	<span style="color: #002200;">&#91;</span>_data release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Degate method called when a response has been received from the server.  All of the headers</span>
<span style="color: #11740a; font-style: italic;">// will have been received but not the body data yet.</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 didReceiveResponse<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSURLResponse</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>response <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// We're doing HTTP so we will have gotten a NSHTTPURLResponse!</span>
	httpResponse <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSHTTPURLResponse</span><span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>response retain<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Check the status code and respond appropriately.</span>
	<span style="color: #a61390;">switch</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>httpResponse statusCode<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
		<span style="color: #a61390;">case</span> <span style="color: #2400d9;">200</span><span style="color: #002200;">:</span> <span style="color: #002200;">&#123;</span>
			<span style="color: #11740a; font-style: italic;">// Got a response so extract any cookies.  The array will be empty if there are none.</span>
			<span style="color: #400080;">NSDictionary</span> <span style="color: #002200;">*</span>theHeaders <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span>httpResponse allHeaderFields<span style="color: #002200;">&#93;</span>;
			<span style="color: #400080;">NSArray</span>      <span style="color: #002200;">*</span>theCookies <span style="color: #002200;">=</span> <span style="color: #002200;">&#91;</span><span style="color: #400080;">NSHTTPCookie</span> cookiesWithResponseHeaderFields<span style="color: #002200;">:</span>theHeaders forURL<span style="color: #002200;">:</span><span style="color: #002200;">&#91;</span>response URL<span style="color: #002200;">&#93;</span><span style="color: #002200;">&#93;</span>;
&nbsp;
			<span style="color: #11740a; font-style: italic;">// Save any cookies</span>
			<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span><span style="color: #002200;">&#91;</span>theCookies count<span style="color: #002200;">&#93;</span> &gt; <span style="color: #2400d9;">0</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
				self._cookies <span style="color: #002200;">=</span> theCookies;
			<span style="color: #002200;">&#125;</span>
		    <span style="color: #a61390;">break</span>;
		<span style="color: #002200;">&#125;</span>
&nbsp;
		<span style="color: #a61390;">default</span><span style="color: #002200;">:</span>
			<span style="color: #a61390;">break</span>;
	<span style="color: #002200;">&#125;</span>
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Initialised the received data which will contain the body of the response and will be</span>
	<span style="color: #11740a; font-style: italic;">// populated via the connection:didReceiveData call below.</span>
	<span style="color: #002200;">&#91;</span>_data setLength<span style="color: #002200;">:</span><span style="color: #2400d9;">0</span><span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Delegate method called when body data has been received.  This will be called one or more times.</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 didReceiveData<span style="color: #002200;">:</span><span style="color: #002200;">&#40;</span><span style="color: #400080;">NSData</span> <span style="color: #002200;">*</span><span style="color: #002200;">&#41;</span>data <span style="color: #002200;">&#123;</span>
	<span style="color: #11740a; font-style: italic;">// Append the data received to our data</span>
	<span style="color: #002200;">&#91;</span>_data appendData<span style="color: #002200;">:</span>data<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #11740a; font-style: italic;">// Delegate method called when the entire transaction has completed.</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>
	<span style="color: #11740a; font-style: italic;">// We have got everything so no longer need the connection so release it.</span>
	<span style="color: #002200;">&#91;</span>_connection release<span style="color: #002200;">&#93;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Do something with the data.</span>
	NSLog<span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">@</span><span style="color: #bf1d1a;">&quot;Data: %@&quot;</span>, _data<span style="color: #002200;">&#41;</span>;
&nbsp;
	<span style="color: #11740a; font-style: italic;">// Then release that too.</span>
	<span style="color: #002200;">&#91;</span>_data release<span style="color: #002200;">&#93;</span>;
<span style="color: #002200;">&#125;</span>
&nbsp;
<span style="color: #a61390;">@end</span></pre></div></div>

<p>The <em>sendRequest</em> method creates a URL and then creates an <em>NSMutableURLRequest</em> object.  If you are carrying out a GET request with default data you can use an <em>NSURLRequest</em> object.  But in order to add cookies to the request you have to use the mutable sub-class of <em>NSURLRequest</em>.  </p>
<p>Unlike in Java, the Cocoa framework is nice and provides an <em>NSHTTPCookie</em> class that represents cookie data.  There could be more than one cookie and so an array is used to store them.  If the array exists its contents are iterated over and the cookies are added to the request object.  When an instance of MyConnection is first created the value of the <em>_cookies</em> variable is <strong>nil</strong>.  So on the first connection no cookies are sent to the server.  This causes PHP&#8217;s <em>session_start()</em> to create a session cookie which is returned in the response.  Finally the method creates a connection for the specified URL with the supplied request.  The connection is asked to connect immediately.</p>
<p>If there is an error trying to contact the server due to a network issue, bad domain or similar, the <em>connection:didFailWithError</em> delegate method will be called with the error object.  Note that all the delegate methods are called with the connection as the first argument.  This allows you to use the same delegate for multiple connections.</p>
<p>Once a connection has been made and the response headers have been read the <em>connection:didReceiveResponse</em> method is called.  The response returned is typically a sub-class of <em>NSURLResponse</em>.  The sub-class depends upon the type of request that has been made.  This example is assuming that an HTTP request is made so an <em>NSHTTPURLResponse</em> will be returned.  The response object is cast and the status code examined.  If its something other than <strong>200</strong> i.e. <strong>OK</strong> then that can be handled at this point.  The example ignores all statuses other than <strong>200</strong>.</p>
<p>Unlike Java, Cocoa does a lot of the work for getting the cookies from the header.  <em>connection:didReceiveResponse</em> calls a method that returns an <em>NSDictionary</em> of header name and value pairs.  It then passes this to the <em>NSHTTPCookie</em> static method <em>cookiesWithResponseHeaderFields:forURL</em> to obtain an array of cookie objects.  These are then stored in the cookies instance property.  Finally the class initialises a data object in which to store the body of the response.</p>
<p>The connection object will call <em>connection:didReceiveData</em> one or more times with the body data.  Once all the data has been read the <em>connectionDidFinishLoading</em> delegate method will be called to indicate this.  The connection instance can now be released and the data processed.</p>
<p>As for the Java version of this code, if the <em>sendRequest</em> is called again, the cookies that were stored the first time a connection was made using <em>sendRequest</em> are sent back to the server.  This causes PHP to re-establish the session.  The life of the session will be consistent with the life of the <em>MyConnection</em> instance used to connect to the server.  To create a new session create a new instance of <em>MyConnection</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cyberspice.org.uk/blog/2009/06/06/using-php-sessions-from-objective-c-and-cocoa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
