iOS Developer Library

Developer

MapKit Framework Reference MKLocalSearch Class Reference

Options
Deployment Target:

On This Page
Language:

MKLocalSearch

Inherits From


Conforms To


Import Statement


Swift

import MapKit

Objective-C

@import MapKit;

Availability


Available in iOS 6.1 and later

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.

  • init(request:) - initWithRequest: Designated Initializer

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

    Declaration

    Swift

    init!(request request: MKLocalSearchRequest!)

    Objective-C

    - (instancetype)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.

    Import Statement

    Objective-C

    @import MapKit;

    Swift

    import MapKit

    Availability

    Available in iOS 6.1 and later

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

    Declaration

    Swift

    func startWithCompletionHandler(_ completionHandler: MKLocalSearchCompletionHandler!)

    Objective-C

    - (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.

    Import Statement

    Objective-C

    @import MapKit;

    Swift

    import MapKit

    Availability

    Available in iOS 6.1 and later

  • searching searching Property

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

    Declaration

    Swift

    var searching: Bool { get }

    Objective-C

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

    Discussion

    The value of this property is set to YEStrue 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 NOfalse.

    Import Statement

    Objective-C

    @import MapKit;

    Swift

    import MapKit

    Availability

    Available in iOS 6.1 and later

  • Cancels an in-progress search operation.

    Declaration

    Swift

    func cancel()

    Objective-C

    - (void)cancel

    Discussion

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

    Import Statement

    Objective-C

    @import MapKit;

    Swift

    import MapKit

    Availability

    Available in iOS 6.1 and later

Data Types

  • A completion handler block for a search operation.

    Declaration

    Swift

    typealias MKLocalSearchCompletionHandler = (MKLocalSearchResponse!, NSError!) -> Void

    Objective-C

    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.

    Import Statement

    Objective-C

    @import MapKit;

    Swift

    import MapKit

    Availability

    Available in iOS 6.1 and later