Core Library HowTo

This page explains some fundamental features of Core@CodeTitans library. It will mostly explain all the features ported back from iOS to .NET world to speed up development and porting of all those Objective-C running apps out there.

Features:

Reading and writing of *.plist files

One of the formats of *.plist files is an XML. Apple made it quite complex and of course have broken several rules around XML format itself. It's concept is explained here. Long story short - it allows to define a tree of properties. How then open *.plist file:

Let's assume first that the simplest file has following content:

var text = @"<?xml version=""1.0"" encoding=""UTF-8""?>
  <!DOCTYPE plist PUBLIC ""-//Apple Computer//DTD PLIST 1.0//EN"" ""http://www.apple.com/DTDs/PropertyList-1.0.dtd"">
  <plist version=""1.0"">
  <dict>
          <key>Name</key>
          <string>Pawel</string>

          <key>Surname</key>
          <string>Tester</string>

          <key>Street</key>
          <string>Somewhere in the Wild</string>
  </dict>
  </plist>";

Then reading it, will look like:
PropertyList data = PropertyList.Read(text);

// get the name value out of the parsed data:
string name = data["Name"].StringValue;

So in general it looks precisely identical to the way JSON@CodeTitans works providing access to JSON properties.
Of course, if the content is in the file still, we can use the overloaded methods version that accepts TextReader.


Writing is even simpler. Following code stores two properties:

var data = new PropertyList();

data.Add("IsCompleted", true);
data.Add("SynchronizationCount", 1);

Console.WriteLine(data.ToString());

Generates output:

<?xml version="1.0" encoding="utf-16"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <array>
        <key>IsCompleted</key>
        <true />
        <key>SynchronizationCount</key>
        <integer>1</integer>
    </array>
</plist>

Reading and writing of *.string files

Another commonly used file format in MacOS / iOS applications and Apple-specific type is "*.strings". Comparing to *.plist it's flat. It allows only single level of data and both: key name and value must be type of string. There is no XML used to store it anymore, thankfully. In C# it maps then directly into Dictionary<string,string> class (and honestly the StringList class is only a wrapper around it). For more indept look, please refer to the Cocoa documentation.

Let's once again assume that the input data is equal to the following text:

var text = @"
    ""Insert Element"" = ""Insert Element"";
    ""ErrorString_1"" = ""An unknown error occurred."";
    ";

Then reading it and accessing values is also similar:

var reader = StringList.Read(text);

int count = reader.Count;
string errorString = reader["ErrorString_1"];

Using abstracted HTTP communication

In the novadays applications, network communication is so natural, that you probably treat it like a fact, without keeping too much attention to it. It starts actually bother, when designing portable application, that must run on many different version of .NET of course. Unfortunatelly, .NET designers were thinking and thinking and as a result, they created a HttpWebRequest. Then they were thinking even more and more and they made it behaving completely different and exposing different APIs for each .NET platform (i.e.: Windows desktop Framework .NET, Windows Phone OS Silverlight and Mono for Linux/MacOS).

And now, Core@CodeTitans introduced IHttpDataSource interface, which pretends to be a cure for all this problems. It exposes the most common properties and to support the most common scenarios. Also as it's an interface, it allows for further extensibility. The idea of an interface is to express the way data is delivered to the upper layer of software. So focus was kept not only HttpWebRequest, but also (maybe) on some local named pipes or just reading from a file. No restrictions apply here. IHttpDataSource is primely used by Bayeux@CodeTitans as an abstraction for data providers and might be also used by other entities and protocols.

Core@CodeTitans obviously brings the simple implementation of IHttpDataSource that works on all mentioned platforms. The HttpDataSource, allows for synchronous and asynchronous communication of text or binary data. Also since this class is not sealed, it might be customized by for example adding custom data encryption or enforcing some custom protocol.

Simple scenario, let them download something from the Internet:

const string url = "www.google.pl";
const string contentType = "text/html";
const int timeout = 10000; // 10 sec

var downloadRequest = new HttpDataSource(url, Encoding.UTF8, null, Encoding.UTF8, contentType);
downloadRequest.Timeout = timeout;
downloadRequest.DataReceived += WebRequestDataReceived;
downloadRequest.DataReceiveFailed += WebRequestDataReceiveFailed;

// start downloading:
downloadRequest.SendRequestAsync(HttpDataSource.MethodGet, HttpDataSourceResponseType.AsString);

And processing downloaded content:
private void WebRequestDataReceiveFailed(object sender, HttpDataSourceEventArgs e)
{
    Console.WriteLine(e.StatusDescription);
}

private void WebRequestDataReceived(object sender, HttpDataSourceEventArgs e)
{
    Console.WriteLine("Downloaded:");
    Console.WriteLIne(e.StringData);
}

Last edited Jul 16, 2011 at 12:11 AM by FeydRauth, version 7

Comments

No comments yet.