MKLocalSearch Class Reference

Inherits from
Conforms to
Framework
/System/Library/Frameworks/MapKit.framework
Availability
Available in iOS 6.1 and later.
Companion guide
Declared in
MKLocalSearch.h

Overview

An MKLocalSearch object initiates a map-based search operation and delivers the results back to your app asynchronously. Search objects are designed to perform one search operation only. To perform several different searches, you must create separate instances of this class and start them separately.

You use this class to perform programmatic searches of map-based information. For example, you can use this class to search for addresses or points-of-interest in much the same way the user might search for those items in the Maps app.

Tasks

Initializing a Search Request

Performing the Search

Properties

searching

A Boolean indicating whether the search is currently in progress. (read-only)

@property (nonatomic, readonly, getter=isSearching) BOOL searching;
Discussion

The value of this property is set to YES when a search is initiated and remains in that state until the search results (or an appropriate error) are delivered, at which time the property is set to NO.

Availability
  • Available in iOS 6.1 and later.
Declared In
MKLocalSearch.h

Instance Methods

cancel

Cancels an in-progress search operation.

- (void)cancel
Discussion

If no search operation is in progress, this method does nothing.

Availability
  • Available in iOS 6.1 and later.
Declared In
MKLocalSearch.h

initWithRequest:

Initializes and returns a search object configured with the specified parameters.

- (id)initWithRequest:(MKLocalSearchRequest *)request
Parameters
request

The search request information. This parameter must not be nil.

Return Value

An initialized search object.

Discussion

This method stores a copy of the object in the request parameter. So any changes you make to your request object after calling this method are ignored.

Availability
  • Available in iOS 6.1 and later.
Declared In
MKLocalSearch.h

startWithCompletionHandler:

Starts the search and delivers the results to the specified completion handler.

- (void)startWithCompletionHandler:(MKLocalSearchCompletionHandler)completionHandler
Parameters
completionHandler

The completion handler block that processes the results. This parameter must not be nil.

Discussion

You use this method to initiate a map-based search operation. The search runs until the results are delivered, at which point the specified completion handler is called.

You should call this method only once to start the search operation. Calling this method while the search is running does not stop the original search operation from finishing. However, for each subsequent call, the search object executes your completion handler and passes an error object to it.

The provided completion handler is always executed on your app’s main thread. The local search object keeps a reference to the completion handler block until the results (or an error) are delivered, at which point it relinquishes that reference.

Availability
  • Available in iOS 6.1 and later.
Declared In
MKLocalSearch.h

Constants

MKLocalSearchCompletionHandler

A completion handler block for a search operation.

typedef void (^MKLocalSearchCompletionHandler)(MKLocalSearchResponse *response, NSError *error);
Discussion

This block takes two parameters:

  • The response parameter contains the search results. If an error occurred, this parameter is set to nil and an appropriate error object is provided in the error parameter.

  • The error parameter is nil if the search was successful. If an error occurred during the operation, this parameter is set to an appropriate error object.

This block has no return value.

Availability
  • Available in iOS 6.1 and later.
Declared In
MKLocalSearch.h