February 11, 2010

Android Development – Setting your location using latitude, longitude in the emulator

Filed under: Android — Tags: , , , — wicheda @ 1:50 pm

This is a quick post since it took me a little while to figure this one out. If you are using location services in your application and testing them on the emulator, you might be wondering how to set your location. In fact the iPhone doesn’t provide for this and defaults you to Cupertino (Apple’s HQ) which can be annoying.

But Google have thought a bit more about it. All you need to know is the port your emulator is running on (it’ll be in the title bar of the emulator), typically 5554.

Fire up a terminal and away you go, let’s say you want to set it around London, UK. That is roughly 52,0 in lat long so connect to the port through Telnet and enter:

telnet localhost 5554
geo fix 52 0 0

The final 0 is optional and sets your altitude. Easy, and now in your application your callback method onLocationChanged should be called.

February 6, 2010

iPhone Development – Gradient background UIViews

Filed under: iphone — Tags: , , , — wicheda @ 3:50 pm

Here’s a neat little tip I’ve found can spruce up your applications easily. It’s very easy to create a UIView with a solid background colour, but in our new rainbow coloured Web 2.0 shiny iPhone world, gradients are called for. You could of course create a gradient image and set the background image of the view, but this has several drawbacks:

  • For every different sized view, you either need to re-cut the gradient image or scale it.
  • It is a waste of space in your application, plus gradient images are often quite big files.
  • You need an image for each colour.
  • It is relatively expensive to load and render an image as a background for a view.

However using a bit of trickery, you can create a general GradientView class which extends UIView and overrides drawRect to achieve a gradient background. So I present GradientView:

//
//  GradientView.m
//  evilrockhopper
//
//  Created by Daniel Wichett on 10/12/2009.
//
//  Extension of UIView to show a gradient, generally used as a background on other views.
//  Mirrored indicates a gradient that is colour1 -> colour2 -> colour1. Non-mirrored simply goes from colour1 -> colour2.

#import "GradientView.h"

@implementation GradientView

@synthesize mirrored;

- (void)drawRect:(CGRect)rect
 {
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGGradientRef glossGradient;
    CGColorSpaceRef rgbColorspace;
    size_t numLocations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };

    //Two colour components, the start and end colour both set to opaque.
    CGFloat components[8] = { startRed, startGreen, startBlue, 1.0, endRed, endGreen, endBlue, 1.0 };

    rgbColorspace = CGColorSpaceCreateDeviceRGB();
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, numLocations);

    CGRect currentBounds = self.bounds;
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds)/2.0);
    CGPoint bottomCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds));

    if (!mirrored)
    {
        // draw a gradient from top to bottom centred.
        CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, bottomCenter, 0);
    }
    else
    {
        // draw a gradient from top to middle, then reverse the colours and draw from middle to bottom.
        CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0);
        CGFloat components2[8] = { endRed, endGreen, endBlue, 1.0, startRed, startGreen, startBlue, 1.0 };
        CGGradientRelease(glossGradient);
        glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components2, locations, num_locations);
        CGContextDrawLinearGradient(currentContext, glossGradient, midCenter, bottomCenter, 0);
    }

    // Release our CG objects.
    CGGradientRelease(glossGradient);
    CGColorSpaceRelease(rgbColorspace);
}

// Set colours as component RGB.
- (void) setColours:(float) _startRed:(float) _startGreen:(float) _startBlue:(float) _endRed:(float) _endGreen:(float)_endBlue
{
	startRed = _startRed;
	startGreen = _startGreen;
	startBlue = _startBlue;

	endRed = _endRed;
	endGreen = _endGreen;
	endBlue = _endBlue;
}

// Set colours as CGColorRefs.
- (void) setColoursWithCGColors:(CGColorRef)color1:(CGColorRef)color2
{
	const CGFloat *startComponents = CGColorGetComponents(color1);
	const CGFloat *endComponents = CGColorGetComponents(color2);

	[self setColours:startComponents[0]:startComponents[1]:startComponents[2]:endComponents[0]:endComponents[1]:endComponents[2]];
}

- (void)dealloc
{
    [super dealloc];
}

@end

And the header file GradientView.h as so:

//
//  GradientView.h
//  evilrockhopper
//
//  Created by Daniel Wichett on 10/12/2009.
//

#import <UIKit/UIKit.h>

@interface GradientView : UIView
{
    float startRed;
    float startGreen;
    float startBlue;

    float endRed;
    float endGreen;
    float endBlue;

    BOOL mirrored;
}

