Posts tagged ‘Cocoa’

Signing Amazon Webservices requests

MPoD is retrieving cover art for the currently playing song from Amazon the AWS (Amazon Web Services). From August 15 onwards, all requests made will have to be signed using a secret key that is attached to the registered AWSAccessKeyId. Searching on Google, I found various helpful bits, especially on the learn amazon web services blog. But I didn’t find a ready made implementation that I could use directly from my code.

Based on what I found I created a helper class AWSRequestSigner, that takes a dictionary of parameters to pass to the webservice, and returns a ready made NSURL object that can be used in an NSURLRequest object as in the following example:


NSURL *url = [AWSRequestSigner urlForParameterDict:[NSDictionary dictionaryWithObjectsAndKeys:
                                               @"Sea Of Tears",        @"Title",
                                               @"Eilen Jewell",        @"Artist",
                                               @"ItemSearch",          @"Operation",
                                               @"Images",              @"ResponseGroup",
                                               @"Music",               @"SearchIndex",
                                               @"AWSECommerceService", @"Service",
                                               nil]];

To look at the source code for the AWSRequestSigner class, download it here and open it in XCode.

Application integration using the iPhone SDK

Recently the successful iPhone applications Trein and iNap realized a tight integration, in that you can activate iNap directly from Trein. How to create the integration on the side of the ‘receiving’ application, iNap in this case, is described in the iPhone SDK example ‘Launch Me’.

When integrating applications, you typically want to pass data from one to the other, in this post I’ll describe how the incoming parameters can easily be extracted in the receiving application.

The receiving application will register itself in the OS (for details see the ‘Launch Me’ example), and the result is that it can be activated from another application or from a hyperlink in a webpage. An example of a url that activates another application is:


myapp://myapp?latitude=51.3&longitude=5.2&title=middle of nowhere

In the receiving application the method - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url will be called on the application delegate object, and in this method you need to extract the parameters from the URL. The following code shows how to do that with minimal code. Like Apple’s example, it does a lot of checking to prevent undesired usage, in this case the url parameters must meet the exact specifications (i.e. no missing parameters, no extra parameters) otherwise the request will be ignored.


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
  // You should be extremely careful when handling URL requests.
  // You must take steps to validate the URL before handling it.
  // If any of the validations fails, bail out returning NO.
  if (!url) {
    // The URL is nil. There's nothing more to do.
    return NO;
  }

  // Split the url into two sections, one section with the base url and one with the parameters.
  NSArray *urlSections = [[url absoluteString] componentsSeparatedByCharactersInSet:
              [NSCharacterSet characterSetWithCharactersInString:@"?"]];

  // Check that you actually got a base section and a parameter section.
  if ([urlSections count] != 2) {
    return NO;
  }

  // Check that the base part of the url is what you expect it to be.
  if (![[urlSections objectAtIndex:0] isEqualToString:@"myapp://myapp"]) {
    return NO;
  }

  // Split up the parameter section into a parameter Array.
  NSArray *parameterArray = [[urlSections objectAtIndex:1] componentsSeparatedByCharactersInSet:
              [NSCharacterSet characterSetWithCharactersInString:@"&="]];
  NSEnumerator *parameterEnum = [parameterArray objectEnumerator];
  NSString *parameter, *value;
  // The parameters values that we are expecting.
  NSString *latitude = nil, *longitude = nil, *title = nil;

  // Run through the array of parameters.
  while (parameter = [parameterEnum nextObject]) {
    // Get the value that goes with this parameter, and if it is missing break.
    if ((value = [parameterEnum nextObject]) == nil)
      break;

    // Check the parameter value against the parameters that you support/expect,
    // and assign the matching value to the right variable.
    if ([parameter isEqualToString:@"latitude"])
      latitude = value;
    else if ([parameter isEqualToString:@"longitude"])
      longitude = value;
    else if ([parameter isEqualToString:@"title"])
      title = [value stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    else {
      // An unexpected parameter was passed, bail out.
      latitude = nil;
      longitude = nil;
      title = nil;
      break;
    }
  }

  // Check if we got all parameters (in this case all are mandatory), if not bail out.
  if (latitude == nil || longitude == nil || title == nil) {
    return NO;
  }

  // Use the parameters to do your thing.
  CLLocation *location = [[[CLLocation alloc]
                               initWithLatitude:[latitude doubleValue]
                               longitude:[longitude doubleValue]] autorelease];
  [someObject activateDestination:title location:location];

  return YES;
}

Multiline cells in NSTableView

When you’re building an application that is based on data records, and each record contains a significant amount of data that you want to display in a table row, the space per row may be too small to fit everything. You can add every data element in its own column, but this leads to horizontal scrolling which can be cumbersome. A better solution to this problem is combining multiple data elements in a single cell. I’ll demonstrate how this can be done with a core data based application using data binding through a basic example. The end result of the finalized sample will look like this:


MultilineCell Sample App

For the sample I have made a core data entity named Order with 2 real attributes, orderNumber and orderLine. To combine these two elements into a single cell I add another transient attribute to the entity, named orderNumberLine.


Datamodel.png

Because the attribute is transient it will not become part of a file when you save the model. This is fine because whenever we need the value for the orderNumberLine attribute we will compose it from the orderNumber and orderLine values.

The entity is linked to the class Order, in which we override the standard key-value method orderNumberLine:


