All tutorials
Objective-C
The what3words Free API plan will no longer include convert-to-coordinate requests. To continue using this feature, please upgrade to one of our paid plans starting from £7.99/month. Learn more about our API plans here.
As a reminder, emergency services can make use of the what3words API for free, and we have a dedicated plan for NGOs and registered charities. If your organisation falls within these categories, please get in touch. For any questions or assistance, feel free to reach out to support@what3words.com.
The Objective-C wrapper is useful for Objective-C developers who wish to seamlessly integrate the what3words Public API into their Objective-C applications, without the hassle of having to manage the low level API calls themselves.
Full sample apps for the wrapper can be found in our Github repo
iOS Sample Apps
You can install the what3Words API V3 Objective-C Wrapper into your project using CocoaPods, or Carthage (see the package manager code below).
Find out more on this repository
platform :ios, '9.0' use_frameworks! target 'MyApp' do pod 'what3words', :git => 'https://github.com/what3words/w3w-objectivec-wrapper.git' end
Carthage:
github "what3words/w3w-objectivec-wrapper"
For manual installation, Drop the W3wGeocoder.m and W3wGeocoder.h file into your project.
Use these header files:
#import "W3wGeocoder.h" #import <CoreLocation/CoreLocation.h>
Or, if you are using Carthage:
#import <what3words/what3words.h> #import <CoreLocation/CoreLocation.h>
First create a W3wGeocoder object:
W3wGeocoder *api = [[W3wGeocoder alloc] initWithApiKey:@"<Your Secret Key>"];
Convert to what3words address
This function converts coordinates (expressed as latitude and longitude) to a what3words address. It takes the latitude and longitude as a CLLocationCoordinate2D object.
More information about ConvertTo3wa
, including returned results is available in the what3words REST API documentation.
Find the words for (51.520847, -0.195521
):
CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(51.520847, -0.195521); [api convertTo3wa:coordinates completion:^(W3wPlace *place, W3wError *error) { NSLog(@"%@", place.words); }];
Convert to coordinates
This function converts a what3words address to a position, expressed as coordinates of latitude and longitude.
It takes the words parameter as a string of a what3words 'table.book.chair'
More information about ConvertToCoordinates
, including returned results is available in the what3words REST API documentation.
Find the words for ///filled.count.soap
:
[api convertToCoordinates:@"filled.count.soap" format:JSON completion:^(W3wPlace *place, W3wError *error) { NSLog(@"Coordinates are: (%f, %f)", place.coordinates.latitude, place.coordinates.longitude); }];
AutoSuggest
When presented with a what3words address which may be incorrectly entered, AutoSuggest returns a list of potential correct 3 word addresses. It needs the first two words plus at least the first character of the third word to produce suggestions.
This method provides corrections for mis-typed words (including plural VS singular), and words being in the wrong order.
Optionally, clipping can narrow down the possibilities, and limit results to:
- One or more countries
- A geographic area (a circle, box or polygon)
This dramatically improves results, so we recommend that you use clipping if possible.
To improve results even further, set the Focus
to user’s current location. This returns an Array?
of W3wSuggestion
objects.
More information about AutoSuggest
, including returned results is available in the what3words REST API documentation.
Code example Simple basic call:
[api autosuggest:@"geschaft.planter.carciofi" completion:^(NSArray *suggestions, W3wError *error) { W3wSuggestion *first_match = suggestions[0]; NSLog(@"%@", first_match.words); }];
Code example AutoSuggest, clipping the results returned to Germany:
[api autosuggest:@"geschaft.planter.carciofi" parameters:@[[AutoSuggestOption clipToCountry:@"DE"], [AutoSuggestOption numberResults:4]] completion:^(NSArray *suggestions, W3wError *error) { W3wSuggestion *first_match = suggestions[0]; NSLog(@"%@", first_match.words); }];
Code example Focus on (51.4243877,-0.34745)
[api autosuggest:@"fun.with.code" parameters:@[[AutoSuggestOption focus:CLLocationCoordinate2DMake(51.4243877, -0.3474524)], [AutoSuggestOption numberResults:6]] completion:^(NSArray *suggestions, W3wError *error) { W3wSuggestion *first_match = suggestions[0]; NSLog(@"%@", first_match.words); }];
Code example AutoSuggest, with Generic Voice input type.
[api autosuggest:@"fun with code" parameters:@[[AutoSuggestOption fallBackLanguage:@"EN"], [AutoSuggestOption inputType:GENERIC_VOICE]] completion:^(NSArray *suggestions, W3wError *error) { W3wSuggestion *first_match = suggestions[0]; NSLog(@"%@", first_match.words); }];
Grid section
Grid section returns a section of the what3words 3m x 3m grid as a set of horizontal and vertical lines covering the requested area, which can then be drawn onto a map.
The requested box must not exceed 4km from corner to corner, or a BadBoundingBoxTooBig
error will be returned.
More information about GridSection
, including returned results is available in the what3words REST API documentation.
Get a grid for 52.207988,0.116126
) in the south west, and 52.208867,0.117540
in the north east:
[api gridSection:52.208867 west_lng:0.117540 north_lat:52.207988 east_lng:0.116126 format:JSON completion:^(NSArray *grid, W3wError *error) { W3wLine *first_line = grid[0]; NSLog(@"First line goes from (%.5f,%.5f) to (%.5f,%.5f)", first_line.start.latitude, first_line.start.longitude, first_line.end.latitude, first_line.end.longitude); }];
Available languages
This function returns the currently supported languages. It will return the two letter code, and the name of the language both in that language and in English.
More information about AvailableLanguages
, including returned results is available in the what3words REST API documentation.
[api availableLanguages:^(NSArray *languages, W3wError *error) { W3wLanguage *language = languages[0]; NSLog(@"First language returned: %@", language.name); }];
Handling errors
All functions call the completion block with error as the second parameter. Be sure to check it for possible problems.
[api convertToCoordinates:@"aaaa.bbbb.cccc" format:JSON completion:^(W3wPlace *result, W3wError *error) { if (error) NSLog(@"There was an %@ error: %@", error.code, error.message); }];
If you encounter errors or issues related to convert-to-coordinate requests while using the Free plan, please check the network panel for the following error message Error 402 payment required
and its response, indicating the need to upgrade to a higher plan:
{ "error": { "code": "QuotaExceeded", "message": "Quota Exceeded. Please upgrade your usage plan, or contact support@what3words.com" } }
For more information, visit our API plans page. If you need further assistance, contact support@what3words.com.
Complete example using AutoSuggest to find a location.
#import "W3wGeocoder.h" #import <CoreLocation/CoreLocation.h> W3wGeocoder *api = [[W3wGeocoder alloc] initWithApiKey:@"<Your Secret Key>"]; AutoSuggestOption *france = [AutoSuggestOption clipToCountry:@"FR"]; AutoSuggestOption *place = [AutoSuggestOption focus:CLLocationCoordinate2DMake(48.856618, 2.3522411)]; AutoSuggestOption *count = [AutoSuggestOption numberResults:10]; [api autosuggest:@"hangry.selfie.x" parameters:@[france, place, count] completion:^(NSArray *suggestions, W3wError *error) { if (error) NSLog(@"There was an %@ error: %@", error.code, error.message); else { NSLog(@"Suggestions Returned:"); for (W3wSuggestion *suggestion in suggestions) { NSLog(@"%@ is near %@", suggestion.words, suggestion.nearestPlace); } } }];