iOS 5 : Look Up Definitions Using Dictionary Service

With iOS 5, there is now a dictionary service that you can access to look up definitions of words. UIReferenceLibraryViewController makes it as easy as creating an instance of the class and providing a term/word to lookup. The view controller will handle the display of the definition and managing the UI (scrolling, etc).

// Create the view controller
UIReferenceLibraryViewController *reference = 
  [[[UIReferenceLibraryViewController alloc] initWithTerm:@"homebrew"] autorelease];
 
[self presentModalViewController:reference animated:YES];
}


If you provide a word that doesn’t exist in the reference, the view controller will look as shown in the screenshot on the right.

Rather than show a view controller with no definition, you can make a call to the class method dictionaryHasDefinitionForTerm prior to creating an object:

// Check if the term exists...
NSString *str = @"sabrage";
if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:str] == YES)
{
  // Create the view controller
  UIReferenceLibraryViewController *reference = 
    [[[UIReferenceLibraryViewController alloc] initWithTerm:str] autorelease];
 
  [self presentModalViewController:reference animated:YES];
}

The default animation is to slide the view controller up from the bottom. To change the animation, set setModalTransitionStyle:

NSString *str = @"homebrew";
if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:str] == YES)
{
  // Create the view controller
  UIReferenceLibraryViewController *reference = 
    [[[UIReferenceLibraryViewController alloc] initWithTerm:str] autorelease];
 
  // Change the transition style
  [reference setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
 
  [self presentModalViewController:reference animated:YES];
}

Out of curiousity, I looked up a ridiculously long word, one that I would consider rather obscure: pneumonoultramicroscopicsilicovolcanoconiosis. Much to my surprise:

  1. You do nice work, thank you for this simple, short code example!

    • 我也觉得。但是如果我们有个retun NSString 就好! 你在中国那里?

  2. Hey, awesome article here – thanks for sharing :)

  3. Thanks for the code tip. But when I try and use it I get a message saying that the presentModalViewController:animated is unrecognised… any thoughts?

  4. Andrew, use the presentViewController instead

Comments are closed.