Norway


Topics

Topics

Today we’ll continue our series on finding hidden gems in UIKit with UIReferenceLibraryViewController.

Believe it or not there’s a entire (yes like for viewing the definition of words/terms) just hanging out inside UIKit.

Let’s give it a try! πŸ“–

We can present a new reference library view controller for any word/term we want like this:

present(
  UIReferenceLibraryViewController(term: "Spaceship"),
  animated: true, 
  completion: nil
)

- 301 1 - #301: The Dictionary Hiding Inside UIKit πŸ“–πŸ“Ό – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

Very cool.

This works great, but things start to break down when we look for something and it’s nowhere to be found:

- 301 2 - #301: The Dictionary Hiding Inside UIKit πŸ“–πŸ“Ό – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

We can fix this by checking if a term can be found before presenting, using the static dictionaryHasDefinition(forTerm:) function:

if UIReferenceLibraryViewController.dictionaryHasDefinition(forTerm: term) {
  present(
    UIReferenceLibraryViewController(term: term),
    animated: true, 
    completion: nil
  )
}

Much better.

UIReferenceLibraryViewController supports many different languages. New languages can be installed using the β€œManage” option in the bottom-left:

- 301 5 - #301: The Dictionary Hiding Inside UIKit πŸ“–πŸ“Ό – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

Finally, it’s worth noting that we get definition functionality for β€œfree” in UITextFields via the β€œLook Up” item in the UIMenu that’s shown when a user selects some text:

- 301 3 - #301: The Dictionary Hiding Inside UIKit πŸ“–πŸ“Ό – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

… and it’s also worth noting that the view controller that appears when a user taps one of these β€œLook Up” menu items in a UITextField appears to be far more advanced than the what we get when presenting a plain ol’ UIReferenceLibraryViewController:

- 301 4 - #301: The Dictionary Hiding Inside UIKit πŸ“–πŸ“Ό – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

The Dictionary results are still present, but we also get results across Music, Wikipedia, Movies, Websites, and even the Store. Very cool.

(And nope, UIKit does not seem to provide a way to trigger this fancier view controller directly. Anyone interested in this should probably file a Radar.)

That’s all for today. Those who want more UIKit B-Sides can check out these bites here.



Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here