@property (nonatomic) BOOL mirrored;

- (void) setColoursWithCGColors:(CGColorRef)color1:(CGColorRef)color2;
- (void) setColours:(float) startRed:(float) startGreen:(float) startBlue:(float) endRed:(float) endGreen:(float)endBlue;

@end

So an example usage could be:

    GradientView *redToBlack = [[GradientView alloc] initWithFrame:CGRectMake(0,0,320,50)];
    [gradientBg setColoursWithCGColors:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0].CGColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0].CGColor];

This creates a vertical gradient from red to black. If you wanted it to go from red to black to red, you can just set redToBlack.mirrored = YES.

It currently ignores the alpha value of the colours passed in, this would be trivial to add of course if required. If you want to experiment with other types of gradients, read the documentation for CGGradientCreateWithColorComponents. Feel free to use the code above, you can download the source by clicking on the menu in the top right of each code snippet.

February 5, 2010

iPhone Development – XCode quirks, bugs and provisioning problems

Filed under: iphone — Tags: , — wicheda @ 4:36 pm

Although XCode is not the worst IDE in the world, it is definitely buggy.

Countless times I have come up against a bug, almost always to do with provisioning profiles, build failures or sync issues which make no sense whatsoever. To prevent you banging your head constantly against a wall, when a problem seems totally illogical I always take these steps.

  1. Stop the app
  2. Delete from the device / simulator as appropriate
  3. Restart XCode
  4. Clean all builds
  5. Pray

If that doesn’t work:

  1. As above
  2. Restart Mac
  3. Restart iPhone

Trust me, more often than not that will fix all sorts of problems.

January 29, 2010

iPhone Development – Keeping the UI responsive and a background thread pattern

Filed under: iphone — wicheda @ 5:13 pm

One of the biggest mistakes you can make as an iPhone developer is running any long tasks on the main thread.

Let’s take a concrete example, say you want to retrieve some data from a web server somewhere. If you run this method on the main thread, all user interface activity is blocked until it finishes. The app will cease to respond to any user input (bar a reset / program kill) and the screen will freeze. Not good.

As any programmer who has worked with interfaces should know, all long running tasks need to be run on a background thread. One more vital point is that any changes to the user interface (ANY changes to a UI component) must always be run back on the MAIN thread. That is because those components are not thread safe and should not be changed by multiple threads. So let’s say after you have fetched your data, you want to change a label on the screen, this must be done on the main thread. I’ve come up with a little pattern I use in these cases, for example:

- (IBAction) performLongTaskOnClick: (id)sender
{
    statusMessage.text = @"Running long task";
    [self performSelectorInBackground:@selector(performLongTaskInBackground) withObject:nil];
}

- (void) performLongTaskInBackground
{
    // Set up a pool for the background task.
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    // perform some long task here, say fetching some data over the web.
    //...

    // Always update the components back on the main UI thread.
    [self performSelectorOnMainThread:@selector(completeLongRunningTask) withObject:nil waitUntilDone:YES];

    [pool release];
}

// Called once the background long running task has finished.
- (void) completeLongRunningTask
{
    statusMessage.text = @"Finished long task";
}

To summarise, we have 3 methods, performLongTaskOnClick might be called in response to a button click say, and kicks off the long running task on a new thread with performLongTaskInBackground. Then when that finished it calls completeLongRunningTask back on the main thread. Notice that we set up an autoreleasepool in the background method, you must do that or you will be leaking memory from that thread. Now you should keep your iphone app nice and responsive.

iPhone Development – Reverse Geocoding

Filed under: iphone — Tags: , , — wicheda @ 4:48 pm

A rather nifty new feature of the iPhone SDK is the ability to reverse geocode a location. Some of you may be wondering what normal geocoding is! Well, geocoding is turning an address into a latitude and longitude. So reverse geocoding is, without surprise, turning a latitude and longitude into an address.

So, in combination with the normal iPhone lookup services, you can find out the lat,long of someone and then convert that into a friendly place name such as the town they are in. It’s very easy to do, firstly you need to implement the MKReverseGeocoderDelegate protocol in your chosen class which has two callbacks, one for when a reverse geocode is successful and one for when there is an error:

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder  didFindPlacemark:(MKPlacemark *)placemark;
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error;

So we create and initialise a reverse Geocoder with the following code. Note the delegate is set to self so the methods defined above should be places in the same class. locationToLookup defines where the location name we want to look up is (as a latitude, longitude coordinate).

        // use whatever lat / long values or CLLocationCoordinate2D you like here.
        CLLocationCoordinate2D locationToLookup = {52.0,0};
        MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:locationToLookup];
        reverseGeocoder.delegate = self;
        [reverseGeocoder start];

