Creating an AutoSuggest text field with Objective-C | what3words
All tutorials

Creating an AutoSuggest text field with Objective-C

easy

The Objective-C library provides UITexfield component to make using the what3words API even easier. In this tutorial we will go through using a component from the wrapper which allows the developer to add a search input to their app, powered by the what3words API AutoSuggest function. As you will see it is straightforward to use and allows you to configure it for your needs using a range of properties.

You will need a what3words API key to complete this tutorial.

2

Installation

CocoaPods (iOS 8+, OS X 10.10+)

You can use CocoaPods to install w3w-objectivec-wrapperby adding it to your Podfile:

platform :ios, '10.0'
use_frameworks!

target 'MyApp' do
    pod 'W3wSuggestionField', :git => 'https://github.com/what3words/w3w-autosuggest-textfield-objectivec.git'
end
Copied
3

Usage

Add the following import statement to the class

#import <W3wTextField.h>
Copied

Select ViewController.m and add the following code to the class

@property (weak, nonatomic) IBOutlet W3wTextField *suggestionField;
Copied

Now, open Main.storyboard and drag a UITextfield to the screen from the Object library. In the Identity Inspector change the custom class to W3wTextField. The last thing to do is to connect the action to the button. Click the yellow View Controller icon in the View Controller scene.

From the Connections Inspector (last tab on the right sidebar), click and drag the open circle next to suggestionField to newly created UITextfield in the storyboard.

Initialize the API by placing the following code into the -viewDidLoad method

 

 

[self.suggestionField setAPIKey:@"<Secret API Key>"];
Copied

To get selected 3 word address from dropdown menu, add the following code to the ViewController.m class

[self.suggestionField didSelect:^(NSString *selectedText) {
    NSLog(@"Three word address: %@", selectedText);
  }];
Copied

Available properties

There are a range of customisation properties you can use to configure the AutoSuggest component.

ParameterTypeDescription
autoFocusstringcomma separated lat/lng of point to focus on. Example value:"51.521251,-0.203586"
clipToCountrystringconfine results to a given country or comma separated list of countries. Example value:"GB,US"
clipToBoundingBoxstringConfine results to a bounding box specified using co-ordinates. Example value:"51.521,-0.343,52.6,2.3324"
clipToCirclestringRestrict AutoSuggest results to a circle, specified by lat,lng,kilometres, where kilometres in the radius of the circle. For convenience, longitude is allowed to wrap around 180 degrees. For example 181 is equivalent to -179.
clipToPolygonstringRestrict AutoSuggest results to a polygon, specified by a comma-separated list of lat,lng pairs. The polygon should be closed, i.e. the first element should be repeated as the last element; also the list should contain at least 4 entries. The API is currently limited to accepting up to 25 pairs. Example value:"51.521,-0.343,52.6,2.3324,54.234,8.343,51.521,-0.343"

Related tutorials

Back to top