// Compose an attributed string that combines the values of the
// orderNumber and orderLine attributes.
- (NSString*)orderNumberLine
{
	NSString *tmpValue;
	NSMutableAttributedString *attrValue;
	NSDictionary *boldFont =
		[NSDictionary dictionaryWithObject:[NSFont boldSystemFontOfSize:10.0]
					forKey:NSFontAttributeName];
	NSDictionary *normalFont =
		[NSDictionary dictionaryWithObject:[NSFont systemFontOfSize:10.0]
					forKey:NSFontAttributeName];

	[self willAccessValueForKey:@"orderNumberLine"];
	// Put the order number in bold
	attrValue = [[NSMutableAttributedString alloc]
				initWithString:(self.orderNumber?self.orderNumber:@"")
				attributes:boldFont];
	// Then add a newline
	[attrValue appendAttributedString:[[NSAttributedString alloc]
				initWithString:@"\n"]];
	// And finally add the order line in normal font
	[attrValue appendAttributedString:[[NSAttributedString alloc]
				initWithString:(self.orderLine?[self.orderLine stringValue]:@"")
				attributes:normalFont]];

	tmpValue = [[NSAttributedString alloc] initWithAttributedString:attrValue];
	[self didAccessValueForKey:@"orderNumberLine"];

	return tmpValue;
}

The orderNumberLine method will compose a NSAttributedString that consist of the orderNumber and orderLine, separated by a newline. Because we’re using an attributed string we can also use different fonts for different elements, here the orderNumber is in 10 point bold, while the orderLine is in 10 point regular.

We can now bind the orderNumberLine value to a cell in an NSTableView in Interface Builder, then the cell will show a 2 line value. To properly display you need to increase the row height of the table view (in this case for a 10 point font size it to 26).

When you build the application adding only the orderNumberLine method to the Order class, you will notice that the cell that is bound to this method will display properly when it is redrawn (for example through scrolling or selecting the row). But when the value of either orderNumber or orderLine is changed elsewhere in the interface, the cell containing the composite string will not automatically redraw. This can easily be solved by doing an update to orderNumberLine attribute whenever one of its components (orderNumber or orderLine) changes. Then the bindings will take care that the UI is updated immediately. It doesn’t matter what you set the value of orderNumberLine to because it will be calculated upon retrieval anyway. The two following two methods demonstrate this:


// Override to trigger update of transient attribute orderNumberLine
- (void)setOrderNumber:(NSString*)value
{
	[self willChangeValueForKey:@"orderNumber"];
	[self setPrimitiveValue:value forKey: @"orderNumber"];
	[self didChangeValueForKey:@"orderNumber"];

	// Set the orderNumberLine to trigger updates to bound UI elements
	// The value we set it do doesn't matter as the actual value
	// is calculated whenever the orderNumberLine is fetched.
	self.orderNumberLine = @"";
}

// Override to trigger update of transient attribute orderNumberLine
- (void)setOrderLine:(NSNumber*)value
{
	[self willChangeValueForKey:@"orderLine"];
	[self setPrimitiveValue:value forKey: @"orderLine"];
	[self didChangeValueForKey:@"orderLine"];

	// Set the orderNumberLine to trigger updates to bound UI elements
	// The value we set it do doesn't matter as the actual value
	// is calculated whenever the orderNumberLine is fetched.
	self.orderNumberLine = @"";
}

To conclude, by adding a transient attribute, and writing three methods we can display a composed string in a cell. As this string can be an attributed string, we can apply different formatting to the elements composing the string. To look at the complete example, download it
here and open it in XCode.

Safari like button bar in Interface Builder

In the previous post on the use of NSGradient I showed a screenshot of a button bar similar to the one that is used in Safari to store your favourites. For an application I’m writing I have an NSTableView with date based rows, and I want to allow easy filtering the table contents on date ranges like month, last month, week, last week etcetera. A button bar is nice looking way of doing this, the screenshot below shows what this looks like (in Dutch, but you’ll get the idea).


Until recently you had to use for example AMButtonBar to get a button bar like this, but with Leopard our developers life has again been made easier: you can now create the button bar in Interface Builder, using only standard components.

Start by dropping an NSMatrix component (you will find this in the objects library under Views & Cells, Inputs & Values) onto your view. Per default it creates a matrix of 1 column, 2 rows with radio buttons on it. Then set the correct prototype on the NSMatrix object so that newly added buttons will have look right. Click on the little circle that is visible on the right bottom of the NSMatrix when it’s selected,



then the prototype attributes are shown in the Inspector window. Select Recessed as Style, and use centered Alignment. Select the right value under Position, based on whether you want to add an image to the buttons or not. In this case I choose not to use images.



Once the prototype is setup properly, select the NSMatrix object and have a look at the attributes in the Inspector window.



Change the number of rows and columns to meet your needs, in this case I’m using 1 row and 6 columns. You end up with one traditional radio button (the one that was originally there) and 5 new buttons according to the prototype. Select the first button and manually set its properties so that it matches the prototype as well.

Next, for each button edit the button Title and specify a unique Tag value in the Inspector window, so that you can distinguish which button was clicked in the attached method in your code later on. Optionally you can add a tooltip under the Identity tab on the Inspector window.

The final step is to write the method in the controller class, and attach it to the NSMatrix object. When you use the tag values to identify which button was clicked you will only need a single method, and don’t have to attach a method directly to the buttons. A sample implementation method:


- (IBAction)filterData:(id)sender
{
    int tag = [sender selectedTag];

    switch (tag) {
        case 1000: // Show all
            // Remove data  filtering so that all rows are shown
            break;
        case 1001: // Show this month
            // Filter data so that only rows with a date between
            // first and last day of current month are shown
            break;
        case 1002: // Show last month
            // Filter data so that only rows with a date between
            // first and last day of previous month are shown
            break;
        default:
            // Should never get here
            break;
    }
}

If you’re using coredata filtering the data is very easy, as you only need to create a NSPredicate object in which you specify the filtering criteria, and set that predicate on the NSArrayController that holds the rows. This is beyond the scope of this post, if you have questions on that let me know.