The first time I tried this I got the very cryptic error message below:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[LBSGAddressComponent _mapkit_cache_heapTime]: unrecognized selector sent to instance 0x166c00'

After a lot of head scratching I realised this is because you must only ever have one reverse geocoder running at once. So I made the reverse geocoder a class variable (to keep track of it) and added:

        if (reverseGeocoder != nil)
        {
            // release the existing reverse geocoder to stop it running
            [reverseGeocoder release];
        }

        // use whatever lat / long values or CLLocationCoordinate2D you like here.
        CLLocationCoordinate2D locationToLookup = {52.0,0};
        MKReverseGeocoder *reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:locationToLookup];
        reverseGeocoder.delegate = self;
        [reverseGeocoder start];

This will stop any existing lookups. And so if successful your callback method will be called with an instance of MKPlacemark that represents where your lookup is. Easy.

October 19, 2009

Comment spammers scum, Wordpress and re-CAPTCHA

Filed under: Wordpress — Tags: , , , — wicheda @ 12:20 pm

The spam comments on this site were progressively getting worse and worse. It’s a ridiculous situation since all comments are only show after approval so not a single one was ever being shown on the site. But it still takes time to monitor and becomes more annoying as time goes on. This morning I had already had 10 and so decided enough is enough.

I installed the very excellent re-CAPTCHA. This captcha software is a bit different for several reasons. Firstly it has a Wordpress plugin which took 5s to install. Secondly it helps organisations digitise books, a very honourable motive. To install you just need to:

  1. Download the archive for the plugin at http://recaptcha.net/plugins/wordpress/
  2. Unzip the archive and copy the whole wp-recaptcha directory to your wp-content/plugins/ directory under Wordpress.
  3. Register at http://recaptcha.net/ , and enter your site domain to recieve a public / private key.
  4. Enable re-CAPTCHA under the Plugins section of your Wordpress admin and enter the two keys under the plugin configuration.
  5. Spend less time dealing with spam.

So how does re-CAPTCHA work then? Captchas rely on testing if you are human by quizzing you on something that should be easy for a human and relatively hard for a computer to solve. OCR, i.e. recognising the image of a word as text can be extremely hard for computers if the page is distorted, grainy or badly printed. So re-CAPTCHA asks you to OCR two words. Why two? Well one is already known since it has been confirmed by other users (how else would re-CAPTCHA know your answer was correct), the other is not known and so you are providing a useful service helping a document be recognised (and providing new captcha answers for other users). Simple but ingenius.

October 6, 2009

Django Admin – An introduction and then applying a textarea instead of a text input

Filed under: django — Tags: , , — wicheda @ 10:30 pm

Well I thought now was as good a time as any to make my first Django post. One of the best things about Django is its automatic admin interface, built very effectively from just your model objects in the ORM. What a good idea. Generally, even if a webapp isn’t there just to do CRUD, its backoffice tool almost certainly is. And even the most beautiful webapps generally have awful backoffice tools. Why? Because who the hell wants to spend their time writing a boring CRUD interface 3 people are going to see! And when does business even consider time for that sort of thing, non-customer-facing mean non-thought-about.

Here’s an example of the backoffice tool for What Ales You:

Choose beers Edit a beer


Pretty isn’t she (I presume admin interfaces have the same gender as ships?). To activate the admin interface ensure under settings.py that django.contrib.admin is included under INSTALLED_APPS and that in urls.py you have the relevant lines (they are marked with comments) included and uncommented. Then add a file called admin.py to each project where you wish to administer model objects. For example, mine for my beer project is currently:

from alephile.beer.models import Brewery, Beer, BeerRating, Media, BeerComment, UserProfile
from django.contrib import admin

admin.site.register(Brewery)
admin.site.register(Beer)
admin.site.register(BeerRating)
admin.site.register(BeerComment)
admin.site.register(Media)
admin.site.register(UserProfile)

This tells Django which model objects I want to be able to edit. You’ll notice that my beer description is a normal text input which is a bit annoying, what I’d prefer is a textarea. To achieve this I need to add a ModelAdmin and a ModelForm. The ModelAdmin is a class telling Django how to administer a particular model object. Each ModelAdmin contains a ModelForm which tells it how to create the editing form. By default this just includes all editable fields, but now we are going to override the ModelAdmin and tell it to use a custom form instead. We end up with:

