Blog

Using Google Charts API inside jQuery .load()

September 3rd, 2012

I recently tried using the Google Charts API on a page which had been .load()ed in jQuery (AJAX) and I was running into a weird problem: loading the chart would cause the page to redirect to the code for the chart, resulting in an abort of the actual page loading and a blank screen.

I used the following code which fixes the issue, note the use of the callback parameter in google.load().


<div id="chart" style="width: 100%; height: 500px;"></div>

<script type="text/javascript">

	google.load("visualization", "1", {packages:["corechart"], callback:drawChart});

	function drawChart() {
 		var jsonData = $.ajax({
			url: "generate_data.php",
			dataType:"json",
			async: false
        	}).responseText;

		var options = {
			title: 'Your Chart Title'
		};

		var data = new google.visualization.DataTable(jsonData);

        var chart = new google.visualization.LineChart(document.getElementById('chart'));
        chart.draw(data, options);
      }
</script>

JEDownloader

April 23rd, 2012

When working on iOS projects, there are some tasks that you find yourself doing over and over. For example, using NSURLRequest and NSURLConnection to call an API, or download a file. There are many heavyweight libraries out there (e.g. ASIHTTPRequest) which are great for larger projects, but I wanted to produce a small and lightweight library that encapsulates the common behaviours needed to access a web service, download a small image, etc. that are core activities within most modern apps.

You can find JEDownloader on GitHub here: https://github.com/jonathanellis/jedownloader

Generating non-retina images automatically for iOS

April 9th, 2012

Developing apps, I found I spent a lot of time in Photoshop making minor adjustments and then having to export them at both retina (@2x) and standard resolutions. After unsuccessfully searching online to try and find any scripts, plugins or apps to generate the non-retina images automatically, I took the plunge and wrote one myself.

Simply export all of your images as PNGs from Photoshop (or other image editing package) at retina resolution, with the @2x suffix, and this PHP (CLI) script will scale the images to 50% and copy them without the @2x suffix. Unlike many other image resizing scripts out there, my version fully supports alpha transparency.

Read the rest of this entry »

Building a Twitter Sentiment Classifier

February 15th, 2012

For my final year project as part of my MEng Computer Science degree at UCL I conducted a sociometric analysis of London-based tweeters. I looked at correlating sentiment (i.e. the positivity/negativity — or happiness/sadness) of 32 million tweets for some 260,000 Twitter users against a number of other factors.

In order to determine the sentiment of each tweet, it was necessary to classify as either 1 corresponding to “positive”, -1 corresponding to “negative” or 0 corresponding to “unclassified” or “neutral”.

In the literature there have been many (many!) approaches to sentiment classification. The simplest method employs a simple word-counting algorithm whereby the word in each text fragment (in this case, tweet) is compared against a list of positive and negative words. The occurrence of each type of word is counted, and those tweets which contain more positive words than negative are classified as positive, and vice-versa for negative words.

Read the rest of this entry »

performSelector with Primitives, Enums, Structs and Objects

February 10th, 2012

Most iOS developers will at some point have come across performSelector: and its’ variants, which provide much greater flexibility than standard method calling, and is often used to defer method call decisions to runtime. Particularly when combined with NSSelectorFromString(), it becomes an incredibly powerful meta-programming tool.

However, one thing that performSelector: and its variants don’t handle well at all are primitives, enums and structs (hereon referred to simply as “primitives”). Because performSelector: heavily uses the generic id type which is only suitable for objects. Therefore, only objects can be specified as arguments, and only objects are returned (if a primitive is returned, it just reverts to nil).

However, there are often times when you are working not only with just objects, but also structs, enums and primitives both as arguments and as return values. I’ve therefore written a wrapper around NSInvocation (a more powerful set of tools than performSelector:) which handles passing pointers to primitives, and returning pointers to primitives.

Read the rest of this entry »

Detecting touches on MKOverlayView

January 27th, 2012

So you’ve used MKOverlayView to add a Google Map to your app and you’ve added to it a series of MKOverlays and implemented mapView:viewForOverlay: which returns the corresponding MKOverlayView for your overlay. But the problem is that now you want to detect touch events on the MKOverlayView so that an action can be taken depending on which view is tapped.

This seems to be one of those questions that many have asked, but has not really received any successful answer! Well, there is actually a way to detect touches on an MKOverlayView, and it’s surprisingly simple!

Read the rest of this entry »

Working with SQLite in Objective-C/iOS

January 26th, 2012

The Cocoa API provides Cocoa Touch which is a great (and powerful!) abstraction layer over the underlying SQLite libraries, but sometimes you just want to keep things simple with simple operations directly on a database. Unfortunately, the SQLite libraries themselves are implemented in C, so not object-oriented, and are fairly complex.

With that in mind, for a recent project I developed my own small wrapper around the underlying SQLite libraries to allow me to perform queries and get back responses as Objective-C objects rather than C primitives. I’m not going to reproduce all of the code here, but I will provide a starter that will get you up & running and give you most of the functionality you would need. Of course there is more you can do (particularly error handling) that I don’t get into here, but feel free to post any improvements you may have in the comments.

Read the rest of this entry »

Multithreading with CocoaAsyncSocket

December 2nd, 2011

CocoaAsyncSocket is a fantastic socket library for Cocoa (iOS and MacOS). It comes in RunLoop and GCD flavours, but either way its asynchronous, which means that you can fire off and receive data without your code blocking (which means your code can get on with other things, like providing GUI interactivity, etc.)

However, one of the problems I found with CocoaAsyncSocket (using the RunLoop version) was that despite its asynchronous nature, when building a server application handling multiple sockets that also required interactivity, managing a large number of sockets and packets could cause significant slowdown to my app.

Read the rest of this entry »

How to enable Zeroconf for Apache

November 14th, 2011

Zeroconf is a great way for hosts and services to advertise themselves on the network. Unfortunately, Apache doesn’t provide support for advertising itself as an HTTP service out-of-the-box, so you’ll need to do some work to get it working.

There are two methods, the first use mod_dnssd which is good because it ties directly into Apache, so when Apache runs, it advertises, but when Apache is down (for whatever reason), then it stops advertising. It also means any changes you make to your Apache configuration (e.g. port) are automatically reflected in the advertisement.

The second method uses the built-in Zeroconf daemon in *nix called Avahi. Avahi is similar to Bonjour/mDNSResponder on Mac OS X and is used to advertise services to the network. By creating a basic XML file and attaching it to Avahi, your service will be advertised as long as the system is running. Using Avahi gives you the flexibility to advertise any service (not just Apache/HTTP) and is also a lot simpler to get up and running. I will outline both approaches here.

Read the rest of this entry »

Hello world!

November 13th, 2011

Hello, hello and welcome to my blog!

I intend to keep this blog updated with information about what projects I’m working on, and will also be offering walkthoughs, tips and tricks to both beginners and seasoned developers.