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.

Full sample apps for the component can be found in our Github repo
<a class=”w3w-button” href=”https://github.com/what3words/w3w-swift-samples” target=”_blank” rel=”noopener” data-variant=”medium” data-testid=”ios-sample-apps-github”><img class=”alignleft” style=”margin-right: 5px;” src=”https://corpassets.what3words.com/wp-content/uploads/2019/11/github.256×249-e1709546116502.png” alt=”github icon white” width=”25″ height=”24″ /> iOS Sample Apps</a>

1
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"
Mobile AppAdd a 3 word address input fieldObjective-C

Related tutorials