from alephile.beer.models import Brewery, Beer, BeerRating, Media, BeerComment, UserProfile
from django.contrib import admin
from django import forms

class BeerModelForm( forms.ModelForm ):
    description = forms.CharField( widget=forms.Textarea )
    class Meta:
        model = Beer

class BeerAdmin( admin.ModelAdmin ):
    form = BeerModelForm

admin.site.register(Brewery)
admin.site.register(Beer, BeerAdmin)
admin.site.register(BeerRating)
admin.site.register(BeerComment)
admin.site.register(Media)
admin.site.register(UserProfile)

and an interface that looks like so. Easy when you know how but something I initially struggled to find out.

A better description field

September 30, 2009

Localsalefinder – BView’s iPhone App

Filed under: iphone — wicheda @ 9:53 pm

Eating OutLondon Bridge results


To accompany my technical ADD, as part of my job at BView we’ve now released another product in another language entirely, an iPhone application.

Called Localsalefinder and powered by the BView API, it’s a fantastic iPhone app for finding local offers and redeeming them without printing out or clipping coupons.

Once we finish off all the finishing touches to the vouchers, this is going to be one powerful app. It includes integrations from the main restaurant booking engines (Toptable / Livebookings and more) plus our own vouchers and those sourced by 3rd parties.

If you’ve got an iPhone check it out! In terms of Objective-C development, I’m going to start a series of articles about the common problems and pitfalls of iPhone development.

Dan.

September 19, 2009

What Ales You

Filed under: django — Tags: , — wicheda @ 5:22 pm

What Ales You Logo

Just a quick post to explain my lack of updates recently, I’ve been building another site in my spare time called What Ales You.

Conceptualised, designed and developed all by me, it’s a beer rating site (actually my girlfriend did the logo and name to be fair). Anyone can add, comment on and rate beers. It was written in Django, an amazing Python based web framework that takes the “convention over configuration” concept even further. In roughly 10 evenings I’ve built a site in a language I didn’t even know, impressive stuff. While some of that might be my genius, at least partial credit has to go to the excellent features of Django.

Technology ADD – On Django and Python

Filed under: General development — Tags: , , — wicheda @ 5:15 pm

I seem to be suffering from a form of ADD recently. Hopefully it’s not the kind I’ll need Ritalin for although I haven’t actually checked with a doctor yet. But this is a special kind of ADD, technology ADD.

After living a lifetime stuck in a bit of a technology trap, using what everyone else seemed to be using and what recruiters wanted to see on your CV I’ve suddenly been opened up to a world of promising technologies. In the past month I’ve not only carried on writing in Ruby on Rails but have also started using Django, a full on web framework which is powered by Python.

A lot of people, my previous self included, would tend to call these new web frameworks lightweight and not suitable for really serious applications. I still haven’t quite made my mind up if this statement holds any merit but maybe a better question would be does the application you are working on have to be “serious” anyway. By serious here I mean seriously built. Does it need to scale horizontally at the flick of a switch, will you be dealing with billion row tables and processing 6,000 requests a second? They’ll probably answer that one day they might. That’s true, although only if they manage to launch without sucking up their funds building a project in a serious fashion.

Case in point about two years ago I tried a personal project in Java. Java’s what I know best so it seemed a sensible option. Professionally I work on a robust codebase with a great Spring / Hibernate base, full on integration testing, custom authentication code and a beautiful Ant based build environment. But I also forgot that the environment took 5 smart people 2 months to set up. Admittedly I now knew the general setup but it was hard work being disciplined and trying to write so much from scratch, in my SPARE TIME. The initial setup must have taken 6 months. I had lost all enthusiasm for it by that point.

Two weeks ago I started a project in Django. I didn’t even know Python. In a week of spare time work I’ve got a fully working database model, authentication framework, frontend with templating. I’d estimate I might have spent about the same time drawing pretty pictures for the frontend as writing backend code. That is truly astounding. I was impressed with Ruby on Rails but this is another level. Webapps can be coded in less time than it would take you to draw a UML diagram or database hierarchy.

The best thing is it has brought back my enthusiasm and hopes for personal projects. When writing something in your spare time, the best thing you can do is finish something. Don’t obsess over perfect theoretical programming practices, scalability and how you’re going to track sessions across multiple servers when you haven’t even got something launched. Obviously if you’ve got £1,000,000 and know your app is going to be huge, build everything as well as you can. For the rest of us I’d suggest speed is the more important factor.

Older Posts »